-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommerce_elavon.module
More file actions
804 lines (698 loc) · 28 KB
/
commerce_elavon.module
File metadata and controls
804 lines (698 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
<?php
/**
* @file
* Implements Elavon payment services for use in Drupal Commerce.
*/
// Elavon transaction mode definitions:
define('ELAVON_TXN_MODE_PRODUCTION', 'production');
define('ELAVON_TXN_MODE_TEST', 'test');
define('ELAVON_TXN_TYPE_DIRECT_CAPTURE', 'direct_capture');
/**
* Implements hook_menu().
*/
function commerce_elavon_menu() {
$items = array();
// Add a menu item for capturing authorizations.
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/elavon-capture'] = array(
'title' => 'Capture',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_elavon_capture_form', 3, 5),
'access callback' => 'commerce_elavon_capture_access',
'access arguments' => array(5),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_elavon.admin.inc',
);
// Add a menu item for refunding payments.
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/elavon-direct-refund'] = array(
'title' => 'Refund',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_elavon_direct_refund_form', 3, 5),
'access callback' => 'commerce_elavon_direct_refund_access',
'access arguments' => array(5),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_elavon.admin.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/elavon-direct-delete'] = array(
'title' => 'Cancel transaction',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_elavon_direct_delete_form', 3, 5),
'access callback' => 'commerce_elavon_direct_delete_access',
'access arguments' => array(5),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_elavon.admin.inc',
);
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/elavon-direct-void'] = array(
'title' => 'Void',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_elavon_direct_void_form', 3, 5),
'access callback' => 'commerce_elavon_direct_void_access',
'access arguments' => array(5),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
'file' => 'includes/commerce_elavon.admin.inc',
);
return $items;
}
/**
* Determines access to the capture form for Elavon direct credit card
* transactions.
*
* @param $transaction
* The payment transaction object to be captured.
*
* @return bool
* TRUE or FALSE indicating refund access.
*/
function commerce_elavon_capture_access($transaction) {
if ($transaction->payment_method != 'elavon_direct' || empty($transaction->remote_id) || $transaction->status != COMMERCE_PAYMENT_STATUS_PENDING) {
return FALSE;
}
// Return FALSE if it is more than 10 days past the original authorization.
if (time() - $transaction->created > 86400 * 10) {
return FALSE;
}
// Allow access if the user can update payments on this transaction.
return commerce_payment_transaction_access('update', $transaction);
}
/**
* Determines access to the refund form for Elavon direct credit card
* transactions.
*
* @param $transaction
* The payment transaction object to be captured.
*
* @return bool
* TRUE or FALSE indicating refund access.
*/
function commerce_elavon_direct_refund_access($transaction) {
if ($transaction->payment_method != 'elavon_direct' || empty($transaction->remote_id) || $transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
return FALSE;
}
// Test if it's a refund.
if (strpos($transaction->amount, '-') === 0) {
return FALSE;
}
// Allow access if the user can update payments on this transaction.
return commerce_payment_transaction_access('update', $transaction);
}
/**
* Determines access to the ccdelete form for Elavon direct credit card
* transactions.
*
* @param $transaction
* The payment transaction object to be captured.
*
* @return bool
* TRUE or FALSE indicating delete access.
*/
function commerce_elavon_direct_delete_access($transaction) {
if ($transaction->payment_method != 'elavon_direct' || empty($transaction->remote_id) || $transaction->status != COMMERCE_PAYMENT_STATUS_PENDING) {
return FALSE;
}
// Allow access if the user can update payments on this transaction.
return commerce_payment_transaction_access('update', $transaction);
}
/**
* Determines access to the void form for Elavon direct credit card
* transactions.
*
* @param $transaction
* The payment transaction object to be captured.
*
* @return bool
* TRUE or FALSE indicating delete access.
*/
function commerce_elavon_direct_void_access($transaction) {
if ($transaction->payment_method != 'elavon_direct' || empty($transaction->remote_id) || $transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
return FALSE;
}
// The void seems to be possible during a short amount of time, before the
// batch is settled, this usually means less than a day.
if (time() - $transaction->changed > 86400) {
return FALSE;
}
// Allow access if the user can update payments on this transaction.
return commerce_payment_transaction_access('update', $transaction);
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_elavon_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['elavon_direct'] = array(
'base' => 'commerce_elavon_direct',
'title' => t('Elavon direct processing'),
'short_title' => t('Elavon direct processing'),
'display_title' => t('Credit card'),
'description' => t('Integrates Elavon direct processing payment, direct or delayed capture '),
'cardonfile' => array(
'charge callback' => 'commerce_elavon_direct_cardonfile_charge',
'update callback' => 'commerce_elavon_direct_cardonfile_update',
'delete callback' => 'commerce_elavon_direct_cardonfile_delete',
),
);
return $payment_methods;
}
/**
* Returns the default settings for the Elavon AIM payment method.
*/
function commerce_elavon_default_settings() {
return array(
'accountid' => '',
'userid' => '',
'pin' => '',
'txn_mode' => ELAVON_TXN_MODE_TEST,
'txn_type' => COMMERCE_CREDIT_AUTH_CAPTURE,
'log' => array('request' => '0', 'response' => '0'),
'cardonfile' => FALSE,
'multicurrency' => FALSE,
);
}
/**
* Payment method callback: settings form.
*/
function commerce_elavon_direct_settings_form($settings = NULL) {
// Merge default settings into the stored settings array.
$settings = (array) $settings + commerce_elavon_default_settings();
$form = commerce_elavon_base_settings_form($settings);
return $form;
}
/**
* Build common form for both payment method.
*/
function commerce_elavon_base_settings_form($settings) {
// Merge default settings into the stored settings array.
$form = array();
$form['accountid'] = array(
'#type' => 'textfield',
'#title' => t('Elavon Account ID'),
'#description' => t('Your Elavon Account ID'),
'#default_value' => $settings['accountid'],
'#required' => TRUE,
'#weight' => -5,
);
$form['userid'] = array(
'#type' => 'textfield',
'#title' => t('Elavon user ID'),
'#description' => t('Your Elavon user id dedicated for web based transaction.'),
'#default_value' => $settings['userid'],
'#required' => TRUE,
'#weight' => -4,
);
$form['pin'] = array(
'#type' => 'textfield',
'#title' => t('Elavon Pin'),
'#description' => t('Your Elavon Pin'),
'#default_value' => $settings['pin'],
'#required' => TRUE,
'#weight' => -3,
);
$form['txn_mode'] = array(
'#type' => 'radios',
'#title' => t('Transaction mode'),
'#description' => t('Adjust to live transactions when you are ready to start processing real payments.'),
'#options' => array(
ELAVON_TXN_MODE_PRODUCTION => t('Live transactions in a production account'),
ELAVON_TXN_MODE_TEST => t('Test transactions with your account'),
),
'#default_value' => $settings['txn_mode'],
);
$form['txn_type'] = array(
'#type' => 'radios',
'#title' => t('Default credit card transaction type'),
'#description' => t('The default will be used to process transactions during checkout.'),
'#options' => array(
COMMERCE_CREDIT_AUTH_CAPTURE => t('Authorization and capture'),
COMMERCE_CREDIT_AUTH_ONLY => t('Authorization only (requires manual or automated capture after checkout)'),
),
'#default_value' => $settings['txn_type'],
);
$form['log'] = array(
'#type' => 'checkboxes',
'#title' => t('Log the following messages for debugging. These messages are credit and transaction information. Keep it off for production site.'),
'#options' => array(
'request' => t('API request messages'),
'response' => t('API response messages'),
),
'#default_value' => $settings['log'],
);
$form['multicurrency'] = array(
'#type' => 'checkbox',
'#title' => t('Multi-Currency support'),
'#description' => t('Use only with a terminal that is setup with Multi-Currency.'),
'#default_value' => $settings['multicurrency'],
);
if (module_exists('commerce_cardonfile')) {
$form['cardonfile'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Card on File functionality.'),
'#description' => t('Stores tokenized value for card data.'),
'#default_value' => $settings['cardonfile'],
);
}
else {
$form['cardonfile'] = array(
'#type' => 'markup',
'#markup' => t('To enable Card on File functionality download and install the Card on File module.'),
);
}
return $form;
}
/**
* Payment method callback: checkout form - Direct.
*/
function commerce_elavon_direct_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
return commerce_payment_credit_card_form(array('code' => ''));
}
/**
* Payment method callback: checkout form validation - Direct.
*/
function commerce_elavon_direct_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
// If the customer specified a card on file, skip the normal validation.
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) &&
!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
return;
}
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
// Validate the credit card fields.
$settings = array(
'form_parents' => array_merge($form_parents, array('credit_card')),
);
if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) {
return FALSE;
}
}
/**
* Payment method callback: checkout form submission - Direct.
*/
function commerce_elavon_direct_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
// If the customer specified payment using a card on file, attempt that now
// and simply return the result.
if (module_exists('commerce_cardonfile') && $payment_method['settings']['cardonfile'] &&
!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
// First attempt to load the card on file.
$card_data = commerce_cardonfile_load($pane_values['cardonfile']);
// Fail now if it is no longer available or the card is inactive.
if (empty($card_data) || $card_data->status == 0) {
drupal_set_message(t('The requested card on file is no longer valid.'), 'error');
return FALSE;
}
return commerce_elavon_direct_cardonfile_charge($payment_method, $card_data, $order, $charge);
}
$info = array(
'ssl_card_number' => $pane_values['credit_card']['number'],
'ssl_exp_date' => $pane_values['credit_card']['exp_month'] . substr($pane_values['credit_card']['exp_year'], 2, 2),
'card_exp_month' => $pane_values['credit_card']['exp_month'],
'card_exp_year' => $pane_values['credit_card']['exp_year'],
);
if (isset($pane_values['credit_card']['code'])) {
$info += array(
'ssl_cvv2cvc2' => $pane_values['credit_card']['code'],
'ssl_cvv2cvc2_indicator' => 1,
);
}
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) &&
!empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
// Tell Elavon we want to generate a token.
$info += array(
'ssl_get_token' => 'Y',
'ssl_add_token' => 'Y',
'cardonfile_instance_default' => $pane_values['cardonfile_instance_default'],
);
}
return commerce_elavon_transaction_process($payment_method, $info, $order, $charge);
}
/**
* Card on file callback: background charge payment.
*
* @param object $payment_method
* The payment method instance definition array.
* @param $card_data
* Card on File entity.
* @param $order
* The order object that is being processed.
* @param array $charge
* The price array for the charge amount with keys of 'amount' and 'currency'
* If null the total value of the order is used.
*
* @return FALSE if the transaction was not successful, TRUE otherwise.
*/
function commerce_elavon_direct_cardonfile_charge($payment_method, $card_data, $order, $charge = NULL) {
// Format order total for the transaction.
if (!isset($charge)) {
$charge = commerce_payment_order_balance($order);
}
$info = array(
'ssl_token' => $card_data->remote_id,
'ssl_exp_date' => str_pad($card_data->card_exp_month, 2, '0', STR_PAD_LEFT) . substr($card_data->card_exp_year, 2, 2),
);
return commerce_elavon_transaction_process($payment_method, $info, $order, $charge);
}
/**
* Card on file callback: updated the card stored in the add to card manager.
*/
function commerce_elavon_direct_cardonfile_update($form, &$form_state, $payment_method, $card_data) {
$info = array(
'ssl_transaction_type' => 'ccupdatetoken',
'ssl_token' => $card_data->remote_id,
'ssl_exp_date' => str_pad($card_data->card_exp_month, 2, '0', STR_PAD_LEFT) . substr($card_data->card_exp_year, 2, 2),
);
list($first_name, $last_name) = explode(' ', $card_data->card_name, 2);
$info += array(
'ssl_first_name' => $first_name,
'ssl_last_name' => $last_name,
);
$response = commerce_elavon_request($payment_method, $info);
if (isset($response['xml']->ssl_token_response)) {
return ((string) $response['xml']->ssl_token_response === 'SUCCESS');
}
return FALSE;
}
/**
* Card on file callback: deletes the card stored in the add to card manager.
*/
function commerce_elavon_direct_cardonfile_delete($form, &$form_state, $payment_method, $card_data) {
$info = array(
'ssl_transaction_type' => 'ccdeletetoken',
'ssl_token' => $card_data->remote_id,
);
$response = commerce_elavon_request($payment_method, $info);
if (isset($response['xml']->ssl_token_response)) {
return ((string) $response['xml']->ssl_token_response === 'SUCCESS');
}
return FALSE;
}
/**
* Proceed to the payment and record a transaction.
*/
function commerce_elavon_transaction_process($payment_method, $info, $order, $charge) {
// Set Tax amount
$sales_tax_total = 0;
// Loop through and get all tax elements and add them up.
$order_working_array = $order->commerce_order_total[LANGUAGE_NONE][0]['data']['components'];
foreach ($order_working_array as $id => $order_working_array_element) {
if (strpos($order_working_array_element['name'], 'tax') !== FALSE) {
$sales_tax_total += $order_working_array_element['price']['amount'];
}
}
$ssl_salestax = number_format(commerce_currency_amount_to_decimal($sales_tax_total, $charge['currency_code']), 2, '.', '');
$auth_only = $payment_method['settings']['txn_type'] === COMMERCE_CREDIT_AUTH_ONLY;
// Prepare the billing information.
$billing_data = commerce_elavon_get_billing_info($order);
// Build a name-value pair array for this transaction.
$info += array(
'ssl_amount' => number_format(commerce_currency_amount_to_decimal($charge['amount'], $charge['currency_code']), 2, '.', ''),
'ssl_avs_address' => $billing_data['street'],
'ssl_address2' => $billing_data['street2'],
'ssl_city' => $billing_data['city'],
'ssl_state' => $billing_data['state'],
'ssl_avs_zip' => $billing_data['zip'],
'ssl_first_name' => $billing_data['first_name'],
'ssl_last_name' => $billing_data['last_name'],
'ssl_salestax' => $ssl_salestax,
'ssl_invoice_number' => $order->order_number,
);
if (!empty($payment_method['settings']['multicurrency'])) {
$info['ssl_transaction_currency'] = $charge['currency_code'];
}
if ($auth_only) {
$info['ssl_transaction_type'] = 'ccauthonly';
}
// Submit the request to Elavon.
$response = commerce_elavon_request($payment_method, $info);
// Store the card.
if ($response['status'] && isset($info['ssl_get_token']) && isset($response['xml']->ssl_token_response)) {
$token_response = ((string) $response['xml']->ssl_token_response === 'SUCCESS') ? TRUE : FALSE;
if ($token_response) {
$card_data = commerce_cardonfile_new();
$card_data->uid = $order->uid;
$card_data->payment_method = $payment_method['method_id'];
$card_data->instance_id = $payment_method['instance_id'];
$card_data->remote_id = (string) $response['xml']->ssl_token;
$card_data->card_type = !empty($card_type) ? $card_type : 'card';
$card_data->card_name = !empty($billing_data['name_on_card']) ? $billing_data['name_on_card'] : '';
$card_data->card_number = substr($info['ssl_card_number'], -4);
$card_data->card_exp_month = $info['card_exp_month'];
$card_data->card_exp_year = $info['card_exp_year'];
$card_data->instance_default = $info['cardonfile_instance_default'];
$card_data->status = 1;
// Save and log the creation of the new card on file.
commerce_cardonfile_save($card_data);
}
}
// Prepare a transaction object to log the API response.
$transaction = commerce_payment_transaction_new('elavon_direct', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->remote_id = isset($response['xml']->ssl_txn_id) ? (string) $response['xml']->ssl_txn_id : '';
$transaction->amount = $charge['amount'];
$transaction->currency_code = $charge['currency_code'];
$transaction->payload[REQUEST_TIME] = isset($response['xml']) ? $response['xml']->asXML() : '';
// Store the Message of transaction in the remote status.
$transaction->remote_status = $response['msg'];
$transaction->message = implode('<br />', commerce_elavon_get_log_message($response, ELAVON_TXN_TYPE_DIRECT_CAPTURE));
// Set the transaction status based on the type of transaction this was.
if ($response['status']) {
$transaction->status = $auth_only ? COMMERCE_PAYMENT_STATUS_PENDING : COMMERCE_PAYMENT_STATUS_SUCCESS;
}
else {
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
}
// Save the transaction information.
commerce_payment_transaction_save($transaction);
// If the payment failed, display an error and rebuild the form.
if (!$response['status']) {
drupal_set_message(t('We received the following error processing your card. Please enter you information again or try a different card.'), 'error');
if (!empty($response['msg'])) {
drupal_set_message(check_plain($response['msg']), 'error');
}
return FALSE;
}
return TRUE;
}
/**
* Build log message.
*/
function commerce_elavon_get_log_message($response, $type) {
// Build a meaningful response message.
$status = !$response['status'] ? t('@type : REJECTED', array('@type' => $type)) : t('@type : ACCEPTED', array('@type' => $type)) . ': ' . check_plain($response['msg']);
$avs = !empty($response['xml']->ssl_avs_response) ? (string) $response['xml']->ssl_avs_response : FALSE;
$cvv = !empty($response['xml']->ssl_cvv2_response) ? (string) $response['xml']->ssl_cvv2_response : FALSE;
$message = array(
$status,
$avs ? t('AVS response: @avs', array('@avs' => commerce_elavon_avs_response($avs))) : '',
$cvv ? t('CVV match: @cvv', array('@cvv' => commerce_elavon_cvv_response($cvv))) : '',
);
return $message;
}
/**
* Prepare the billing information.
*/
function commerce_elavon_get_billing_info($order) {
$billing_data = array(
'street' => '',
'street2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'name_on_card' => '',
'first_name' => '',
'last_name' => '',
);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
if ($order_wrapper->commerce_customer_billing->value()) {
$billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
if (empty($billing_address['first_name'])) {
$name_parts = explode(' ', $billing_address['name_line'], 2);
$billing_address['first_name'] = $name_parts[0];
$billing_address['last_name'] = $name_parts[1];
}
$billing_data['street'] = substr($billing_address['thoroughfare'], 0, 30);
$billing_data['street2'] = substr($billing_address['premise'], 0, 30);
$billing_data['city'] = substr($billing_address['locality'], 0, 30);
$billing_data['state'] = substr($billing_address['administrative_area'], 0, 30);
$billing_data['zip'] = substr($billing_address['postal_code'], 0, 9);
$billing_data['country'] = $billing_address['country'];
$billing_data['first_name'] = $billing_address['first_name'];
$billing_data['last_name'] = $billing_address['last_name'];
$billing_data['name_on_card'] = substr($billing_address['first_name'], 0, 50) . ' ' . substr($billing_address['last_name'], 0, 50);
}
return $billing_data;
}
/**
* Returns the URL to the Elavon server determined by transaction mode.
*
* @param $txn_mode
* The transaction mode that relates to the production or test server.
*
* @return
* The URL to use to submit requests to the Elavon server.
*/
function commerce_elavon_server_url($txn_mode) {
switch ($txn_mode) {
case ELAVON_TXN_MODE_PRODUCTION:
return 'https://www.myvirtualmerchant.com/VirtualMerchant/processxml.do';
case ELAVON_TXN_MODE_TEST:
return 'https://demo.myvirtualmerchant.com/VirtualMerchantDemo/processxml.do';
}
}
/**
* Submits a request to Elavon.
*
* @param $payment_method
* The payment method instance array associated with this API request.
*/
function commerce_elavon_request($payment_method, $info = array()) {
// Get the API endpoint URL for the method's transaction mode and type.
$url = commerce_elavon_server_url($payment_method['settings']['txn_mode']);
// Add the default name-value pairs to the array.
$info += array(
'ssl_merchant_id' => $payment_method['settings']['accountid'],
'ssl_user_id' => $payment_method['settings']['userid'],
'ssl_pin' => $payment_method['settings']['pin'],
'ssl_show_form' => 'false',
'ssl_test_mode' => 'false',
'ssl_transaction_type' => 'ccsale',
);
// Allow modules to alter parameters of the API request.
drupal_alter('commerce_elavon_direct_request', $info);
// Log the request if specified.
if ($payment_method['settings']['log']['request'] === 'request') {
// Mask the credit card number and CVV.
$log_nvp = $info;
$log_nvp['ssl_merchant_id'] = str_repeat('X', strlen($log_nvp['ssl_merchant_id']));
$log_nvp['ssl_user_id'] = str_repeat('X', strlen($log_nvp['ssl_user_id']));
$log_nvp['ssl_pin'] = str_repeat('X', strlen($log_nvp['ssl_pin']));
if (!empty($log_nvp['ssl_card_number'])) {
$log_nvp['ssl_card_number'] = str_repeat('X', strlen($log_nvp['ssl_card_number']) - 4) . substr($log_nvp['ssl_card_number'], -4);
}
if (!empty($log_nvp['ssl_cvv2cvc2'])) {
$log_nvp['ssl_cvv2cvc2'] = str_repeat('X', strlen($log_nvp['ssl_cvv2cvc2']));
}
watchdog('commerce_elavon', 'Elavon request to @url: !param', array('@url' => $url, '!param' => '<pre>' . check_plain(print_r($log_nvp, TRUE)) . '</pre>'), WATCHDOG_DEBUG);
}
// Prepare xml for Elavon.
$xmldata = 'xmldata=<txn>';
foreach ($info as $key => $value) {
// Keep keys starting by ssl_.
if (strpos($key, 'ssl_') !== 0) {
continue;
}
$sanitized_key = check_plain($key);
$xmldata .= '<' . $sanitized_key . '>' . check_plain($value) . '</' . $sanitized_key . '>';
}
$xmldata .= '</txn>';
// Setup the cURL request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$result = curl_exec($ch);
// Log any errors to the watchdog.
if ($error = curl_error($ch)) {
watchdog('commerce_elavon', 'cURL error: @error', array('@error' => $error), WATCHDOG_ERROR);
$response['status'] = FALSE;
$response['msg'] = $error;
return $response;
}
curl_close($ch);
// If we received data back from the server.
if (empty($result)) {
watchdog('commerce_elavon', 'cURL error empty result returned.', array(), WATCHDOG_ERROR);
$response['status'] = FALSE;
$response['msg'] = t('No answer from server');
}
else {
// Extract the result into an XML response object.
$xml = new SimpleXMLElement($result);
$response = array();
// Log the API response if specified.
if ($payment_method['settings']['log']['response'] === 'response') {
watchdog('commerce_elavon', 'API response received:<pre>@xml</pre>', array('@xml' => $xml->asXML()));
}
$response['status'] = ((string) $xml->ssl_result_message === 'APPROVAL') ? TRUE : FALSE;
$response['msg'] = (string) $xml->ssl_result_message;
// Request approved, Save original xml response containing all the data.
$response['xml'] = $xml;
}
return $response;
}
/**
* Returns the message text for an AVS response code.
*/
function commerce_elavon_avs_response($code) {
switch ($code) {
case 'A':
return t('Address matches - ZIP Code does not');
case 'B':
return t('Street address match, Postal code in wrong format (international issuer)');
case 'C':
return t('Street address and postal code in wrong formats');
case 'D':
return t('Street address and postal code match (international issuer)');
case 'E':
return t('AVS Error');
case 'F':
return t('Address does compare and five-digit ZIP code does compare (UK only)');
case 'G':
return t('Service not supported by non-US issuer');
case 'I':
return t('Address information not verified by international issuer');
case 'M':
return t('Street Address and Postal code match (international issuer)');
case 'N':
return t('No Match on Address (Street) or ZIP');
case 'O':
return t('No Response sent');
case 'P':
return t('Postal codes match, Street address not verified due to incompatible formats');
case 'R':
return t('Retry, System unavailable or Timed out');
case 'S':
return t('Service not supported by issuer');
case 'U':
return t('Address information is unavailable');
case 'W':
return t('9-digit ZIP matches, Address (Street) does not');
case 'X':
return t('Exact AVS Match');
case 'Y':
return t('Address (Street) and 5-digit ZIP match');
case 'Z':
return t('5-digit ZIP matches, Address (Street) does not');
default:
return '-';
}
}
/**
* Returns the message text for a CVV match.
*/
function commerce_elavon_cvv_response($code) {
switch ($code) {
case 'M':
return t('CVV2/CVC2 Match');
case 'N':
return t('CVV2/CVC2 No match');
case 'P':
return t('Not Processed');
case 'S':
return t('Issuer indicates that the CVV2/CVC2 data should be present on the card, but the merchant has indicated that the CVV2/CVC2 data is not resent on the card');
case 'U':
return t('Issuer has not certified for CVV2/CVC2 or Issuer has not provided Visa with the CVV2/CVC2 encryption keys');
default:
return '-';
}
}