-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquikpay.module
More file actions
524 lines (448 loc) · 19.9 KB
/
quikpay.module
File metadata and controls
524 lines (448 loc) · 19.9 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
<?php
/**
*
* @file
* Adds Nelnet QuikPAY payment method to Drupal Commerce
**/
function quikpay_menu() {
// Callback function for users returning from checkout.
$items['quikpay/rtpn'] = array(
'title' => 'QuikPAY Real Time Payment Notification',
'page callback' => 'quikpay_rtpn',
'access arguments' => array('access checkout'),
'type' => MENU_CALLBACK,
'file' => 'quikpay.pages.inc',
);
// LEGACY: should use the "quikpay" namespaced path.
// CE project is configured to use this path.
$items['commerce_nelnet/rtpn'] = array(
'title' => 'Nelnet QuikPAY Real Time Payment Notification',
'page callback' => 'quikpay_rtpn',
'access arguments' => array('access checkout'),
'type' => MENU_CALLBACK,
'file' => 'quikpay.pages.inc',
);
return $items;
}
function quikpay_commerce_payment_method_info() {
// Helpful reference at
// http://www.drupalcommerce.org/developer-guide/utilizing-core-apis/writing-payment-method-module
// Get settings, if we have them.
$settings = quikpay_get_settings();
if ($settings && is_array($settings['quikpay_cc_images'])) {
$cards = array_values(array_filter($settings['quikpay_cc_images']));
$card_images = '';
foreach ($cards as $card_name) {
$card_images .= theme('image', array(
'path' => drupal_get_path('module', 'quikpay') . '/images/' . $card_name . '.gif',
'alt' => $card_name
));
}
$card_list = array(
'#type' => 'markup',
'#markup' => $card_images,
);
}
else {
$cards = '';
}
drupal_add_css(drupal_get_path('module', 'quikpay') . '/css/' . 'quikpay.css');
// Now build actual payment methods
$payment_methods = array();
$payment_methods['quikpay'] = array(
'base' => 'quikpay',
'title' => t('Nelnet QuikPAY'),
'short_title' => t('QuikPAY'),
'description' => t('Nelnet QuikPAY'),
'display_title' => '<div id="quikpay-cards">Pay via Nelnet QuikPAY' . drupal_render($card_list) . '</div>',
'description' => t('Nelnet QuikPAY Payment Gateway'),
'active' => TRUE,
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => FALSE,
'callbacks' => array(
'settings_form' => 'quikpay_settings_form',
'redirect_form' => 'quikpay_redirect_form',
),
);
return $payment_methods;
}
/**
* Callback function from quikpay_commerce_payment_method_info()
*
* Generates commerce payment method's settings form
*
* @TODO - Pass in machine name of payment method
*
* @param null $settings
* @return array
*/
function quikpay_settings_form($settings = NULL) {
$form = array();
global $base_url;
$red_url = $base_url . '/quikpay/rtpn';
if (empty($settings)) {
$settings['order_type'] = "";
$settings['quikpay_title'] = "Nelnet QuikPAY Secure Payment Server";
$settings['quikpay_cc_images'] = array();
$settings['quikpay_mode'] = 'test';
$settings['quikpay_redirect'] = 'rtpn';
$settings['quikpay_redirect_url'] = $red_url;
$settings['quikpay_test_pt_key'] = 'key';
$settings['quikpay_test_rtpn_key'] = 'key';
$settings['quikpay_prod_pt_key'] = 'key';
$settings['quikpay_prod_rtpn_key'] = 'key';
$settings['quikpay_test_url'] = "https://uatquikpayasp.com/##ORGNAME##/commerce_manager/payer.do";
$settings['quikpay_prod_url'] = "https://quikpayasp.com/##ORGNAME##/commerce_manager/payer.do";
$settings['quikpay_success_text'] = 'Thank you for your payment. You may now view your orders by clicking on the link below.';
$settings['quikpay_checkout_text'] = 'IMPORTANT! In order to make an online payment a new window will open to receive and process your payment details. You may return to this site once your payment is complete by closing that window. You will receive an email receipt as well as an additional email containing course registration details.';
$settings['quikpay_checkout_red'] = 'You will be directed to a payment page that will process your payment details. Once the payment is complete, you will automatically return to this site and will be able to view your completed order. You will receive an email receipt as well as an additional email containing course registration details.';
}
if (empty($settings['quikpay_redirect_url'])) {
$settings['quikpay_redirect_url'] = $red_url;
}
$form['order_type'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY order type'),
'#default_value' => $settings['order_type'],
);
$form['quikpay_title'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY payment method title'),
'#default_value' => $settings['quikpay_title'],
'#description' => t('Title for payment method displayed to users.'),
);
$form['quikpay_cc_images'] = array(
'#type' => 'checkboxes',
'#title' => t('Credit card images to display'),
'#default_value' => $settings['quikpay_cc_images'],
'#options' => array(
'visa' => 'Visa',
'mastercard' => 'MasterCard',
'amex' => 'American Express',
'discover' => 'Discover',
),
'#description' => t('Choose credit card images to display when checking out.'),
);
$form['quikpay_mode'] = array(
'#type' => 'select',
'#title' => t('QuikPAY operation mode'),
'#default_value' => $settings['quikpay_mode'],
'#options' => array(
'test' => t('Test'),
'prod' => t('Production'),
),
'#description' => t('Global setting for the length of XML feed items that are output by default.'),
);
$form['quikpay_redirect'] = array(
'#type' => 'select',
'#title' => t('QuikPAY redirect method'),
'#default_value' => $settings['quikpay_redirect'],
'#options' => array(
'rtpn' => t('Real Time Payment Notification'),
'url' => t('Redirect URL'),
),
'#description' => t('Select whether to use RTPN or the Redirect URL method upon completion of payment.'),
);
$form['quikpay_redirect_url'] = array(
'#type' => 'textfield',
'#title' => t('URL used for redirect if redirect method is selected above.'),
'#default_value' => $settings['quikpay_redirect_url'],
'#description' => t('<strong>Make sure there is no trailing / in the URL as it matters in being authenticated by Nelnet!</strong>'),
);
$form['quikpay_test_pt_key'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY passthrough authentication test key'),
'#default_value' => $settings['quikpay_test_pt_key'],
);
$form['quikpay_test_rtpn_key'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY real-time payment notification test key'),
'#default_value' => $settings['quikpay_test_rtpn_key'],
);
$form['quikpay_prod_pt_key'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY passthrough authentication production key'),
'#default_value' => $settings['quikpay_prod_pt_key'],
);
$form['quikpay_prod_rtpn_key'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY real-time payment notification production key'),
'#default_value' => $settings['quikpay_prod_rtpn_key'],
);
$form['quikpay_test_url'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY test URL'),
'#default_value' => $settings['quikpay_test_url'],
);
$form['quikpay_prod_url'] = array(
'#type' => 'textfield',
'#title' => t('QuikPAY production URL'),
'#default_value' => $settings['quikpay_prod_url'],
);
$form['quikpay_success_text'] = array(
'#type' => 'textarea',
'#title' => t('Success message'),
'#default_value' => $settings['quikpay_success_text'],
'#description' => ('Text to display upon successful completion of payment'),
'#required' => TRUE
);
$checkout_text = 'IMPORTANT! In order to make an online payment a new window will open to receive and process your payment details. You may return to this site once your payment is complete by closing that window. You will receive an email receipt as well as an additional email containing course registration details.';
$form['quikpay_checkout_text'] = array(
'#type' => 'textarea',
'#title' => t('RTPN checkout instructions'),
'#default_value' => $settings['quikpay_checkout_text'],
'#description' => t('Instructional text to display below the proceed to checkout link'),
'#required' => TRUE
);
$form['quikpay_checkout_red'] = array(
'#type' => 'textarea',
'#title' => t('Redirect URL checkout instructions'),
'#default_value' => $settings['quikpay_checkout_red'],
'#description' => t('Instructional text to display below the proceed to checkout link'),
'#required' => TRUE
);
return $form;
}
/**
* Implements hook_form_alter().
*
* quikpay_settings_form is just a form snippet, so we need to do an alter on the
* containing form with form id rules-ui-edit-element in order to add a custom
* submit handler so we can add logging to settings changes.
*/
function quikpay_form_alter(&$form, $form_state, $form_id) {
// Add a custom submit handler so we can log updates to settings.
if (($form_id == 'rules_ui_edit_element') && (arg(5) == 'commerce_payment_quikpay')) {
$form['#submit'][] = 'quikpay_settings_form_log_submit';
}
}
/**
* Custom submit handler
*/
function quikpay_settings_form_log_submit($form, &$form_state) {
// Get current user
GLOBAL $user;
// Get current time.
$time = time();
$timestring = date('c', $time) . ' [' . $time . ']';
$message = t('Nelnet QuikPAY payment method settings updated by ') . $user->name . ' - ' . $timestring;
// Log settings changes to Drupal db log and PHP error log.
watchdog('quikpay', $message, NULL, WATCHDOG_INFO);
error_log($message);
}
/**
*
* Implements hook_form().
*
* Callback for quikpay_commerce_payment_method_info()
*
* @param $form
* @param $form_state
* @param $order - Order settings
* @param $payment_method - Payment method data
* @return mixed
*/
function quikpay_redirect_form($form, &$form_state, $order, $payment_method) {
// Determine if we're connecting to Test or Production payment processing server.
$environ = quikpay_get_environ();
//Set all variables, pull from environ, payment settings & current order
$url = $environ['quikpay_url']; //$payment_method['settings']['quikpay_test_url'];
$ptkey = $environ['pt_key']; //$payment_method['settings']['quikpay_test_pt_key'];
$redirect_method = $environ['redirect'];
$redirect_url = $environ['redirect_url'];
$order_data = entity_metadata_wrapper('commerce_order', $order);
$address = $order_data->commerce_customer_billing->commerce_customer_address->value();
$telephone = $order_data->commerce_customer_billing->field_commerce_customer_phone->value();
$amount_arr = $order_data->commerce_order_total->value();
// Setting and simplifying the phone number to 10 digits with no special characters
$telephone = preg_replace("/[^0-9]/", '', $telephone); // eliminate every character except 0-9
if (strlen($telephone) == 11) $telephone = preg_replace("/^1/", '',$telephone); // eliminate leading 1 if its there
// Get details for payload from order and line items.
// Format line item ids for load_multiple.
$raw_line_item_ids = $order->commerce_line_items['und'];
foreach ($raw_line_item_ids as $raw_line_item_id) {
$line_item_ids[] = $raw_line_item_id['line_item_id'];
}
$line_items = commerce_line_item_load_multiple($line_item_ids);
$order_items = array();
foreach ($line_items as $line_item) {
if ($line_item->type == 'product') {
$product = commerce_product_load($line_item->commerce_product['und'][0]['product_id']);
$price_items = field_get_items('commerce_product', $product, 'commerce_price');
// Added support for multiple order types.
// See README.txt for the steps in the UI to implement this change to product
// types.
if (isset($product->field_quikpay_order_type[LANGUAGE_NONE][0]['target_id'])) {
$payment_method['settings']['order_type'] = taxonomy_term_load($product->field_quikpay_order_type[LANGUAGE_NONE][0]['target_id'])->name;
}
$price = '$' . number_format($price_items[0]['amount'] / 100, 2);
$quantity = number_format($line_item->quantity, 0);
// Finally, put it all together.
$item_entry = 'SKU ' . $product->sku . ' | ';
$item_entry .= $quantity . ' @ ' . $price;
$order_items[] = $item_entry;
}
elseif ($line_item->type == 'commerce_coupon') {
$price = number_format($line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] / 100, 2);
$quantity = number_format($line_item->quantity, 0);
$item_entry = "Discount " . $line_item->line_item_label . " | " . $quantity . ' @ ' . $price;
$order_items[] = $item_entry;
}
else {
$item_entry = "Misc " . $line_item->line_item_label . " | " . $line_item->type;
$order_items[] = $item_entry;
}
// TODO If auth problems, try imposing documented limit of 50 characters
// on userChoiceNN fields...
}
// Get data for Nelnet together.
// The digest/hash includes our pass through key and parameters. Order
// matters - check pass through authentication documentation.
$variables = "orderType,orderNumber,amount,userChoice2,userChoice3,userChoice4,userChoice5,userChoice6,userChoice7,userChoice8,userChoice9,userChoice10,streetOne,streetTwo,city,state,zip,email";
// If method is url redirect, then add in the redirect parameters
if ($redirect_method == 'url') {
$variables .= ",redirectUrl,redirectUrlParameters";
}
$variables .= ",timestamp";
// Set up parameters for payload.
$param['orderType'] = $payment_method['settings']['order_type'];
$param['orderNumber'] = $order->order_id;
$param['amount'] = $amount_arr['amount'];
// Drupal Order ID
$param['userChoice2'] = $order->order_id;
// Individual order items. Trimmed to 48 characters.
$param['userChoice3'] = isset($order_items[0]) ? substr($order_items[0], 0, 48) : '';
$param['userChoice4'] = isset($order_items[1]) ? substr($order_items[1], 0, 48) : '';
$param['userChoice5'] = isset($order_items[2]) ? substr($order_items[2], 0, 48) : '';
$param['userChoice6'] = isset($order_items[3]) ? substr($order_items[3], 0, 48) : '';
$param['userChoice7'] = isset($order_items[4]) ? substr($order_items[4], 0, 48) : '';
$param['userChoice8'] = isset($order_items[5]) ? substr($order_items[5], 0, 48) : '';
$param['userChoice9'] = isset($order_items[6]) ? substr($order_items[6], 0, 48) : '';
$param['userChoice10'] = isset($order_items[7]) ? substr($order_items[7], 0, 48) : '';
// If we have an excess of 8 order items, overwrite last entry.
if (isset($order_items[8])) {
$param['userChoice10'] = "More... For full details see order ID " . $order->order_id . ".";
}
$param['streetOne'] = $address['thoroughfare'];
$param['streetTwo'] = $address['premise'];
$param['city'] = $address['locality'];
$param['state'] = $address['administrative_area'];
$param['zip'] = $address['postal_code'];
$param['daytimePhone'] = $telephone;
$param['email'] = $order_data->mail->value();
if ($redirect_method == 'url') {
$param['redirectUrl'] = $redirect_url;
$trans_variables = "transactionType,transactionStatus,transactionId,originalTransactionId,transactionTotalAmount,transactionDate,transactionAcountType,transactionEffectiveDate,transactionDescription,transactionResultDate,transactionResultEffectiveDate,transactionResultCode,transactionResultMessage,orderNumber,orderType,orderName,orderDescription,orderAmount,orderFee,orderAmountDue,orderDueDate,orderBalance,orderCurrentStatusBalance,orderCurrentStatusAmountDue";
$param['redirectUrlParameters'] = $trans_variables;
}
// Timestamp in milliseconds.
$param['timestamp'] = quikpay_get_timestamp();
// Allow for alter of $variables and $param to add/change data. Be sure to keep the two in sync.
// Function implementation signature:
// hook_quikpay_hash_param_alter(&$variables, &$param, $order)
drupal_alter('quikpay_hash_param', $variables, $param, $order);
$vars = explode(',', $variables);
// Create hash
$hash_string = "";
foreach ($vars as $key) {
$hash_string .= $param[$key];
}
// If using URL method, need to form truncated hash since most of the trans_variables are not available till post-processing
$param['hash'] = hash('sha256', $hash_string . $ptkey);
// Need to add this straggler.
array_push($vars, "hash");
// Add each parameter as hidden form value. It appears Commerce will submit these for us, correctly.
foreach ($vars as $key) {
$form[$key] = array(
'#type' => 'hidden',
'#value' => $param[$key],
);
}
// Redirect to the Quikpay url
$form['#action'] = $url;
$uid = $order->uid;
$quikpay_settings = quikpay_get_settings();
if (!$checkout_text = $quikpay_settings['quikpay_checkout_text']) {
$checkout_text = t('IMPORTANT! In order to make an online payment a new window will open to receive and process your payment details. You may return to this site once your payment is complete by closing that window. You will receive a receipt via email.');
}
if ($redirect_method == 'rtpn') {
$checkout_text = $quikpay_settings['quikpay_checkout_text'];
}
else {
$checkout_text = $quikpay_settings['quikpay_checkout_red'];
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Complete checkout'),
'#prefix' => '<div class="quikpay-instructions alert alert-status">' . $checkout_text . '</div>',
);
// Had issues with anything other than a button here - only a button
// would render. So we use jquery to add target="_blank" to form so
// we open in a new window. Kinda hacky, but it works.
// See http://css-tricks.com/snippets/html/form-submission-new-window/
if ($redirect_method == 'rtpn') {
drupal_add_js('jQuery(document).ready(function () { jQuery("form#quikpay-redirect-form").attr("target", "_blank"); });', 'inline');
}
return $form;
}
/**
* Helper function to determine if we're connecting to Test or Production payment processing server.
*
*/
function quikpay_get_environ() {
// Settings are stored in {rules_config}.
// @TODO Get which payment method programmatically
$rule = rules_config_load('commerce_payment_quikpay');
foreach ($rule->actions() as $action) {
$settings[] = $action->settings;
}
// Assuming only one payment method. For multiple payment account / order
// types see README.txt.
$environ['mode'] = $settings[0]['payment_method']['settings']['quikpay_mode'];
$environ['redirect'] = $settings[0]['payment_method']['settings']['quikpay_redirect'];
$environ['redirect_url'] = $settings[0]['payment_method']['settings']['quikpay_redirect_url'];
$environ['pt_key'] = $settings[0]['payment_method']['settings']['quikpay_' . $environ['mode'] . '_pt_key'];
$environ['rtpn_key'] = $settings[0]['payment_method']['settings']['quikpay_' . $environ['mode'] . '_rtpn_key'];
$environ['quikpay_url'] = $settings[0]['payment_method']['settings']['quikpay_' . $environ['mode'] . '_url'];
$environ['success'] = $settings[0]['payment_method']['settings']['quikpay_success_text'];
return $environ;
}
/**
* Helper function to get the settings for the chosen payment method
*
*/
function quikpay_get_settings() {
// Settings are stored in {rules_config}.
$rule = rules_config_load('commerce_payment_quikpay');
if (is_object($rule)) {
foreach ($rule->actions() as $action) {
$settings[] = $action->settings;
}
// Assuming only one payment method. For multiple payment account / order
// types see README.txt.
return $settings[0]['payment_method']['settings'];
}
return FALSE;
}
/**
* Helper function to get timestamp in milliseconds.
*
*/
function quikpay_get_timestamp() {
list($msecs, $uts) = explode(' ', microtime());
$timestamp = floor(($uts + $msecs) * 1000);
// Some configs of PHP can render this as scientific notation. Stop that.
$timestamp = number_format($timestamp, 0, '.', '');
return $timestamp;
}
function quikpay_commerce_payment_method_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
}
function quikpay_mail($key, &$message, $params) {
switch ($key) {
case 'rtpn':
$message['subject'] = t('RTPN page is Hit');
$message['body'] = t('Success.');
break;
}
}