-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_dragonpay.module
319 lines (270 loc) · 10.9 KB
/
commerce_dragonpay.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
<?php
/**
* @file
* Implements Dragonpay standard payment in Drupal Commerce checkout.
*/
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_dragonpay_commerce_payment_method_info() {
$payment_methods = array();
$payment_methods['Dragonpay'] = array(
'base' => 'commerce_dragonpay',
'title' => t('Dragonpay'),
'short_title' => t('Dragonpay'),
'description' => t('Dragonpay Standard Payments'),
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
'buttonsource' => 'Commerce_dragonpay',
);
return $payment_methods;
}
/**
* Returns the default settings for the Dragonpay standard payment method.
*/
function commerce_dragonpay_default_settings() {
$default_currency = commerce_default_currency();
return array(
'merchantid' => '',
'secretkey' => '',
'currency_code' => 'PHP',
'server' => 'sandbox',
);
}
/**
* Payment method callback: settings form.
*/
function commerce_dragonpay_settings_form($settings = array()) {
$form = array();
// Merge default settings into the stored settings array.
$settings = (array) $settings + commerce_dragonpay_default_settings();
$form['merchantid'] = array(
'#type' => 'textfield',
'#title' => t('Merchant ID'),
'#description' => t('A unique code assigned to Merchant. Register at Dragonpay.ph to get your Merchant ID'),
'#default_value' => $settings['merchantid'],
'#required' => TRUE,
);
$form['secretkey'] = array(
'#type' => 'textfield',
'#title' => t('Secret Key / Password'),
'#description' => t('A unique password assigned to Merchant for checksum validation.'),
'#default_value' => $settings['secretkey'],
'#required' => TRUE,
);
$form['server'] = array(
'#type' => 'radios',
'#title' => t('Dragonpay server'),
'#options' => array(
'sandbox' => ('Test - use for testing, requires a Dragonpay Test account'),
'live' => ('Live - use for processing real transactions'),
),
'#default_value' => $settings['server'],
);
$form['currency_code'] = array(
'#type' => 'select',
'#title' => t('Default currency'),
'#description' => t('Transactions in other currencies will be converted to this currency, so multi-currency sites must be configured to use appropriate conversion rates.'),
'#options' => commerce_dragonpay_currencies(),
'#default_value' => $settings['currency_code'],
);
return $form;
}
/**
* Payment method callback: adds a message to the submission form if enabled in
* the payment method settings.
*/
function commerce_dragonpay_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
$form = array();
if (!empty($payment_method['settings']['show_payment_instructions'])) {
$form['Dragonpay_information'] = array(
'#markup' => '<span class="commerce-Dragonpay--info">' . t('(Continue with checkout to complete payment via Dragonpay.)') . '</span>',
);
}
return $form;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_dragonpay_form_commerce_checkout_form_alter(&$form, &$form_state) {
// If this checkout form contains the payment method radios...
if (!empty($form['commerce_payment']['payment_method']['#options'])) {
// Loop over its options array looking for a Dragonpay option.
foreach ($form['commerce_payment']['payment_method']['#options'] as $key => &$value) {
list($method_id, $rule_name) = explode('|', $key);
// If we find Dragonpay ...
if ($method_id == 'Dragonpay') {
// Prepare the replacement radio button text with icons.
$value = t('Dragonpay - pay securely without sharing your financial information');
//$value .= '<div class="commerce-Dragonpay-icons"><span class="label">' . t('Includes:') . '</span>' . implode(' ', $icons) . '</div>';
// Add the CSS.
$form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_dragonpay') . '/theme/commerce_dragonpay.theme.css';
break;
}
}
}
}
/**
* Payment method callback: redirect form, a wrapper around the module's general
* use function for building a form.
*/
function commerce_dragonpay_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']['merchantid'])) {
drupal_set_message(t('Dragonpay is not configured for use. No Dragonpay Merchant ID has been specified.'), 'error');
return array();
}
$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'],
// Include the application indicator
'bn' => $payment_method['buttonsource'],
);
return commerce_dragonpay_order_form($form, $form_state, $order, $payment_method['settings'] + $settings);
}
/**
* Builds a Website Payments Standard form from an order object.
*
* @param $order
* The fully loaded order being paid for.
* @param $settings
* An array of settings used to build out the form, including:
* - server: which server to use, either sandbox or live
* - business: the Dragonpay e-mail address the payment submits to
* - cancel_return: the URL Dragonpay should send the user to on cancellation
* - return: the URL Dragonpay should send the user to on successful payment
* - currency_code: the Dragonpay currency code to use for this payment if the
* total for the order is in a non-Dragonpay supported currency
* - language: the Dragonpay language code to use on the payment form
* - payment_action: the Dragonpay payment action to use: sale, authorization,
* or order
* - payment_method: optionally a payment method instance ID to include in the
* IPN notify_url
*
* @return
* A renderable form array.
*/
function commerce_dragonpay_order_form($form, &$form_state, $order, $settings) {
$wrapper = entity_metadata_wrapper('commerce_order', $order);
// Determine the currency code to use to actually process the transaction,
// which will either be the default currency code or the currency code of the
// order if it's supported by Dragonpay if that option is enabled.
$currency_code = $settings['currency_code'];
$order_currency_code = $wrapper->commerce_order_total->currency_code->value();
if (!empty($settings['allow_supported_currencies']) && in_array($order_currency_code, array_keys(commerce_dragonpay_currencies()))) {
$currency_code = $order_currency_code;
}
$amount = $wrapper->commerce_order_total->amount->value();
// Ensure a default value for the payment_method setting.
$settings += array('payment_method' => '');
// Build the data array that will be translated into hidden form values.
$data = array(
// Varchar(20) A unique code assigned to Merchant.
'merchantid' => $settings['merchantid'],
// Varchar(40) A unique id identifying this specific transaction from the merchant side.
'txnid' => 'Order ' . $order->order_number,
// Numeric(12,2) The amount to get from the end-user.
'amount' => commerce_dragonpay_currency_amount_to_decimal($amount, $currency_code),
// Char(3) The currency of the amount.
'ccy' => $currency_code,
// Varchar(128) A brief description of what the payment is for.
'description' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name'))),
// Varchar(40) Email address of the customer.
'email' => $order->mail,
);
// Allow modules to alter parameters of the API request.
drupal_alter('commerce_dragonpay_order_form_data', $data, $order);
// Generate the digest key and it to the data array.
$digest = commerce_dragonpay_generate_digest_key($data, $settings['secretkey']);
$data += array('digest' => $digest);
$server_url = commerce_dragonpay_server_url($settings['server']);
// Create the redirect path.
$form['#action'] = url($server_url, array('query' => $data, 'absolute' => TRUE));
foreach ($data as $name => $value) {
if (!empty($value)) {
$form[$name] = array('#type' => 'hidden', '#value' => $value);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed to Dragon Pay'),
);
return $form;
}
/**
* Returns the URL to the specified Dragonpay server.
*
* @param $server
* Either sandbox or live indicating which server to get the URL for.
*
* @return
* The URL to use to submit requests to the Dragonpay server.
*/
function commerce_dragonpay_server_url($server) {
switch ($server) {
case 'sandbox':
return 'http://test.Dragonpay.ph/Pay.aspx';
case 'live':
return 'https://gw.Dragonpay.ph/Pay.aspx';
}
}
/**
* Returns a sha1 string generated from the data and password.
*
* @param $data array
* The data array containing the following data, Merchant ID, Transaction ID, Amount in decimal,
* - Currency code, Simple description, and Email Address.
*
* @param $passwd string
* The secret code or password.
*
* @return
* The generated sha1 string
*/
function commerce_dragonpay_generate_digest_key($data, $passwd) {
// Add the password into the data.
$data += array('passwd' => $passwd);
//Generate a format e.g. "merchantid:txnid:amount:PHP:description:email:passwd";
$string = implode(':', $data);
// Return a sha1 string.
return sha1($string);
}
/**
* Returns an array of all possible currency codes for the different PayPal
* payment methods.
*
* @param $method_id
* The ID of the PayPal 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 PayPal payment method.
*/
function commerce_dragonpay_currencies($method_id = 'default') {
switch ($method_id) {
case 'default':
return drupal_map_assoc(array('PHP', 'USD'));
}
}
/**
* Converts a price amount to a decimal value based on the currency.
* - Always return a decimal value.
*
* @param $amount
* The price amount to convert to a decimal value.
*
* @param $currency_code
* The currency code of the price whose decimals value will be used to divide by the proper divisor when converting the amount.
*
* @return
* The decimal amount depending on the number of places the currency uses.
*/
function commerce_dragonpay_currency_amount_to_decimal($amount, $currency_code) {
$val = commerce_currency_amount_to_decimal($amount, $currency_code);
return (is_numeric( $val ) && floor( $val ) != $val) ? $val : sprintf('%.2f', $val);
}