-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_asciitb.module
711 lines (617 loc) · 21.3 KB
/
commerce_asciitb.module
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
<?php
/**
* @file
* Implements ASC-IITB payment services for use with Drupal Commerce.
*/
// Local payment transaction status definitions:
define('COMMERCE_PAYMENT_Recon_STATUS_Rejected', 'rejected');
define('COMMERCE_PAYMENT_Recon_STATUS_Reconciled', 'reconciled');
/**
* Implements hook_menu().
*/
function commerce_asciitb_menu() {
$items = array();
// Define an always accessible path to receive IPNs.
$items['commerce_asciitb/ipn'] = array(
'page callback' => 'commerce_asciitb_process_ipn',
'page arguments' => array(),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
// Define a payment method specific path
$items['commerce_asciitb/ipn/simp'] = array(
'page callback' => 'commerce_asciitb_process_ipn_pm',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
// Define an additional IPN path that is payment method / instance specific.
$items['commerce_asciitb/ipn/%commerce_payment_method_instance'] = array(
'page callback' => 'commerce_asciitb_process_ipn',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Returns the IPN URL.
*
* @param $method_id
* Optionally specify a payment method instance ID to include in the URL.
*/
function commerce_asciitb_ipn_url($instance_id = NULL) {
$parts = array(
'commerce_asciitb',
'ipn',
);
if (!empty($instance_id)) {
$parts[] = $instance_id;
}
return url(implode('/', $parts), array('absolute' => TRUE));
}
/**
* Forward the incoming IPN data to the method specific processor
*
* @param $payment_method_short
* Short label for the payment method name
*
* @return
* TRUE or FALSE indicating whether the IPN was successfully processed or not.
*/
function commerce_asciitb_process_ipn_pm($pmshort) {
// Since ASC does not use a notify_url the default method process_ipn
// cannot be used. The payment method instance is loaded here instead.
switch ($pmshort) {
case 'simp':
$payment_method = commerce_payment_method_instance_load('asciitb_simp|commerce_payment_asciitb_simp');
}
commerce_asciitb_process_ipn($payment_method);
//$redirect_url = commerce_asciitb_process_ipn($payment_method);
//drupal_goto($redirect_url);
}
/**
* Processes an incoming IPN.
*
* @param $payment_method
* The payment method instance array that originally made the payment.
* @param $debug_ipn
* Optionally specify an IPN array for debug purposes; if left empty, the IPN
* be pulled from the $_GET. If an IPN is passed in, validation of the IPN
* at ASC-IITB will be bypassed.
*
* @return
* TRUE or FALSE indicating whether the IPN was successfully processed or not.
*/
function commerce_asciitb_process_ipn($payment_method = NULL, $debug_ipn = array()) {
// Retrieve the IPN from $_GET if the caller did not supply an IPN array.
// Note that Drupal has already run stripslashes() on the contents of the
// $_GET array at this point, so we don't need to worry about them.
if (empty($debug_ipn)) {
//$ipn = $_POST;
$msg = $_GET['sMsg'];
//echo '<pre>';
//print_r($_GET);
parse_str($msg,$ipn);
// Exit now if the $_GET was empty.
if (empty($ipn)) {
watchdog('commerce_asciitb', 'IPN URL accessed with no GET data submitted.', array(), WATCHDOG_WARNING);
return FALSE;
}
else {
watchdog('commerce_asciitb', 'IPN URL processed from GET data submitted.', array(), WATCHDOG_NOTICE);
}
// this is missed out otherwise
$ipn['q'] = $_GET['q'];
// some defaults
$ipn['mc_currency'] = 'INR';
//echo 'IPN <pre>'; print_r($ipn); echo '</pre>';
#$ipn = drupal_get_query_parameters($Msg);
/*
echo 'loaded instance <br/>';
echo '<pre>';
print_r ($payment_method);
echo '</pre>';
*/
#$pm = commerce_payment_method_instance_load('asciitb_simp|commerce_payment_asciitb_simp');
//echo 'loaded instance <br/>';
//print_r ($pm);
/*
echo 'All methods <br/>';
$pms = commerce_payment_methods();
print_r ($pms);
*/
/* ASC does not validate IPN
// Prepare an array to POST back to ASC-IITB to validate the IPN.
$variables = array('cmd=_notify-validate');
foreach ($ipn as $key => $value) {
$variables[] = $key . '=' . urlencode($value);
}
// Determine the proper ASC-IITB server to POST to.
if (!empty($ipn['test_ipn']) && $ipn['test_ipn'] == 1) {
$host = 'https://www.sandbox.asciitb.com/cgi-bin/webscr';
}
else {
$host = 'https://www.asciitb.com/cgi-bin/webscr';
}
// Process the HTTP request to validate the IPN.
$response = drupal_http_request($host, array('method' => 'GET', 'data' => implode('&', $variables)));
// If an error occurred during processing, log the message and exit.
if (property_exists($response, 'error')) {
watchdog('commerce_asciitb', 'Attempt to validate IPN failed with error @code: @error', array('@code' => $response->code, '@error' => $response->error), WATCHDOG_ERROR);
return FALSE;
}
// If the IPN was invalid, log a message and exit.
if ($response->data == 'INVALID') {
watchdog('commerce_asciitb', 'Invalid IPN received and ignored.', array(), WATCHDOG_ALERT);
return FALSE;
}
*/
}
else {
$ipn = $debug_ipn;
}
// If the payment method specifies full IPN logging, do it now.
if (!empty($payment_method['settings']['ipn_logging']) &&
$payment_method['settings']['ipn_logging'] == 'full_ipn') {
if (!empty($ipn['transId'])) {
watchdog('commerce_asciitb', 'Attempting to process IPN @txn_id. !ipn_log', array('@txn_id' => $ipn['transId'], '!ipn_log' => '<pre>' . check_plain(print_r($ipn, TRUE)) . '</pre>'), WATCHDOG_NOTICE);
}
else {
watchdog('commerce_asciitb', 'Attempting to process an IPN. !ipn_log', array('!ipn_log' => '<pre>' . check_plain(print_r($ipn, TRUE)) . '</pre>'), WATCHDOG_NOTICE);
}
}
// Exit if the IPN has already been processed. TODO: find if transID is uniqueto ASC OP
/*
if (!empty($ipn['transId']) && $prior_ipn = commerce_asciitb_ipn_load($ipn['transId'])) {
echo '<pre>'; echo 'IPN process check' ; echo '</pre>';
if ($prior_ipn['status'] == $ipn['status']) {
echo '<pre>'; echo 'IPN already processed' ; echo '</pre>';
watchdog('commerce_asciitb', 'Attempted to process an IPN that has already been processed with transaction ID @txn_id.', array('@txn_id' => $ipn['transId']), WATCHDOG_NOTICE);
return FALSE;
}
}
*/
// Load the order based on the IPN's invoice number (reqId).
if (!empty($ipn['reqId']) && strpos($ipn['reqId'], '-') !== FALSE) {
list($ipn['order_id'], $timestamp) = explode('-', $ipn['reqId']);
}
elseif (!empty($ipn['reqId'])) {
$ipn['order_id'] = $ipn['reqId'];
}
else {
$ipn['order_id'] = 0;
$timestamp = 0;
}
if (!empty($ipn['order_id'])) {
$order = commerce_order_load($ipn['order_id']);
}
else {
$order = FALSE;
}
/*
echo 'loaded order <br/>';
echo "<br/> Order status 01 = $order->status <br/>";
//echo '<pre>';
//print_r ($order);
//echo '</pre>';
*/
/* No applicable to ASC
// Give the payment method module an opportunity to validate the receiver
// e-mail address and amount of the payment if possible. If a validate
// function exists, it is responsible for setting its own watchdog message.
if (!empty($payment_method)) {
$callback = $payment_method['base'] . '_asciitb_ipn_validate';
// If a validator function existed...
if (function_exists($callback)) {
// Only exit if the function explicitly returns FALSE.
if ($callback($order, $payment_method, $ipn) === FALSE) {
return FALSE;
}
}
}
*/
// Give the payment method module an opportunity to process the IPN.
if (!empty($payment_method)) {
$callback = $payment_method['base'] . '_asciitb_ipn_process';
//echo '<pre>'; echo 'IPN process callback ' ; echo '</pre>';
// If a processing function exists...
if (function_exists($callback)) {
// Skip saving if the function explicitly returns FALSE, meaning the IPN
// wasn't actually processed.
if ($callback($order, $payment_method, $ipn) !== FALSE) {
// Save the processed IPN details.
commerce_asciitb_ipn_save($ipn);
}
}
}
//echo "<br/> Order status 02 = $order->status <br/>";
// Invoke the hook here so implementations have access to the order and
// payment method if available and a saved IPN array that includes the payment
// transaction ID if created in the payment method's default process callback.
module_invoke_all('commerce_asciitb_ipn_process', $order, $payment_method, $ipn);
//echo "<br/> Order status 03 = $order->status <br/>";
// Do the final redirection
if( $ipn['requestType'] == 'I') { #Immediate response
switch ($ipn['status']) {
case 'F':
// Return to the previous page when payment is canceled
$return_path = url('checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array('absolute' => TRUE));
break;
case 'S':
// Return to the payment redirect page for processing successful payments
$return_path = '/checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'];
break;
}
//echo "<br/> <a href=" . $return_path .">return path</a>";
header('Location: ' . $return_path);
}
// else: no redirection for reconcile response
//echo "<br/>Cancel url: $cancel_return_url <br/>";
//drupal_goto($cancel_return_url);
//echo "<br/> $return_url";
//$return_path = '/checkout/' . $order->order_id ;
//echo 'ipn processed order <br/>';
/*
echo '<pre>';
print_r ($order);
echo '</pre>';
*/
//echo "<br/> <a href=" . $return_url .">return url</a>";
#drupal_goto('/user/1/orders');
#drupal_goto($return_path);
#$target_uri = commerce_checkout_order_uri($order);
#echo "<br/> <a href=" . $target_uri .">return url</a>";
#drupal_goto($target_uri);
/*
$path=$return_url;
$http_response_code = 302;
echo "<br/>path: $path";
$options=array();
//drupal_alter('drupal_goto', $path, array(), $http_response_code);
//drupal_alter('drupal_goto', $path, $options, $http_response_code);
$options['absolute'] = TRUE;
$url = url($path, $options);
echo "<br/>path post alter: $path";
echo "<br/>url: $url";
//header('Location: ' . $url, TRUE, $http_response_code);
//header('Location: ' . $url);
*/
#drupal_goto($return_path);
//drupal_exit($url);
//return $return_path;
//header('Location: ' . $return_path);
}
/**
* Loads a stored IPN by ID.
*
* @param $id
* The ID of the IPN to load.
* @param $type
* The type of ID you've specified, either the serial numeric ipn_id or the
* actual ASC-IITB transId. Defaults to transId.
*
* @return
* The original IPN with some meta data related to local processing.
*/
function commerce_asciitb_ipn_load($id, $type = 'txn_id') {
return db_select('commerce_asciitb_ipn', 'cpi')
->fields('cpi')
->condition('cpi.' . $type, $id)
->execute()
->fetchAssoc();
}
/**
* Saves an IPN with some meta data related to local processing.
*
* @param $ipn
* An IPN array with additional parameters for the order_id and Commerce
* Payment transaction_id associated with the IPN.
*
* @return
* The operation performed by drupal_write_record() on save; since the IPN is
* received by reference, it will also contain the serial numeric ipn_id
* used locally.
*/
function commerce_asciitb_ipn_save(&$ipn) {
if (!empty($ipn['ipn_id']) && commerce_asciitb_ipn_load($ipn['transId'])) {
$ipn['changed'] = REQUEST_TIME;
return drupal_write_record('commerce_asciitb_ipn', $ipn, 'ipn_id');
}
else {
$ipn['created'] = REQUEST_TIME;
$ipn['changed'] = REQUEST_TIME;
return drupal_write_record('commerce_asciitb_ipn', $ipn);
}
}
/**
* Deletes a stored IPN by ID.
*
* @param $id
* The ID of the IPN to delete.
* @param $type
* The type of ID you've specified, either the serial numeric ipn_id or the
* actual ASC-IITB transId. Defaults to transId.
*/
function commerce_asciitb_ipn_delete($id, $type = 'txn_id') {
db_delete('commerce_asciitb_ipn')
->condition($type, $id)
->execute();
}
/**
* Returns a unique invoice number (reqId) based on the Order ID and timestamp.
*/
function commerce_asciitb_ipn_invoice($order) {
return $order->order_id . '-' . REQUEST_TIME;
}
/**
* Returns an appropriate message given a pending reason.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_ipn_pending_reason($pending_reason) {
switch ($pending_reason) {
case 'address':
return t('The payment is pending because your customer did not include a confirmed shipping address and your Payment Receiving Preferences is set to allow you to manually accept or deny each of these payments.');
case 'authorization':
return t('You set the payment action to Authorization and have not yet captured funds.');
case 'echeck':
return t('The payment is pending because it was made by an eCheck that has not yet cleared.');
case 'intl':
return t('The payment is pending because you hold a non-U.S. account and do not have a withdrawal mechanism.');
case 'multi-currency':
return t('You do not have a balance in the currency sent, and you do not have your Payment Receiving Preferences set to automatically convert and accept this payment.');
case 'order':
return t('You set the payment action to Order and have not yet captured funds.');
case 'paymentreview':
return t('The payment is pending while it is being reviewed by ASC-IITB for risk.');
case 'unilateral':
return t('The payment is pending because it was made to an e-mail address that is not yet registered or confirmed.');
case 'upgrade':
return t('The payment is pending because it was either made via credit card and you do not have a Business or Premier account or you have reached the monthly limit for transactions on your account.');
case 'verify':
return t('The payment is pending because you are not yet verified.');
case 'other':
return t('The payment is pending for a reason other than those listed above. For more information, contact ASC-IITB Customer Service.');
}
}
*/
/**
* Loads the payment transaction matching the ASC-IITB transaction ID.
*
* @param $txn_id
* The ASC-IITB transaction ID to search for in the remote_id field.
*
* @return
* The loaded payment transaction.
*/
function commerce_asciitb_payment_transaction_load($txn_id) {
$transactions = commerce_payment_transaction_load_multiple(array(), array('remote_id' => $txn_id));
return $transactions ? reset($transactions) : FALSE;
}
/**
* Returns the relevant ASC-IITB payment action for a given transaction type.
*
* @param $txn_type
* The type of transaction whose payment action should be returned; currently
* supports COMMERCE_CREDIT_AUTH_CAPTURE and COMMERCE_CREDIT_AUTH_ONLY.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_payment_action($txn_type) {
switch ($txn_type) {
case COMMERCE_CREDIT_AUTH_ONLY:
return 'Authorization';
case COMMERCE_CREDIT_AUTH_CAPTURE:
return 'Sale';
}
}
*/
/**
* Returns the description of a transaction type for a ASC-IITB payment action.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_reverse_payment_action($payment_action) {
switch (strtoupper($payment_action)) {
case 'AUTHORIZATION':
return t('Authorization only');
case 'SALE':
return t('Authorization and capture');
}
}
*/
/**
* Returns an array of all possible currency codes for the different ASC-IITB
* payment methods.
*
* @param $method_id
* The ID of the ASC-IITB payment method whose currencies should be returned.
*
* @return
* An associative array of currency codes with keys and values being the
* currency codes accepted by the specified ASC-IITB payment method.
*/
function commerce_asciitb_currencies($method_id) {
switch ($method_id) {
case 'asciitb_simp':
return drupal_map_assoc(array('INR', 'AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'USD'));
}
}
/**
* Returns an appropriate message given an AVS code.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_avs_code_message($code) {
if (is_numeric($code)) {
switch ($code) {
case '0':
return t('All the address information matched.');
case '1':
return t('None of the address information matched; transaction declined.');
case '2':
return t('Part of the address information matched.');
case '3':
return t('The merchant did not provide AVS information. Not processed.');
case '4':
return t('Address not checked, or acquirer had no response. Service not available.');
case 'Null':
default:
return t('No AVS response was obtained.');
}
}
switch ($code) {
case 'A':
case 'B':
return t('Address matched; postal code did not');
case 'C':
case 'N':
return t('Nothing matched; transaction declined');
case 'D':
case 'F':
case 'X':
case 'Y':
return t('Address and postal code matched');
case 'E':
return t('Not allowed for MOTO transactions; transaction declined');
case 'G':
return t('Global unavailable');
case 'I':
return t('International unavailable');
case 'P':
case 'W':
case 'Z':
return t('Postal code matched; address did not');
case 'R':
return t('Retry for validation');
case 'S':
return t('Service not supported');
case 'U':
return t('Unavailable');
default:
return t('An unknown error occurred.');
}
}
*/
/**
* Returns an appropriate message given a CVV2 match code.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_cvv_match_message($code) {
if (is_numeric($code)) {
switch ($code) {
case '0':
return t('Matched');
case '1':
return t('No match');
case '2':
return t('The merchant has not implemented CVV2 code handling.');
case '3':
return t('Merchant has indicated that CVV2 is not present on card.');
case '4':
return t('Service not available');
default:
return t('Unkown error');
}
}
switch ($code) {
case 'M':
case 'Y':
return t('Match');
case 'N':
return t('No match');
case 'P':
return t('Not processed');
case 'S':
return t('Service not supported');
case 'U':
return t('Service not available');
case 'X':
return t('No response');
default:
return t('Not checked');
}
}
*/
/**
* Returns a short description of the pending reason based on the given value.
*/
/* Not relevant to IITB - sunthar
function commerce_asciitb_short_pending_reason($pendingreason) {
switch ($pendingreason) {
case 'none':
return t('No pending reason.');
case 'authorization':
return t('Authorization pending capture.');
case 'address':
return t('Pending unconfirmed address review.');
case 'echeck':
return t('eCheck has not yet cleared.');
case 'intl':
return t('Pending international transaction review.');
case 'multi-currency':
return t('Pending multi-currency review.');
case 'verify':
return t('Payment held until your account is verified.');
case 'completed':
return t('Payment has been completed.');
case 'other':
return t('Pending for an unknown reason.');
default:
return '';
}
}
*/
/**
* Returns an array of ASC-IITB payment methods.
*/
function commerce_asciitb_payment_methods() {
return array(
'sbinet' => t('SBI Netbanking'),
'cannet' => t('Canara Netbanking'),
'debit' => t('Debit cards'),
'visa' => t('Visa'),
'mastercard' => t('Mastercard'),
'amex' => t('American Express'),
);
}
/**
* Returns an array of ASC-IITB payment method icon img elements.
*
* @param $methods
* An array of ASC-IITB payment method names to include in the icons array; if
* empty, all icons will be returned.
*
* @return
* The array of themed payment method icons keyed by name: visa, mastercard,
* amex, discover, echeck, asciitb
*/
function commerce_asciitb_icons($methods = array()) {
$icons = array();
foreach (commerce_asciitb_payment_methods() as $name => $title) {
if (empty($methods) || in_array($name, $methods, TRUE)) {
$variables = array(
'path' => drupal_get_path('module', 'commerce_asciitb') . '/images/' . $name . '.gif',
'title' => $title,
'alt' => $title,
'attributes' => array(
'class' => array('commerce-asciitb-icon'),
),
);
$icons[$name] = theme('image', $variables);
}
}
return $icons;
}
/**
* Formats a price amount into a decimal value as expected by ASC-IITB.
*
* @param $amount
* An integer price amount.
* @param $currency_code
* The currency code of the price.
*
* @return
* The decimal price amount as expected by ASC-IITB API servers.
*/
function commerce_asciitb_price_amount($amount, $currency_code) {
$rounded_amount = commerce_currency_round($amount, commerce_currency_load($currency_code));
return number_format(commerce_currency_amount_to_decimal($rounded_amount, $currency_code), 2, '.', '');
}