-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_sbpayment.module
401 lines (345 loc) · 12.2 KB
/
commerce_sbpayment.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
<?php
/**
* @file
* Implements the SoftBank Payment Service for use with Drupal Commerce.
*/
use Commerce\SBPayment\SBPayment;
use Commerce\SBPayment\Response\Response;
use Commerce\SBPayment\Payment\Methods;
// TODO: Add unit tests to validate API functionality.
define('COMMERCE_SBPAYMENT_BASE_URL', 'commerce/sbpayment');
define('COMMERCE_SBPAYMENT_PAYMENT_METHOD_BASE', 'commerce_sbpayment');
/**
* Implements hook_help().
*/
function commerce_sbpayment_help($path, $arg) {
switch ($path) {
case 'admin/help#commerce_sbpayment':
$output = '<h2>' . t('Commerce SoftBank Payment Service') . '</h2>';
$output .= '<p>' . t('Implements the SoftBank Payment Service for use with Drupal Commerce') . '<p>';
return $output;
}
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_sbpayment_commerce_payment_method_info() {
$payment_methods = array();
// Add a new payment method for each available SoftBank payment service.
foreach (commerce_sbpayment_services() as $machine_name => $sbpayment_service) {
// Build the commerce payment method machine name.
$sbpayment_service_method_name = sprintf('%s_%s', COMMERCE_SBPAYMENT_PAYMENT_METHOD_BASE, $machine_name);
// Determine the service name.
$sbpayment_service_name = !empty($sbpayment_service['name']) ? $sbpayment_service['name'] : $machine_name;
$payment_methods[$sbpayment_service_method_name] = array(
'file' => 'includes/' . $sbpayment_service_method_name . '.payment.inc',
'title' => t('SoftBank Payment Service'),
'description' => t('Redirect users to submit payments through the SoftBank Payment Service: @service_name.', array('@service_name' => $sbpayment_service_name)),
'active' => (commerce_default_currency() === 'JPY'),
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
);
}
return $payment_methods;
}
/**
* Implements hook_menu().
*/
function commerce_sbpayment_menu() {
$items = array();
$items[COMMERCE_SBPAYMENT_BASE_URL . '/%/%/%'] = array(
'type' => MENU_CALLBACK,
'file' => 'includes/commerce_sbpayment.menu.inc',
'page callback' => 'commerce_sbpayment_notification_controller',
'delivery callback' => 'commerce_sbpayment_deliver',
'page arguments' => array(2, 3, 4),
'access callback' => TRUE,
);
return $items;
}
/**
* Light weight menu delivery callback.
*
* Intended for use with payment notification routes.
*
* @param string $response
* Response from callback.
*
* @see commerce_sbpayment_notification_controller()
* @see drupal_deliver_html_page()
*/
function commerce_sbpayment_deliver($response) {
// Default Drupal delivery callback contains a lot of heavy logic that we
// don't need. So we are just returning the raw callback response.
print $response;
}
/**
* Get the list of available SoftBank services.
*
* @return array
* Array of SoftBank services keyed by a unique machine name.
*/
function commerce_sbpayment_services() {
// TODO: The XML API can be implemented here if required in future.
return array(
'link_type' => array(
'name' => t('Link Type'),
'class' => 'Commerce\SBPayment\Service\LinkType',
),
);
}
/**
* Get an instantiated payment service object.
*
* @param string $machine_name
* Machine name of the service to instantiate.
*
* @return bool|\Commerce\SBPayment\SBPayment
* An instantiated service object or FALSE if invalid.
*/
function commerce_sbpayment_get_service_object($machine_name) {
$services = commerce_sbpayment_services();
$sbpayment_service = $services[$machine_name];
// Check we have some service info.
if (empty($sbpayment_service)) {
return FALSE;
}
// Check if the defined class exists and extends the correct abstract class.
if (!class_exists($sbpayment_service['class']) || !is_subclass_of($sbpayment_service['class'], SBPayment::class)) {
return FALSE;
}
// Instantiate the service.
try {
return new $sbpayment_service['class']();
}
catch (Exception $e) {
watchdog_exception('commerce_sbpayment', $e);
}
return FALSE;
}
/**
* Generate a payment redirect form for a SoftBank payment service.
*
* @param \Commerce\SBPayment\SBPayment $sbpayment_service
* The payment service object with payment data set.
* @param object $order
* A fully loaded commerce order object.
* @param bool $debug
* Should debug information on the payment request be logged?
*
* @return array
* A renderable Drupal form array.
*/
function commerce_sbpayment_service_order_form(SBPayment $sbpayment_service, $order, $debug = FALSE) {
$form = array();
// Set the SoftBank service URL.
$form['#action'] = $sbpayment_service->getServiceUrl();
// Get the payment data from the service.
$payment_data = $sbpayment_service->getPaymentData();
// Allow other modules to alter the payment data.
$order_data = clone $order;
$sbpayment_service_name = (string) $sbpayment_service;
drupal_alter('commerce_sbpayment_service_order_form_data', $payment_data, $sbpayment_service_name, $order_data);
// Set the payment data fields.
foreach ($payment_data as $name => $value) {
$form[$name] = array('#type' => 'hidden', '#value' => $value);
}
// Set the product fields.
$i = 1;
foreach ($sbpayment_service->getProducts() as $product) {
foreach ($product->toArray() as $product_field => $product_data) {
$form[$product_field . '__' . $i] = array(
'#type' => 'hidden',
'#value' => $product_data,
'#pre_render' => array('commerce_sbpayment_service_product_pre_render'),
);
}
++$i;
}
// Add the request data and limit fields. They have to appear after the
// product detail fields.
$form['request_date'] = array(
'#type' => 'hidden',
'#value' => $sbpayment_service->getRequestDate(),
);
$form['limit_second'] = array(
'#type' => 'hidden',
'#value' => $sbpayment_service->getLimitSecond(),
);
// Add the checksum value.
$form['sps_hashcode'] = array(
'#type' => 'hidden',
'#value' => $sbpayment_service->getChecksum(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Proceed to SoftBank'),
);
$form['#pre_render'][] = 'commerce_sbpayment_service_order_form_pre_render';
// Log the debug data if required.
if ($debug === TRUE) {
watchdog('commerce_sbpayment', 'Payment request debug: <pre>!data</pre>', array(
'!data' => var_export($form, TRUE),
));
}
return $form;
}
/**
* Form element pre render callback.
*
* Tweak the name attribute of SoftBank product fields.
*
* @param array $element
* Product form field element.
*
* @return array
* Tweakedd product form field element.
*/
function commerce_sbpayment_service_product_pre_render($element) {
// Remove the index from the element name. Workaround for limitation of how
// Drupal form arrays are built (no duplicate field names).
$element_name = explode('__', $element['#name']);
$element['#name'] = $element_name[0];
return $element;
}
/**
* Form pre-render callback.
*
* Remove Drupal fields from a form (such as "form_token", "form_id" etc).
* This needs to be done since SoftBank will generate a signature from submitted
* data and will expect only those fields which described in API. Any other
* data will lead to wrong signature of payment request.
*/
function commerce_sbpayment_service_order_form_pre_render($form) {
unset($form['form_build_id']);
unset($form['form_id']);
return $form;
}
/**
* Creates/Updates a commerce payment transaction based on SoftBank data.
*
* @param array $payment_method
* Payment method information array.
* @param Response $sbpayment_response
* A SoftBank payment response object.
* @param object $order
* A fully loaded commerce order object.
*
* @see commerce_payment_methods()
*/
function commerce_sbpayment_transaction($payment_method, Response $sbpayment_response, $order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Determine the status for this payment.
if ($sbpayment_response->getRemoteStatus() === $sbpayment_response::RESPONSE_OK) {
$payment_status = COMMERCE_PAYMENT_STATUS_SUCCESS;
}
else {
// Assume it's failed otherwise.
$payment_status = COMMERCE_PAYMENT_STATUS_FAILURE;
}
// Get the remote status and ID.
$remote_status = $sbpayment_response->getRemoteStatus();
$remote_id = $sbpayment_response->getRemoteId();
// Build an array of conditions to check if a matching transaction
// already exists.
$conditions = array(
'order_id' => $order->order_id,
'instance_id' => $payment_method['instance_id'],
'payment_method' => $payment_method['method_id'],
'status' => $payment_status,
'remote_status' => $remote_status,
'remote_id' => $remote_id,
);
// Check for an existing transaction with these conditions.
$transactions = commerce_payment_transaction_load_multiple(array(), $conditions);
$transaction = reset($transactions);
// Create a new transaction if required.
if (empty($transaction)) {
$transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
}
// Set the transaction data.
$transaction->instance_id = $payment_method['instance_id'];
$transaction->status = $payment_status;
$transaction->remote_status = $remote_status;
$transaction->remote_id = $remote_id;
$transaction->amount = (int) $order_wrapper->commerce_order_total->amount->value();
$transaction->currency_code = $order_wrapper->commerce_order_total->currency_code->value();
$transaction->payload = !empty($sbpayment_response->toArray()) ? $sbpayment_response->toArray() : array();
// Build the transaction message.
$transaction->message = 'Payment status: @status. SoftBank status: @remote_status. SoftBank tracking ID: @remote_id.';
$transaction->message_variables = array(
'@status' => $payment_status,
'@remote_status' => !empty($remote_status) ? $remote_status : 'missing',
'@remote_id' => !empty($remote_id) ? $remote_id : 'missing',
);
commerce_payment_transaction_save($transaction);
}
/**
* Get the available gateway mode options for use with the Drupal form API.
*
* @return string[]
* Array of gateway modes keyed by machine name.
*/
function _commerce_sbpayment_mode_options() {
return array(
SBPayment::API_MODE_TEST => t('Test'),
SBPayment::API_MODE_LIVE => t('Live'),
);
}
/**
* Get a list of SoftBank payment methods for use with the Drupal form API.
*
* @param string[] $payment_methods
* List of the available payment method machine names as defined
* by Commerce\SBPayment\Payment\Methods.
*
* @return string[]
* Array of payment method names keyed by machine name.
*/
function _commerce_sbpayment_payment_method_options($payment_methods) {
// Build an array of payment method names keyed by the machine name. Filtering
// out any invalid methods.
$payment_method_options = array_filter(array_map(function ($payment_method) {
return Methods::getPaymentMethodByMachineName($payment_method);
}, $payment_methods));
// Allow other modules to alter the list of payment methods.
drupal_alter('commerce_sbpayment_payment_method_options', $payment_method_options);
return $payment_method_options;
}
/**
* Prepare an order number for use in a SoftBank request.
*
* Each request must have a unique order number, even if the user canceled and
* wants to try payment again.
*
* @param object $order
* A fully loaded Drupal commerce order.
*
* @return string
* The prepared order number.
*/
function _commerce_sbpayment_prepare_order_number($order) {
$order_number = $order->order_number;
// Append the number of payment attempts.
if (!empty($order->data['softbank']['payment_attempts']) && $order->data['softbank']['payment_attempts'] > 1) {
$order_number .= '__' . $order->data['softbank']['payment_attempts'];
}
return $order_number;
}
/**
* Get the appropriate SoftBank response data from the page request.
*
* @return array
* Array containing the response data from SoftBank.
*/
function _commerce_sbpayment_get_response_data() {
switch ($_SERVER['REQUEST_METHOD']) {
case 'POST':
return $_POST;
case 'GET':
return drupal_get_query_parameters();
default:
return array();
}
}