-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_chronopay.module
372 lines (318 loc) · 12.3 KB
/
commerce_chronopay.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
<?php
/**
* @file
* Provides a ChronoPay API payment method for Drupal Commerce.
*/
// ChronoPay Server URL Transaction.
define('CHRONOPAY_SERVER', 'https://payments.chronopay.com');
/**
* Implements hook_menu().
*/
function commerce_chronopay_menu() {
$items['commerce/chronopay/response/%/%'] = array(
'page callback' => 'commerce_chronopay_response',
'page arguments' => array(3, 4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_chronopay_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['chronopay'] = array(
'base' => 'commerce_chronopay',
'title' => t('ChronoPay'),
'short_title' => t('ChronoPay'),
'display_title' => t('ChronoPay'),
'description' => t('Integration with ChronoPay.'),
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
'active' => TRUE,
);
return $payment_methods;
}
/**
* Settings form for Chronopay payment method.
*
* Used to set merchant id and password within Rules settings.
*/
function commerce_chronopay_settings_form($settings = NULL) {
$form = array();
$settings = (array) $settings + array(
'settings' => array(
'site_id' => '',
'shared_sec' => '',
'ip' => '159.255.220.140',
'product_id' => '00000-0000-0000',
'product_name' => '',
'language_code' => 'RU',
),
'checkout' => array(
'payment_complete' => '<h1>PAYMENT COMPLETED!</h1><p>ChronoPay has accepted your card and payment has been made. Thank you.</p>',
'payment_denied' => '<h1>PAYMENT Declined!</h1><p>ChronoPay has declined your payment. Thank you. Please contact us if you are having difficulties.</p>',
'server_fail' => '<h1>Sorry!</h1><p>We were unable to validate your request with the ChronoPay server. Please contact us if you are having difficulties.</p>',
),
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Main settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
//Site_id
$form['settings']['site_id'] = array(
'#type' => 'textfield',
'#title' => t('Site_id'),
'#description' => t('This is the Site_id that ChronoPay sent you when you set up your account.'),
'#default_value' => $settings['settings']['site_id'],
'#required' => TRUE,
);
//Shared_sec
$form['settings']['shared_sec'] = array(
'#type' => 'textfield',
'#title' => t('Shared_sec'),
'#description' => t('This is the Shared_sec that ChronoPay sent you when you set up your account.'),
'#default_value' => $settings['settings']['shared_sec'],
'#required' => TRUE,
);
$form['settings']['ip'] = array(
'#type' => 'textfield',
'#title' => t('Chronopay Server IP'),
'#description' => t('IP address of the Chronppay payment server for security check.'),
'#default_value' => $settings['settings']['ip'],
'#required' => TRUE,
);
// Product ID
$form['settings']['product_id'] = array(
'#type' => 'textfield',
'#title' => t('Product ID'),
'#default_value' => $settings['settings']['product_id'],
'#size' => 20,
'#maxlength' => 255,
'#description' => t('<p>ChronoPay Product ID. This should be set to your product ID that has been set up in the ChronoPay client interface.</p><p>This is the ChronoPay product that all purchases will be processed against. The cost will be changed depending on the grand total of the users cart.</p>'),
);
// Product Name
$form['settings']['product_name'] = array(
'#type' => 'textfield',
'#title' => t('Product Name'),
'#default_value' => $settings['settings']['product_name'],
'#size' => 20,
'#maxlength' => 255,
'#description' => t('ChronoPay Product Name. This is not important and is usually set to the name of the web shop. It is displayed on the ChronoPay secure processing page..'),
);
// Language Code (language)
$language_codes = array('EN' => 'English', 'ES' => 'Spanish', 'NL' => 'Dutch', 'RU' => 'Russian');
$form['settings']['language_code'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#default_value' => $settings['settings']['language_code'],
'#options' => $language_codes,
'#description' => t('The language that the ChronoPay secure processing page will be displayed in.'),
);
$form['checkout'] = array(
'#type' => 'fieldset',
'#title' => t('ChronoPay checkout settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['checkout']['payment_complete'] = array(
'#type' => 'textarea',
'#title' => t('Payment Completed message'),
'#default_value' => $settings['checkout']['payment_complete'],
'#cols' => 60,
'#rows' => 5,
'#description' => t('Insert your own message that is displayed when a payment is completed using chronopay_callback'),
);
$form['checkout']['payment_denied'] = array(
'#type' => 'textarea',
'#title' => t('Order Declined message'),
'#default_value' => $settings['checkout']['payment_denied'],
'#cols' => 60,
'#rows' => 5,
'#description' => t('Insert your own message that is displayed when a payment is declined using chronopay_callback'),
);
$form['checkout']['server_fail'] = array(
'#type' => 'textarea',
'#title' => t('Server validation check failed message'),
'#default_value' => $settings['checkout']['server_fail'],
'#cols' => 60,
'#rows' => 5,
'#description' => t('Insert your own message that is displayed when the ChronoPay server validation check fails using chronopay_callback - a possible security breach'),
);
return $form;
}
/**
* Payment method callback: checkout form.
*/
function commerce_chronopay_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
$form = array();
$text = theme('image', array('path' => drupal_get_path('module', 'commerce_chronopay') . '/commerce_chronopay.png', 'alt' => t('VISA / MasterCard / MaestroCard'), 'title' => t('VISA / MasterCard / MaestroCard'), 'attributes' => array('class' => 'cards')));
$text .= '<div class="acquiring-desc">' . t('Pay via ChronoPay') . '</div>';
$form['commerce_chronopay_information'] = array('#markup' => $text);
$form['dummy'] = array(
'#type' => 'hidden',
'#value' => 'dummy'
);
return $form;
}
/**
* Payment method callback: checkout form submission.
*/
function commerce_chronopay_submit_form_submit($payment_method, $pane_form, $pane_values, &$order, $charge) {
// Create request to send to ChronoPay.
if (!isset($payment_method['settings']['settings']['site_id']) && !isset($payment_method['settings']['settings']['shared_sec'])) {
watchdog('commerce_chronopay', '%method : Site_id and shared_sec have not been set in the payment settings. Cannot proceed with transaction', WATCHDOG_ERROR);
drupal_set_message(t('The ChronoPay gateway has not been correctly configured - see watchdog for details. Cannot proceed with payment'), 'error');
return FALSE;
}
return TRUE;
}
/**
* Payment method callback: payment redirect form.
*/
function commerce_chronopay_redirect_form($form, &$form_state, $order, $payment_method) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$amount = $order_wrapper->commerce_order_total->amount->value();
$product_id = $payment_method['settings']['settings']['product_id'];
$shared_sec = $payment_method['settings']['settings']['shared_sec'];
$form['#action'] = CHRONOPAY_SERVER;
$form['product_id'] = array(
'#type' => 'hidden',
'#value' => $product_id,
);
$form['product_price'] = array(
'#type' => 'hidden',
'#value' => $amount / 100,
);
$form['sign'] = array(
'#type' => 'hidden',
'#value' => md5($product_id . '-' . $amount / 100 . '-' . $shared_sec),
);
$form['order_id'] = array(
'#type' => 'hidden',
'#value' => $order->order_id,
);
$form['cb_url'] = array(
'#type' => 'hidden',
'#value' => url($GLOBALS['base_url'] . '/commerce/chronopay/response/' . $order->order_id . '/callback'),
);
$form['cb_type'] = array(
'#type' => 'hidden',
'#value' => 'P',
);
$form['success_url'] = array(
'#type' => 'hidden',
'#value' => url($GLOBALS['base_url'] . '/commerce/chronopay/response/' . $order->order_id . '/success'),
);
$form['decline_url'] = array(
'#type' => 'hidden',
'#value' => url($GLOBALS['base_url'] . '/commerce/chronopay/response/' . $order->order_id . '/decline'),
);
$form['language'] = array(
'#type' => 'hidden',
'#value' => $payment_method['settings']['settings']['language_code'],
);
$form['country'] = array(
'#type' => 'hidden',
'#value' => 'RUS',
);
$form['email'] = array(
'#type' => 'hidden',
'#value' => $order->mail,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Pay via ChronoPay'),
);
return $form;
}
/**
* Menu callback.
*/
function commerce_chronopay_response($order_id, $response) {
// $order_id = isset($_POST['order_id']) ? $_POST['order_id'] : '';
$order = commerce_order_load($order_id);
$payment_method = commerce_payment_method_instance_load($order->data['payment_method']);
$sign = isset($_POST['sign']) ? $_POST['sign'] : '';
$shared_sec = $payment_method['settings']['settings']['shared_sec'];
$customer_id = isset($_POST['customer_id']) ? $_POST['customer_id'] : '';
$transaction_id = isset($_POST['transaction_id']) ? $_POST['transaction_id'] : '';
$transaction_type = isset($_POST['transaction_type']) ? $_POST['transaction_type'] : '';
$total = isset($_POST['total']) ? $_POST['total'] : '';
// drupal_set_message('<pre>' . print_r($_POST, TRUE) . '</pre>');
if (!$order) {
return t('The order does not exist.');
}
switch ($response) {
case 'callback':
//Check remote IP
if ($_SERVER['REMOTE_ADDR'] != $payment_method['settings']['settings']['ip']) {
watchdog('commerce_chronopay', 'Payment failed: An attempt to substitute ip address.', array(), WATCHDOG_WARNING);
return;
}
//Check hash
if ($sign != md5($shared_sec . $customer_id . $transaction_id . $transaction_type . $total)) {
watchdog('commerce_chronopay', 'Payment failed: An attempt to substitute ip address.', array(), WATCHDOG_WARNING);
return;
}
//Process the transaction
commerce_chronopay_transaction($order, '3');
break;
case 'success':
//Process the transaction
commerce_chronopay_transaction($order, '1');
commerce_payment_redirect_pane_next_page($order);
drupal_set_message($payment_method['settings']['checkout']['payment_complete'], 'status');
drupal_goto(commerce_checkout_order_uri($order));
break;
case 'decline':
$back_url = url($GLOBALS['base_url'] . '/checkout/' . $order->order_id);
drupal_set_message($payment_method['settings']['checkout']['payment_denied'], 'error');
commerce_payment_redirect_pane_previous_page($order);
drupal_goto($back_url);
break;
}
}
/**
* Create a transaction and associate it with an order.
*/
function commerce_chronopay_transaction($order, $status_code) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// If this is authorization for which we've already
// created a transaction...
$transactions = commerce_payment_transaction_load_multiple(array(), array('order_id' => $order->order_id));
if (!empty($transactions)) {
// Load transaction and update that with the capture values.
$transaction = reset($transactions);
}
else {
// Create a new payment transaction for the order.
$transaction = commerce_payment_transaction_new('commerce_chronopay', $order->order_id);
$transaction->instance_id = $order->data['payment_method'];
}
$transaction->amount = $order_wrapper->commerce_order_total->amount->value();
$transaction->currency_code = $order_wrapper->commerce_order_total->currency_code->value();
// Set a status for the payment - one of COMMERCE_PAYMENT_STATUS_SUCCESS, COMMERCE_PAYMENT_STATUS_PENDING or COMMERCE_PAYMENT_STATUS_FAILURE.
switch ($status_code) {
case '1':
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
$transaction->message = t('The payment has pending.');
// commerce_order_status_update($order, 'checkout_payment');
break;
case '3':
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$transaction->message = t('The payment has pending.');
commerce_order_status_update($order, 'checkout_complete');
break;
case '2':
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
$transaction->message = t('The payment has canceled.');
// commerce_order_status_update($order, 'canceled');
break;
}
commerce_payment_transaction_save($transaction);
}