-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_zarinpal.module
265 lines (229 loc) · 8.59 KB
/
commerce_zarinpal.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
<?php
/**
* @file
* Implements Zarinpal payment services for use in Drupal Commerce.
* @author
* Farhad Hedayati Fard <[email protected]> - 2012 (c)
* @license
* GPLv2
*/
/**
* Implements hook_commerce_payment_method_info().
*
* This hook will define the Zarinpal payment method
*/
function commerce_zarinpal_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['zarinpal'] = array(
'base' => 'commerce_zarinpal',
'title' => t('Zarinpal'),
'short_title' => t('Zarinpal'),
'display_title' => t('Zarinpal'),
'description' => t('Integrates Zarinpal payment system'),
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
);
return $payment_methods;
}
/**
* Payment method callback: settings form.
*
* Returns form elements for the payment methods settings form included
* as part of the payment methods enabling action in Rules
*/
function commerce_zarinpal_settings_form($settings = NULL) {
$form = array();
$settings = (array) $settings + array(
'merchant_id' => '',
'currency' => variable_get('commerce_default_currency', 'IRR'),
);
$form['merchant_id'] = array(
'#type' => 'textfield',
'#title' => t('Merchant ID'),
'#description' => t('Your Zarinpal Merchant ID'),
'#default_value' => isset($settings['merchant_id']) ? $settings['merchant_id'] : "",
);
$form['currency'] = array(
'#type' => 'select',
'#title' => t('Currency code'),
'#description' => t('Transactions can only be processed in one of the listed currencies.'),
'#options' => commerce_zarinpal_currencies(),
'#default_value' => $settings['currency'],
);
$form['#submit'][] = 'commerce_zarinpal_settings_form_submit';
return $form;
}
/**
* Payment method callback: redirect form
*
* returns form elements that should be submitted to the redirected
* payment service
*/
function commerce_zarinpal_redirect_form($form, &$form_state, $order, $payment_method) {
// Return an error if the enabling action's settings haven't been configured.
if (empty($payment_method['settings']['merchant_id'])) {
drupal_set_message(t('Zarinpal is not configured for use. No Zarinpal merchant ID has been specified.'), 'error');
return array();
}
// Set feedback URLs
$settings = array(
// Return to the previous page when payment is canceled
'cancel_return' => url('checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
// Return to the payment redirect page for processing successful payments
'return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
// Specify the current payment method instance ID in the notify_url
'payment_method' => $payment_method['instance_id'],
);
return commerce_zarinpal_build_redirect_form($form, $form_state, $order, $payment_method['settings'] + $settings);
}
/**
* Helper function for the redirect_form callback.
* Builds an Zarinpal payment form from an order object.
*/
function commerce_zarinpal_build_redirect_form($form, &$form_state, $order, $settings) {
global $user;
$wrapper = entity_metadata_wrapper('commerce_order', $order);
// Get financial info
$currency_code = $wrapper->commerce_order_total->currency_code->value();
// price is saved in "Xyz" format meaning X.yz rials, therfore We need to devide it by 100 * 10 to get price in tomans
$amount = (int)$wrapper->commerce_order_total->amount->value()/1000;
// Build the data array that will be translated into hidden form values.
$data = array(
$settings['merchant_id'],
$amount,
$settings['return'],
urlencode(t('Commerce Order') . ' #' .$order->order_id),
);
$nusoap_path = drupal_get_path('module', 'commerce_zarinpal') . '/nusoap.php';
include_once($nusoap_path);
$client = new nusoap_client('https://www.zarinpal.com/WebserviceGateway/wsdl', 'wsdl');
$res = $client->call('PaymentRequest', $data);
// Redirect to Zarinpal URL
$url = 'https://www.zarinpal.com/users/pay_invoice/'.$res;
$form['#action'] = $url;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed with payment'),
);
return $form;
}
/**
* Implements hook_redirect_form_validate().
*/
function commerce_zarinpal_redirect_form_validate($order, $payment_method) {
$nusoap_path = drupal_get_path('module', 'commerce_zarinpal') . '/nusoap.php';
include_once($nusoap_path);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$merchant_id = $payment_method['settings']['merchant_id'];
// price is saved in "Xyz" format meaning X.yz rials, therfore We need to devide it by 100 * 10 to get price in tomans
$amount = (int)$wrapper->commerce_order_total->amount->value()/1000;
if (isset($_GET['au'])) {
$client = new nusoap_client('https://www.zarinpal.com/WebserviceGateway/wsdl', 'wsdl');
$res = $client->call("PaymentVerification", array($merchant_id, $_GET['au'], $amount));
$feedback = array(
'result' => $res,
'remote_id' => $_GET['refID'],
'au' => $_GET['au'],
);
if ($res == 1) {
watchdog('commerce_zarinpal', "Zarinpal payment #" . $feedback['remote_id'] . " verification succeeded, Zarinpal returned " + $res, array(), WATCHDOG_INFO);
commerce_zarinpal_process_transaction($order, $payment_method, $feedback);
return TRUE;
}
else {
watchdog('commerce_zarinpal', "Zarinpal payment #" . $feedback['remote_id'] . " verification failed, Zarinpal returned " + $res, array(), WATCHDOG_ERROR);
return FALSE;
}
}
else {
watchdog('commerce_zarinpal', 'No valid au found', array(), WATCHDOG_ERROR);
return FALSE;
}
}
/**
* Process the payment transaction with the info received from Zarinpal
*
* @param $order
* The loaded order that is being processed
* @param $payment_method
* The payment method settings
* @param $feedback
* The parameters received from Zarinpal regarding the payment
*/
function commerce_zarinpal_process_transaction($order, $payment_method, $feedback) {
$transaction = commerce_payment_transaction_new('zarinpal', $order->order_id);
$payment_status = commerce_zarinpal_feedback_status($feedback['result']);
// identifying data
$transaction->instance_id = $payment_method['instance_id'];
$transaction->remote_id = $feedback['remote_id'];
// payment amount details
$transaction->amount = $order->commerce_order_total[LANGUAGE_NONE][0]['amount'];
$transaction->currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'];
// payment status
$transaction->remote_status = $feedback;
$transaction->status = $payment_status['code'];
$transaction->message = $payment_status['message'];
$transaction->payload = $feedback['au'];
commerce_payment_transaction_save($transaction);
if ($payment_status['code'] == COMMERCE_PAYMENT_STATUS_FAILURE) {
commerce_payment_redirect_pane_previous_page($order);
}
else {
commerce_payment_redirect_pane_next_page($order);
}
}
/**
* Get a list of enabled currencies
*
* @TODO: figure out which currencies Zarinpal actually supports
*/
function commerce_zarinpal_currencies() {
// Build a currency options list from all enabled currencies.
$options = array();
foreach (commerce_currencies(TRUE) as $currency_code => $currency) {
$options[$currency_code] = t('@code - !name', array('@code' => $currency['code'], '@symbol' => $currency['symbol'], '!name' => $currency['name']));
if (!empty($currency['symbol'])) {
$options[$currency_code] .= ' - ' . check_plain($currency['symbol']);
}
}
return $options;
}
function commerce_zarinpal_feedback_status($status) {
switch ($status) {
/** SUCCESS **/
case 1: // Order stored
$st = COMMERCE_PAYMENT_STATUS_SUCCESS;
$msg = t('Payment processed by merchant');
break;
/** FAILURE **/
case -1:
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Incomplete data');
break;
case -2:
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Invalid webservice');
break;
case -12:
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Payment timed out');
break;
case -11: // Authorization not known
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Payment amount conflict');
break;
case 0:
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Payment canceled by user');
break;
default:
$st = COMMERCE_PAYMENT_STATUS_FAILURE;
$msg = t('Unknown feedback status');
break;
}
return array(
'code' => $st,
'message' => $msg,
);
}