/[drupal]/contributions/modules/ec_linkpoint/ec_linkpoint.module
ViewVC logotype

Contents of /contributions/modules/ec_linkpoint/ec_linkpoint.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Mon Nov 24 13:55:10 2008 UTC (12 months ago) by webengr
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +850 -0 lines
File MIME type: text/x-php
Initial version of ec_linkpoint
1 <?php
2 // $Id: ec_linkpoint.module
3 /*
4 * 20081110
5 * A lot of comments left in for developing, final
6 * may want to remove comments, or have a commented
7 * file and then file with comments removed for module file
8 *
9 * A module used for Linkpoint API payment gateway
10 * with Drupal e-commerce, not ubercart
11 *
12 * This derivation done by Paul Pruett, ec_linkpoint@cocoavillage.com
13 * because, well I needed a linkpoint api for myself ;).
14 * If someone wants to run with this module full time, or help, let me know!
15 *
16 ********************************************************************
17 * OTHER NOTES:
18 *
19 * ACHTUNG! The codes expects that the website supports SSL, https
20 * and if your website does not allow redirect to https, or is not
21 * forced to be https it may fail in a way not explaining. SO be sure
22 * to setup webserver so same address with either http or https works!
23 *
24 * This is a credit card processing interface using the Linkpoint API payment
25 * gateway. It requires php to support CURL and SSL.
26 *
27 * Configuration:
28 * From Linkpoint, you get a Store ID, and a certificate (.pem) file.
29 * Upload the .pem file to your web server, preferrably NOT under the
30 * public_html/www directory.
31 *
32 * Enter your Store ID, and the location of the .pem file. You can
33 * use relative path names. For example, if you uploaded the file to
34 * your home directory (the one above your public_html directory), then
35 * you can use "../12345.pem"
36 *
37 * Note: providing full path recommended - relative path might not work
38 * on some systems resulting the following error:
39 * "unable to use client certificate (no key found or wrong pass phrase?)"
40 *
41 ********************************************************************
42 */
43
44
45 // We set these defines php constants here at runtime, and why
46 // They are used later in the function ec_linkpoint_format_not_accepted
47 define('LINKPOINT_ERROR_SHORT_VIEW', 0);
48 define('LINKPOINT_ERROR_MEDIUM_VIEW', 1);
49 define('LINKPOINT_ERROR_LARGE_VIEW', 2);
50
51
52 /********************************************************************
53 * Drupal Hooks
54 ********************************************************************/
55
56
57 /**
58 * Implementation of hook_menu().
59 */
60 function ec_linkpoint_menu($may_cache) {
61
62 $items = array();
63
64 if ($may_cache) {
65 $items[] = array(
66 'path' => 'store/payment/ec_linkpoint',
67 'title' => t('Credit Card Payment'),
68 'callback' => 'drupal_get_form',
69 'callback arguments' => array('ec_linkpoint_form'),
70 'access' => true,
71 'type' => MENU_CALLBACK,
72 );
73 $items[] = array(
74 'path' => 'admin/ecsettings/ec_linkpoint',
75 'title' => 'Linkpoint',
76 'callback' => 'drupal_get_form',
77 'callback arguments' => array('ec_linkpoint_ec_settings'),
78 'access' => user_access('administer store'),
79 'type' => MENU_NORMAL_ITEM,
80 'description' => t('Configure for the Linkpoint payment gateway'),
81 );
82 }
83 return $items;
84 }
85
86
87 /**
88 * Implementation of hook_help().
89 */
90 function ec_linkpoint_help($section = 'admin/help#ec_linkpoint') {
91 switch ($section) {
92 case 'admin/ecsettings/ec_linkpoint':
93 return t("You need to have a linkpoint merchant account in order to use this module.");
94 case 'ec_linkpoint/form_submit_guidlines':
95 return t("Be carefull not submit this form again if already paid, or you may be double billed!");
96 }
97 }
98
99
100
101
102 // This function provides the settings that you will fill out under menu e-commerce settings/linkpoint
103 // and the sttings like linkpoint store id will be used to transact
104 function ec_linkpoint_ec_settings() {
105
106 $form['ec_linkpoint_help'] = array(
107 '#type' => 'textarea',
108 '#title' => t('Explanation or submission guidelines'),
109 '#description' => t('This text will be displayed at the top of the credit card submission form.'),
110 '#rows' => 5,
111 '#required' => TRUE,
112 '#default_value' => variable_get('ec_linkpoint_help', ec_linkpoint_help('ec_linkpoint/form_submit_guidlines')),
113 );
114
115 $form['ec_linkpoint_login'] = array(
116 '#type' => 'textfield',
117 '#title' => t('Store ID'),
118 '#description' => t("Enter your merchant Store ID."),
119 '#required' => TRUE,
120 '#maxlength' => 70,
121 '#default_value' => variable_get('ec_linkpoint_login', ''),
122 );
123
124 $form['ec_linkpoint_ssl_certificate'] = array(
125 '#type' => 'textfield',
126 '#title' => t('SSL Certificate'),
127 '#description' => t("Enter your merchant SSL certificate (.pem). It is preferred that you enter full server path (like /home/smb/1234567.pem, not ./1234567.pem) "),
128 '#required' => TRUE,
129 '#maxlength' => 255,
130 '#default_value' => variable_get('ec_linkpoint_ssl_certificate', ''),
131 );
132
133 $form['ec_linkpoint_url'] = array(
134 '#type' => 'textfield',
135 '#title' => t('Linkpoint processing URL'),
136 '#description' => t('URL of the secure payment processing page, including the port number.'),
137 '#required' => TRUE,
138 '#maxlength' => 70,
139 '#default_value' => variable_get('ec_linkpoint_url', 'https://secure.linkpt.net:1129/LSGSXML'),
140 );
141
142 $form['ec_linkpoint_success_url'] = array(
143 '#type' => 'textfield',
144 '#title' => t('Successful payment URL'),
145 '#description' => t("This is the destination to which you would like to send your customers when their payment has been successfully completed. The URL must be a Drupal system path (without leading slash), e.g. 'product'. If not set, user will see transaction invoice (recommended)."),
146 '#required' => FALSE,
147 '#maxlength' => 70,
148 '#default_value' => variable_get('ec_linkpoint_success_url', ''),
149 );
150
151 // DEBUG linkpoint payment; Note the code uses a variable set to decide
152 // to use LIVE or GOOD for Transaction mode when order processing
153 // Note, the ubercart linkpoint api appears to use the aliases of Production
154 // for LIVE, and Test for GOOD, instead of debug enabled/disabled.
155 $form['ec_linkpoint_debug'] = array(
156 '#type' => 'radios',
157 '#title' => t('LinkPoint test mode'),
158 '#options' => array(t('Disabled'), t('Enabled')),
159 '#description' => t('If enabled, transactions will be sent in test GOOD mode - any card numbers will be approved and cards will not be charged. Also, in test mode full connection error description is shown instead of Sorry - Could not connect to payment gateway.'),
160 '#default_value' => variable_get('ec_linkpoint_debug', 0),
161 );
162
163
164
165 // We could add this setting option that is not in authorize.net module, may want to not make it an option
166 // will need to if/then the later checks for https...
167 //$form['ec_linkpoint_httpsforce'] = array(
168 // '#type' => 'radios',
169 // '#title' => t('For security verify using url with https for transaction or fault with access denied.'),
170 // '#options' => array(t('Disabled'), t('Enabled')),
171 // '#description' => t('If enabled, this module will redirect and expect url for transaction to support https, ssl. Recommend that this be always enabled and only disable for testing if necessary.'),
172 // '#default_value' => variable_get('ec_linkpoint_httpsforce', 1),
173 // );
174 //
175
176
177
178 // later in the xml we give the ordertype, transaction method. the orig code set
179 // this to "SALE" but here we are giving a setting to have option to set to either
180 // preauth or sale, but default to "SALE" maybe. For tangible shipped items, you
181 // should perhaps use PREAUTH, it authorizes a credit card but does not charge it
182 // But then go back on your linkpoint vitual terminal and AUTH it when shipping?
183 // Will set default to PREAUTH to be conservative.
184 $form['ec_linkpoint_ordertype'] = array(
185 '#type' => 'radios',
186 '#title' => t('Transaction Type'),
187 '#default_value' => variable_get('ec_linkpoint_ordertype', 'PREAUTH'),
188 '#options' => array(
189 'PREAUTH' => t('PREAUTH'),
190 'SALE' => t('SALE'),
191 ),
192 '#description' => t('Linkpoint states for tangible items or anything that will be shipped to always use PREAUTH. If you are selling a download item etc, use SALE. PREAUTH authorizes a credit card but does not charge it. Go to <a target="_blank" href="http://www.linkpointcentral.com">Linkpoint Central</a> > Reports > Transctions to complete the transaction. SALE automatically charges the card.'),
193 );
194
195 $form['ec_linkpoint_format_not_accepted'] = array(
196 '#type' => 'radios',
197 '#title' => t('How to format error message if card is not accepted'),
198 '#options' => array(
199 LINKPOINT_ERROR_SHORT_VIEW => t('Simply Accepted, Declined or Duplicate'),
200 LINKPOINT_ERROR_MEDIUM_VIEW => t('Linkpoint\'s error message without code (e.g. "The order already exists in the database")'),
201 LINKPOINT_ERROR_LARGE_VIEW => t('Full Linkpoint\'s error message (e.g. "SGS-005003: The order already exists in the database.")'),
202 ),
203 '#default_value' => variable_get('ec_linkpoint_format_not_accepted', 0),
204 );
205
206 return system_settings_form($form);
207 }
208
209
210 /********************************************************************
211 * E-commerce Hooks
212 ********************************************************************/
213
214 // here is something different than ubercart, the newer e-commerce core has
215 // hooks for payment interface, like hook_paymentapi
216 // for the hook_paymentapi we need to return:
217 // &$txn The transaction object for an order. This keeps growing in data from screen to screen.
218 // $op What kind of action is being performed.
219 // see: http://drupalecommerce.org/api/function/hook_paymentapi/53
220 //
221 // BTW, we could set form variables so that the return could be custom, but for
222 // now we will leave it 'Pay with credit card'
223 //
224 // notes for $op parameters
225 // "display name"
226 // The name of the method displayed to customers.
227 // This is purely for display purposes and my be descriptive.
228 // "on checkout"
229 // Called after the customer selects their payment method.
230 // This can be used for any specific validation the payment method needs to impose.
231 // Be sure to call form_set_error() to raise a warning to the system.
232 // "form"
233 // Called durring the checkout process. This is used to display options
234 // to the user for choosing between payment options. Nothing wil be displayed
235 // if only one payment option exists.
236 // "update/insert"
237 // Called after the user has submitted the payment page so any additional
238 // payment details can be stored in the database.
239 // "payment page"
240 // Display the form for accepting credit card information or redirect to a
241 // third party payment processor.
242 // "delete"
243 // Called when the transaction is being deleted
244 //
245 /**
246 * Implementation of hook_paymentapi().
247 */
248 function ec_linkpoint_paymentapi(&$txn, $op) {
249 switch ($op) {
250 case 'display name':
251 return t('Pay with Credit Card');
252 // we have them type the credit card on our site with the ec_linkpoint_goto form
253 // then we curl the xml info with linkpoint, therefore we use "payment page" case.
254 case 'payment page':
255 return ec_linkpoint_goto($txn);
256 }
257 }
258
259
260 // fyi, another hook that ubercart does not use but we need,
261 // because e-commerce gateways payment modules
262 /*
263 * Implementation of hook_ec_transactionapi().
264 *
265 * http://drupalecommerce.org/api/function/hook_ec_transactionapi/53
266 *
267 * Handle store transaction actions.
268 *
269 * &$txn
270 * A transaction object, passed by reference.
271 *
272 * $op
273 * A string containing the name of the ec_transactionapi operation. Possible values:
274 *
275 * "insert"
276 * A new transaction is being created. Called by e.g. store_transaction_save(), with
277 * $txn an object containing edit fields Standard records in ec_transaction etc. have
278 * already been inserted. No return value.
279 * "load"
280 * A transaction is being loaded from the database. Called by e.g. store_transaction_load(),
281 * with $txn containing the transaction loaded so far.
282 * "update"
283 * A transaction is being updated. Called by e.g. store_transaction_save(), with $txn
284 * an object containing edit fields Standard records in ec_transaction etc. have already
285 * been updated. No return value.
286 * "delete"
287 * A transaction is being deleted. The parameter $txn contains all the transaction
288 * information, since the standard records in ec_transaction etc. have already been
289 * deleted. No return value.
290 * "validate"
291 * Transaction $txn is being inserted. Argument $a3 contains the section to validate.
292 * Errors should be set with form_set_error(). No return value. Called by e.g.
293 * store_transaction_validate(), from e.g.
294 * Menu: ?q=admin/store with $_POST['op'] == [ t('Update transaction') or t('Create new transaction')
295 * ] on submission of a form to create a new transaction or update an existing transaction.
296 *
297 * $a3 Additional argument, depending on $op.
298 *
299 * For "validate", this is the section to validate, in upper case. Possible values include:
300 * o 'OVERVIEW':
301 * o 'ADDRESSES':
302 * o 'ITEMS':
303 * o 'ALL' (default): Validate all sections.
304 *
305 */
306
307 function ec_linkpoint_ec_transactionapi(&$txn, $op, $a3 = NULL, $a4 = NULL) {
308 // sanity check, we should not be here if not using ec_linkpont
309 // maybe we sould check for curl_ini here also?
310 if ($txn->payment_method != 'ec_linkpoint') return NULL;
311 switch ($op) {
312 case 'load':
313 $txn->payment = db_fetch_object(db_query("SELECT * FROM {ec_linkpoint_transaction} WHERE txnid = %d", $txn->txnid));
314 break;
315 case 'insert':
316 case 'update':
317 ec_linkpoint_save($txn);
318 break;
319 case 'delete':
320 ec_linkpoint_delete($txn);
321 break;
322 }
323 }
324
325
326
327 // ?? I guess this is okay, to call our own function from function ec_linkpoint_ec_transactionapi
328 // here use save to return the 'update' case and delete to take action for 'delete' case ??
329
330 function ec_linkpoint_save($txn) {
331 if (is_numeric($txn->txnid) && is_numeric($txn->anid)) {
332
333 if (db_result(db_query("SELECT COUNT(txnid) FROM {ec_linkpoint_transaction} WHERE txnid = '%s'", $txn->txnid))) {
334 db_query("UPDATE {ec_linkpoint_transaction} SET anid = '%s', amount = '%f' WHERE txnid = %d", $txn->anid, $txn->amount, $txn->txnid);
335 }
336 else {
337 db_query("INSERT INTO {ec_linkpoint_transaction} (txnid, anid, amount) VALUES (%d, '%s', '%f')", $txn->txnid, $txn->anid, $txn->amount);
338 }
339 }
340 }
341
342 function ec_linkpoint_delete($txn) {
343 db_query('DELETE FROM {ec_linkpoint_transaction} WHERE txnid = %d', $txn->txnid);
344 }
345
346
347
348 /**
349 * This funcition is Called immediately after the user
350 * has clicked the checkout button.
351 * Redirect the user to the secure server if not already using https
352 * to collect credit card information.
353 * (note we are assuming the secure serer is same URL except https instead of http.)
354 */
355 function ec_linkpoint_goto($txn) {
356
357 global $base_url;
358 $payment_url = str_replace('http://', 'https://', url('store/payment/ec_linkpoint/'. $txn->txnid, NULL, NULL, TRUE));
359
360 drupal_goto($payment_url);
361 exit();
362 }
363
364
365
366 // ???? do we need this? I think so, this calls the form to get credit card info from customer?
367 // Don't see similar function for authorize_net.module ???
368 // think we pasted this snippet from http://drupal.org/node/142150 to see if could be used ???
369 /**
370 * Controller for collecting and processing credit card data.
371 */
372 //function ec_linkpoint_page($txnid = null) {
373 //
374 // $output = ec_linkpoint_form($txnid);
375 // print theme('page', $output);
376 //
377 //}
378
379
380 /**
381 * Build the credit card form.
382 */
383 function ec_linkpoint_form($txnid) {
384 global $user, $base_url;
385
386 $t = store_transaction_load($txnid);
387
388 // Make sure the user owns the transaction or is an admin.
389 if ($user->uid != $t->uid && $user->uid != 1 && !user_access('administer store')) {
390 return drupal_access_denied();
391 }
392
393 // Make sure the user is connected via SSL by checking the URL for https prefix
394
395 if (!$_SERVER['HTTPS']) {
396 drupal_access_denied();
397 return;
398 }
399
400 //not sure this code is correct???
401 // if ($t->items) {
402 // foreach ($t->items as $p) {
403 // $product = product_load($p);
404 // $subtotal += $p->qty * $p->price;
405 // $items[] = t('%order of <b>%title</b> at %price each', array('%order' => format_plural($p->qty, '1 order', '@count orders'), '%title' => $p->title, '%price' => payment_format($product->price))). "\n";
406 // }
407 // }
408 //
409 //code from authorize.net
410 if ($t->items) {
411 foreach ($t->items as $p) {
412 $subtotal += $p->qty * $p->price;
413 $items[] = t('%order of <b>%title</b> at %price each', array('%order' =>
414 format_plural($p->qty, '1 order', '@count orders'), '%title' => $p->title,
415 '%price' => payment_format($p->price)));
416 }
417 }
418
419
420 $form['help'] = array('#value' => t('<div class="help">%ec_linkpoint_help</div>', array('%ec_linkpoint_help' => variable_get('ec_linkpoint_help', ec_linkpoint_help('ec_linkpoint/form_submit_guidlines')))));
421
422 $form['items'] = array('#value' => theme('item_list', $items, t('Your items')). '</p>');
423
424 // Prepare the values of the form fields.
425 // note, linkpoint expecting 2 digit year, will use php substr during xml generation to convert
426 $years = drupal_map_assoc(range(2008, 2021));
427 $months = drupal_map_assoc(array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'));
428
429 $form['expiry_date'] = array(
430 '#type' => 'fieldset',
431 '#title' => t('Expiration Date'),
432 );
433 $form['expiry_date']['cc_month'] = array(
434 '#type' => 'select',
435 '#title' => t('Month'),
436 '#default_value' => ($month ? $month : date('m')),
437 '#options' => $months,
438 '#description' => null,
439 '#extra' => 0,
440 '#multiple' => false,
441 '#required' => true,
442 );
443 $form['expiry_date']['cc_year'] = array(
444 '#type' => 'select',
445 '#title' => t('Year'),
446 '#default_value' => ($year ? $year : date('Y')),
447 '#options' => $years,
448 '#description' => null,
449 '#extra' => 0,
450 '#multiple' => false,
451 '#required' => true,
452 );
453
454 $form['details'] = array(
455 '#type' => 'fieldset',
456 '#title' => t('Card details'),
457 );
458
459 $form['details']['cc_firstname'] = array(
460 '#type' => 'textfield',
461 '#title' => t('Cardholder\'s first name'),
462 '#default_value' => $t->address['billing']->firstname,
463 '#size' => 50,
464 '#maxlength' => 50,
465 );
466
467 $form['details']['cc_lastname'] = array(
468 '#type' => 'textfield',
469 '#title' => t('Cardholder\'s last name'),
470 '#default_value' => $t->address['billing']->lastname,
471 '#size' => 50,
472 '#maxlength' => 50,
473 );
474
475 $form['details']['cc_number'] = array(
476 '#type' => 'textfield',
477 '#title' => t('Credit Card Number'),
478 '#default_value' => '',
479 '#size' => 21,
480 '#maxlength' => 21,
481 '#description' => t('Just the credit card number without dashes nor spaces'),
482 '#attributes' => null,
483 '#required' => true,
484 );
485
486 $form['details']['cc_ccv'] = array(
487 '#type' => 'textfield',
488 '#title' => t('CCV Security Code'),
489 '#description' => t('Three digit number on back of card'),
490 '#size' => 3,
491 '#maxlength' => 3,
492 '#required' => true,
493 );
494
495 $form['txnid'] = array(
496 '#type' => 'value',
497 '#value' => $txnid,
498 );
499
500 $form['submit'] = array(
501 '#type' => 'submit',
502 '#value' => t('Place your order'),
503 );
504
505 $form['#method'] = 'POST';
506 // added option to configuration, httpsforce, if disabled don't use https
507 // ?????? need to put an if/then here
508 $form['#action'] = str_replace('http://', 'https://', url("store/payment/ec_linkpoint/$txnid", NULL, NULL, TRUE));
509 return $form;
510 }
511
512
513 /**
514 * Ensure the integrity of the user-submitted data.
515 */
516 // we may want to go further here and make sure
517 // the credit card is a credit card number and
518 // the username and other information makes sense...
519 //
520 // some things the XML needs to have...
521 // 1. Make sure the amount for chargetotal is not blank.
522 // 2. Make sure expiration year is only 2 digits
523 // 3. Make sure there is no dollar sign for the amount.
524 // 4. Make sure there are no symbols like an ampersand, apostrophe, or letters with accents
525 // 5. If there is no shipping make sure you pass zero for the amount
526 // 6. Make sure there are no commas in the amount for chargetotal
527 //
528 function ec_linkpoint_form_validate($form_id, $edit) {
529 $errors = array();
530 if (!$edit['cc_number']) {
531 $errors['cc_number'] = t('You must enter a credit card number.');
532 }
533 elseif (!is_numeric($edit['cc_number'])) {
534 $errors['cc_number'] = t('Error in credit card number. Please make sure it is typed correctly and without dashes nor spaces.');
535 }
536 // sanity check that the credit card number is not to small of a number,
537 // usually 16 characters like 41111111111111
538 // could do by length, but since its a number we could just simply do a greater than hack...
539 elseif ($edit['cc_number']<100000000) {
540 $errors['cc_number'] = t('Check you credit card number, seems to be to small.');
541 }
542 // while we are at it, we can also say the CCV number should be numeric and not to small
543 elseif (!is_numeric($edit['cc_ccv'])) {
544 $errors['cc_ccv'] = t('Check your CCV security code, expecting a number without spaces nor dashes.');
545 }
546 // and that CCV is not to small of a number
547 elseif ($edit['cc_ccv'] < 10) {
548 $errors['cc_ccv'] = t('Check your CCV security code, seems to be to small of a number.');
549 }
550 // ? Card Holders first name should not be blank and a least a few characters long...
551 // if so another elseif here?
552 // ??? Cardholders last name should not be blank and a least a few characters long...
553 // if so another elseif here?
554
555
556 foreach ($errors as $name => $message) {
557 form_set_error($name, $message);
558 }
559
560 return count($errors) == 0;
561 }
562
563 /**
564 * Send the HTTPS POST request and process the returned data.
565 *
566 * similar to uc_linkpoint_api_charge for ubercart ?
567 *
568 */
569 function ec_linkpoint_form_submit($form_id, $edit) {
570
571 // we need to get the order information to make our xml for curl
572 // can use store_transacton_load from drupal ecommerce store module
573 // http://ecommerce.heydon.com.au/api/function/store_transaction_load/53
574 // could some data in load may be serialized requiring unserialize() ???
575 $storeOrder = store_transaction_load($edit['txnid']);
576
577 global $user;
578 global $base_url;
579
580 //Make sure the user owns the transaction or is admin.
581 if ($user->uid != $storeOrder->uid && $user->uid != 1 && !user_access('administer store')) {
582 drupal_access_denied();
583 }
584
585 //Make sure the user is connected via SSL
586 if (!$_SERVER['HTTPS']) {
587 drupal_access_denied();
588 }
589
590 // default result is live, but if debug change to GOOD
591 $linkptResult = 'LIVE';
592
593 if (variable_get('ec_linkpoint_debug', 1)) {
594 $linkptResult = 'GOOD';
595 }
596
597 $d['cert'] = variable_get('ec_linkpoint_ssl_certificate', '');
598
599
600 // build order description to pass in comment to linkpoint xml
601 $orderdescript = '';
602
603 // need some code here to generate a brief description of order
604 // to pass to linkpoint comment in the generated xml ??????????????
605 // maybe we can use something like items[] array in ec_linkpoit_form function ?
606
607 // makesure it is less than 255 characters
608 $orderdescript = substr($orderdescript, 0, 255);
609
610
611
612 // ***********************************************
613 // Creaton of the XML string to send to linkpoint
614 // ***********************************************
615 // note, added address2/street2 to old code
616 // note, added PREAUTH/SALE choice to ordertype
617
618 $xml ="<order>";
619
620 $xml .="<billing>";
621 $xml .="<name>" . $edit['cc_firstname'] . " " . $edit['cc_lastname'] ."</name>";
622 $xml .="<company>" . $storeOrder->address['billing']->company . "</company>";
623 $xml .="<address1>" . $storeOrder->address['billing']->street1 . "</address1>";
624 $xml .="<address2>" . $storeOrder->address['billing']->street2 . "</address2>";
625 $xml .="<city>" . $storeOrder->address['billing']->city . "</city>";
626 $xml .="<state>" . $storeOrder->address['billing']->state . "</state>";
627 $xml .="<zip>" . $storeOrder->address['billing']->zip . "</zip>";
628 $xml .="<country>" . store_get_country($storeOrder->address['billing']->country) . "</country>";
629 $xml .="<phone>" . $storeOrder->address['billing']->phone . "</phone>";
630 $xml .="<email>" . $storeOrder->mail . "</email>";
631 $xml .="</billing>";
632
633 $xml .="<shipping>";
634 $xml .="<name>" . $storeOrder->address['shipping']->firstname . " " . $storeOrder->address['shipping']->lastname ."</name>";
635 $xml .="<address1>" . $storeOrder->address['shipping']->street1 . "</address1>";
636 $xml .="<address2>" . $storeOrder->address['shipping']->street2 . "</address2>";
637 $xml .="<city>" . $storeOrder->address['shipping']->city . "</city>";
638 $xml .="<state>" . $storeOrder->address['shipping']->state . "</state>";
639 $xml .="<zip>" . $storeOrder->address['shipping']->zip . "</zip>";
640 $xml .="<country>" . store_get_country($storeOrder->address['shipping']->country) . "</country>";
641 $xml .="</shipping>";
642
643 $xml .="<orderoptions>";
644 $xml .="<result>" . $linkptResult . "</result>";
645 $xml .="<ordertype>" . variable_get('ec_linkpoint_ordertype', '') . "</ordertype>";
646 $xml .="</orderoptions>";
647
648 $xml .="<merchantinfo>";
649 $xml .="<configfile>" . variable_get('ec_linkpoint_login', '') . "</configfile>";
650 $xml .="</merchantinfo>";
651
652 $xml .="<creditcard>";
653 // something screwy, don't clarify edit array.... and works.. my bad syntax...
654 //$xml .="<cardnumber>" . $edit['details']['cc_number'] . "</cardnumber>";
655 $xml .="<cardnumber>" . $edit['cc_number'] . "</cardnumber>";
656 //$xml .="<cardexpmonth>" . $edit['expiry_date']['cc_month'] . "</cardexpmonth>";
657 $xml .="<cardexpmonth>" . $edit['cc_month'] . "</cardexpmonth>";
658 // linkpoint expecting two digit year,
659 // thus php substr to return 2 characters starting at positon 2 the 3rd characer
660 //$xml .="<cardexpyear>" . substr($edit['expiry_date']['cc_year'],2,2) . "</cardexpyear>";
661 $xml .="<cardexpyear>" . substr($edit['cc_year'],2,2) . "</cardexpyear>";
662 //$xml .="<cvmvalue>" . $edit['details']['cc_cvv'] . "</cvmvalue>";
663 $xml .="<cvmvalue>" . $edit['cc_ccv'] . "</cvmvalue>";
664 // not sure about cvmindicator, other implementations have $cvmindicator but from where?
665 $xml .="<cvmindicator>provided</cvmindicator>";
666 $xml .="</creditcard>";
667
668 $xml .="<payment>";
669 $xml .="<chargetotal>" . $storeOrder->gross . "</chargetotal>";
670 $xml .="</payment>";
671
672 $xml .="<transactiondetails>";
673 $xml .="<transactionorigin>ECI</transactionorigin>";
674 $xml .="<oid>" . $edit['txnid'] . "</oid>";
675 $xml .="<ip>" . $_SERVER['REMOTE_ADDR'] . "</ip>";
676 $xml .="</transactiondetails>";
677
678 $xml .="<notes>";
679 $xml .="<comments>" . $orderdescript . "</comments>";
680 $xml .="</notes>";
681
682
683 $xml .="</order>";
684
685
686 // Start CURL session
687 $ch = curl_init();
688 // set our options to be used by curl session, like the website and that we are posting
689 curl_setopt($ch, CURLOPT_URL, variable_get('ec_linkpoint_url', 'https://secure.linkpt.net:1129/LSGSXML'));
690 curl_setopt($ch, CURLOPT_POST, 1);
691 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
692 curl_setopt($ch, CURLOPT_SSLCERT, $d['cert']);
693 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
694 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
695 // not sure we need want this line?
696 // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // this line is used to fix errors on some systems
697 // Some other implementations include the CURLOPT_VERBOSE setting
698 // curl_setopt($ch, CURLOPT_VERBOSE, 0);
699 //
700 // now we have the curl session initiated and have set options,
701 // next we do the buffer to execute
702 $buffer = curl_exec($ch);
703 if (curl_errno($ch)) {
704 // oops the conection did not go correctly...
705 // use if/then to check debug setting to see how to report
706 if (variable_get('ec_linkpoint_debug', 0)) {
707 //debug set so we display error to screen
708 drupal_set_message(curl_error($ch),'error');
709 } else {
710 //debug not set, so let viewer know we could not connect, but do not dispaly why
711 drupal_set_message(t('Sorry - Could not connect to payment gateway.'),'error');
712 }
713 // we can also let the watchdog know about unable to connect to gateway
714 watchdog('ec_linkpoint', curl_error($ch), WATCHDOG_ERROR);
715 // return to next web page....
716 return str_replace('http://', 'https://', $base_url) . url('store/payment/ec_linkpoint/'. $edit['txnid']);
717 } else {
718 // hopefully we had a good connect to gateway so now we close curl connection
719 curl_close($ch);
720 }
721
722
723 // use php preg_match_all to perform a global regular expression match...
724 // preg_set_order tells it to order results to that $outara[0] is an array of the
725 // first set of matches, $outarra[1] is an array of the second set and so on...
726 preg_match_all ("/<(.*?)>(.*?)\</", $buffer, $outarr, PREG_SET_ORDER);
727
728 // loop through arrray to strip HTML and PHP tags from $outarr
729 // and create array $retarr with the LinkPoint information
730 $n = 0;
731 while (isset($outarr[$n])) {
732 $retarr[$outarr[$n][1]] = strip_tags($outarr[$n][0]);
733 $n++;
734 }
735
736
737 // We can take a moment here to create some strings
738 // like concatenating returns for approval,
739 $approval_text = "status: " . $retarr['r_approved'];
740 $approval_text .= "approval code: " . $retarr['r_code'];
741 $approval_text .= " " . $retarr['r_message'];
742
743
744 // our array $retarr has the information from LinkPoint
745 // now we can do a case switch to act upon the results
746 //
747 // for inside the array 'r_approved'
748 // if the value of r_aproved is exactly "APPROVED" then...
749 // we are approved so take appropriate action
750 //
751 // fyi, make sure we give them the approval code somewhere..... ????
752 //
753 switch ($retarr['r_approved']) {
754
755 case "APPROVED": // Credit card successfully processed (depends if SALE or AUTH for how)
756
757 // if we have debug enabled....
758 if ((!$retarr['r_ordernum']) && (!variable_get('ec_linkpoint_debug', 1))) {
759 drupal_set_message(t('No ordernumber received from Linkpoint'),'error');
760 }
761 $storeOrder->anid = $retarr['r_ordernum'];
762 $storeOrder->amount = $t->gross;
763 $storeOrder->payment_status = payment_get_status_id('completed');
764 $storeOrder->payment_method = 'ec_linkpoint';
765
766 // unset some credit card information like cc_number to prevent accidently saving elsewhere
767 // ?????????????????????????????????????????????
768 //unset($edit['details']['cc_ccv']);
769 //unset($edit['details']['cc_number']);
770
771 $is_new = (db_result(db_query('SELECT COUNT(txnid) FROM {ec_linkpoint_transaction} WHERE txnid = %d', $edit['txnid']))) ? false : true;
772
773 $txnid = store_transaction_save((array)$storeOrder);
774
775 if ($is_new && $txnid) {
776 // Compose and send confirmation email to the user
777 store_send_invoice_email($txnid);
778 }
779
780 // note if we are PREAUTH it was approved, but if we are SALE, it was charged.
781 if (variable_get('ec_linkpoint_ordertype', '')=="SALE") {
782 drupal_set_message("Credit card was charged: $approval_text <br> You will also receive confirmation by email.");
783 } else {
784 drupal_set_message("Credit card was approved: $approval_text <br> You will also receive confirmation by email.");
785 }
786
787 $url=trim(variable_get('ec_linkpoint_success_url', ''));
788 if ($url) {
789 // We want to go to a http, not https.
790 return str_replace('https://', 'http://', $base_url). '/' . $url;
791 } else {
792 return str_replace('https://', 'http://', $base_url). '/store/transaction/view/' . $edit['txnid'];
793 }
794 break;
795
796
797
798
799 // for this case switch, default, the above was not matched, r_approved did not have "APPROVED"
800 // so then we failed... so default is card not chared, but why?
801 // the approach is to do another case switch for default to handle the not approved situation....
802 default: // Credit card error: card was not charged.
803
804 // this case switch sets $error to be displayed for customer from earlier array saved when declined
805 // using the varialbe set for How to format error message if card is not accepted
806 // from beginning of this module:
807 // define('LINKPOINT_ERROR_SHORT_VIEW', 0);
808 // define('LINKPOINT_ERROR_MEDIUM_VIEW', 1);
809 // define('LINKPOINT_ERROR_LARGE_VIEW', 2);
810 //
811 switch (variable_get('ec_linkpoint_format_not_accepted',0)) {
812 case 0:
813 // short view of returned error
814 // instead of APPROVED, something else is sent for this value by linkpoint
815 $error=$retarr['r_approved'];
816 break;
817 case 1:
818 // medium view of returned error
819 // use preg_replace blank for part of r_error to shortn ?
820 $error=preg_replace('/^(.*?: )/','',$retarr['r_error']);
821 break;
822 case 2:
823 // large view of returned error
824 $error=$retarr['r_error'];
825 break;
826 }
827
828 // If we have an error message include it.. else generic decline message
829 if (variable_get('ec_linkpoint_debug', 0)) {
830 //debug set so we display error and xml to screen
831 drupal_set_message('Debug, Your card was not billed. The following error was received:<br>' . $error . '<br>The following xml for gateway was used:<br>' . $xml,'error');
832 } elseif ((is_string($error)) && ($error != '')){
833 drupal_set_message('Your card was not billed. The following error was received:<br>' . $error, 'error');
834 } else {
835 drupal_set_message('Your card was not billed. Apologies but the transaction did not complete.', 'error');
836 }
837
838
839 // Log the error also to watchdog?
840 // note ??? the userlink in log does not work.... to be fixed? seems same as authorize.net contrib ?
841 watchdog('ec_linkpoint', t('Linkpoint payment error for transaction %txnid for user %user_link, response: %lverror', array('%txnid' => $edit['txnid'], '%user_link' => l($storeOrder->uid, 'user/'. $storeOrder->uid), '%lverror' => $retarr['r_error']),WATCHDOG_ERROR, l(t('view'), 'admin/store/transaction')));
842
843 return str_replace('http://', 'https://', $base_url) . url('/store/payment/ec_linkpoint/'. $edit['txnid']);
844 break;
845 }
846 }
847
848
849 // eof
850 ?>

  ViewVC Help
Powered by ViewVC 1.1.2