-
Notifications
You must be signed in to change notification settings - Fork 0
/
multisafepay_payment.module
426 lines (373 loc) · 13.9 KB
/
multisafepay_payment.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
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
<?php
/**
* @file multisafepay_payment.module
*/
/**
* Implements hook_libraries_info().
*/
function multisafepay_payment_libraries_info() {
$libraries['multisafepay_php'] = array(
'name' => 'MultiSafePay Library PHP',
'vendor url' => 'https://github.com/MultiSafepay/PHP',
'download url' => 'https://github.com/MultiSafepay/PHP/archive/master.zip',
'version callback' => '_multisafepay_payment_simple_version_callback',
'files' => array(
// For PHP libraries
'php' => array(
'models/API/Client.php',
'models/API/Object/Core.php',
'models/API/Object/Gateways.php',
'models/API/Object/Issuers.php',
'models/API/Object/Orders.php',
),
),
);
return $libraries;
}
/**
* Return TRUE value.
*
* @return bool
* TRUE
*/
function _multisafepay_payment_simple_version_callback() {
return TRUE;
}
/**
* Implements hook_menu().
*/
function multisafepay_payment_menu() {
$items['multisafepay/callback'] = array(
'page callback' => 'multisafepay_payment_callback',
'access callback' => 'multisafepay_payment_callback_access',
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Implements hook_payment_method_controller_info().
*/
function multisafepay_payment_payment_method_controller_info() {
return array('MultiSafePayPaymentMethodController');
}
/**
* Retrieves the definitions of all MultiSafepay order plugins, or a specific one.
*
* @param string|null $plugin_msp_order
* (optional) The ID of the plugin whose definition should be
* retrieved. Or NULL to return all known definitions.
*
* @return array|null
* If $plugin_msp_order was given, either the definition of the given
* plugin, or NULL if it isn't known. Otherwise, an associative array of all
* known plugin definitions, keyed by ID.
*/
function multisafepay_payment_plugin_msp_order_info($plugin_msp_order = NULL) {
$plugins_msp_order = &drupal_static(__FUNCTION__);
if (!isset($plugins_msp_order)) {
$plugins_msp_order = module_invoke_all('multisafepay_payment_plugin_msp_order_info');
drupal_alter('multisafepay_payment_plugin_msp_order_info', $plugins_msp_order);
foreach ($plugins_msp_order as $i => $definition) {
if (empty($definition['class']) ||
!class_exists($definition['class']) ||
!in_array('MultiSafePayPaymentCreatorOrderInterface', class_implements($definition['class']))
) {
$variables['@plugin_msp_order'] = $i;
watchdog('multisafepay_payment', 'MultiSafepay creator plugin with ID @plugin_msp_order does not specify a valid plugin class.', $variables, WATCHDOG_ERROR);
unset($plugins_msp_order[$i]);
}
}
}
if (!isset($plugin_msp_order)) {
return $plugins_msp_order;
}
return isset($plugins_msp_order[$plugin_msp_order]) ? $plugins_msp_order[$plugin_msp_order] : NULL;
}
/**
* Implements multisafepay_payment_plugin_msp_order_info().
*/
function multisafepay_payment_multisafepay_payment_plugin_msp_order_info() {
$plugings_msp_order['redirect'] = array(
'label' => t('Redirect'),
'description' => t('For normal orders.'),
'class' => 'MultiSafePayPaymentCreatorOrderRedirect',
);
return $plugings_msp_order;
}
/**
* Implements hook_entity_load().
*/
function multisafepay_payment_entity_load(array $entities, $entity_type) {
if ($entity_type == 'payment_method') {
$pmids = array();
foreach ($entities as $payment_method) {
if ($payment_method->controller->name == 'MultiSafePayPaymentMethodController') {
$pmids[] = $payment_method->pmid;
}
}
if ($pmids) {
$query = db_select('multisafepay_payment_payment')
->fields('multisafepay_payment_payment')
->condition('msp_id', $pmids);
$result = $query->execute();
while ($data = $result->fetchAssoc()) {
$payment_method = $entities[$data['msp_id']];
$payment_method->controller_data = (array) $data;
unset($payment_method->controller_data['msp_id']);
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function multisafepay_payment_payment_method_insert(PaymentMethod $payment_method) {
if ($payment_method->controller->name == 'MultiSafePayPaymentMethodController') {
$values = $payment_method->controller_data += $payment_method->controller->controller_data_defaults;
$values['msp_id'] = $payment_method->pmid;
drupal_write_record('multisafepay_payment_payment', $values);
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function multisafepay_payment_payment_method_update(PaymentMethod $payment_method) {
if ($payment_method->controller->name == 'MultiSafePayPaymentMethodController') {
$values = $payment_method->controller_data += $payment_method->controller->controller_data_defaults;
$values['msp_id'] = $payment_method->pmid;
drupal_write_record('multisafepay_payment_payment', $values, 'msp_id');
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function multisafepay_payment_payment_method_delete($entity) {
if ($entity->controller->name == 'MultiSafePayPaymentMethodController') {
db_delete('multisafepay_payment_payment')
->condition('msp_id', $entity->pmid)
->execute();
}
}
/**
* Form build callback: implements
* PaymentMethodController::payment_method_configuration_form_elements_callback.
*/
function multisafepay_payment_payment_method_configuration_form_elements(array $form, array &$form_state) {
$payment_method = $form_state['payment_method'];
$controller = $payment_method->controller;
$controller_data = $payment_method->controller_data + $controller->controller_data_defaults;
// api_key
$elements['api_key'] = array(
'#default_value' => $controller_data['api_key'],
'#required' => TRUE,
'#title' => t('Api Key'),
'#description' => t('Api Key Site.'),
'#type' => 'textfield',
);
// api_server
$elements['api_server'] = array(
'#default_value' => $controller_data['api_server'],
'#options' => array(
$controller::SERVER_SANDBOX => t('Sandbox - use for testing'),
$controller::SERVER_PRODUCTION => t('Production - use for processing real transactions'),
),
'#required' => TRUE,
'#title' => t('Server call'),
'#description' => t(''),
'#type' => 'radios',
);
// Get all Plugins.
$plugins = multisafepay_payment_plugin_msp_order_info();
// Retrive default plugin used.
$plugin_default = isset($controller_data['plugin']) ? $controller_data['plugin'] : key($plugins);
$elements['plugin'] = array(
'#type' => 'radios',
'#title' => t('Plugin'),
'#description' => t('Choose the plugin implementation to use.'),
'#options' => array(),
'#required' => TRUE,
'#default_value' => $plugin_default,
'#ajax' => array(
'callback' => 'multisafepay_payment_plugin_ajax_callback',
'wrapper' => 'multisafepay-payment-plugin-configuration',
),
);
foreach ($plugins as $plugin_id => $plugin_data) {
$configuration = unserialize($controller_data['configuration']);
$configuration = is_array($configuration) ? $configuration : array();
$plugin = new $plugin_data['class']($plugin_id, $plugin_data, $configuration);
// Add the creator to the creator options.
$elements['plugin']['#options'][$plugin_id] = $plugin->label();
// Then, also add the configuration form for the selected suggester.
if ($plugin_id != $plugin_default) {
continue;
}
$plugin_form_state = &multisafepay_payment_get_plugin_form_state($form_state);
$plugin_form = $plugin->buildConfigurationForm(array(), $plugin_form_state);
if ($plugin_form) {
$elements['configuration']['plugin_configuration'] = $plugin_form;
$elements['configuration']['plugin_configuration']['#type'] = 'fieldset';
$elements['configuration']['plugin_configuration']['#title'] = t('Configure the %plugin plugin', array('%plugin' => $plugin->label()));
$elements['configuration']['plugin_configuration']['#description'] = $plugin->getDescription();
$elements['configuration']['plugin_configuration']['#collapsible'] = TRUE;
}
else {
$elements['configuration']['plugin_configuration']['#type'] = 'item';
}
$elements['configuration']['plugin_configuration']['#prefix'] = '<div id="multisafepay-payment-plugin-configuration">';
$elements['configuration']['plugin_configuration']['#suffix'] = '</div>';
}
$elements['configuration']['plugin_configuration']['old_plugin_id'] = array(
'#type' => 'hidden',
'#value' => $controller_data['plugin'],
'#tree' => FALSE,
);
return $elements;
}
/**
* Implements form validate callback for
* multisafepay_payment_payment_method_configuration_form_elements().
*/
function multisafepay_payment_payment_method_configuration_form_elements_validate(array $element, array &$form_state) {
$values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
$controller_data = &$form_state['payment_method']->controller_data;
$controller_data['api_key'] = $values['api_key'];
$controller_data['api_server'] = $values['api_server'];
$controller_data['plugin'] = $values['plugin'];
$controller_data['plugin'] = $values['plugin'];
$controller_data['configuration'] = $values['configuration']['plugin_configuration'];
// TODO: implements validateConfigurationForm() of plugin selected.
}
/**
* Implements form submit callback for
* multisafepay_payment_payment_method_configuration_form_elements().
*/
function multisafepay_payment_payment_method_configuration_form_elements_submit(array $element, array &$form_state) {
// TODO: implements submitConfigurationForm() of plugin selected.
}
/**
* Form AJAX handler for multisafepay_payment_payment_method_configuration_form_elements().
*/
function multisafepay_payment_plugin_ajax_callback(array $form, array &$form_state) {
return $form['controller_form']['configuration']['plugin_configuration'];
}
/**
* Returns a new form state for the suggester configuration sub-form.
*
* @param array $form_state
* The original form state.
*
* @return array
* A form state for the sub-form.
*/
function &multisafepay_payment_get_plugin_form_state(array &$form_state) {
$sub_form_state = &$form_state['suggester_form_state'];
foreach (array('values', 'input') as $key) {
if (!isset($form_state[$key]['options']['suggester_configuration'])) {
$form_state[$key]['options']['suggester_configuration'] = array();
}
$sub_form_state[$key] = &$form_state[$key]['options']['suggester_configuration'];
}
foreach (array('rebuild', 'rebuild_info', 'redirect') as $key) {
$sub_form_state[$key] = &$form_state[$key];
}
return $sub_form_state;
}
/**
* Defines the payment callback.
* POST callback that MultiSafepay uses to update the transaction.
*
*/
function multisafepay_payment_callback($status = NULL) {
if (!isset($_GET['transactionid']) && isset($_GET['payment_pid'])) {
drupal_access_denied();
}
// Load variables.
$transaction_id = $_GET['transactionid'];
$payment_pid = $_GET['payment_pid'];
$payment = entity_load_single('payment', $payment_pid);
if (!isset($payment)) {
drupal_access_denied();
}
// Load plugin creator MSP.
$plugin_id = $payment->method->controller_data['plugin'];
$definition = multisafepay_payment_plugin_msp_order_info($plugin_id);
if (!$definition) {
drupal_access_denied();
}
try {
$configuration = unserialize($payment->method->controller_data['configuration']);
$configuration = is_array($configuration) ? $configuration : array();
$plugin = new $definition['class']($plugin_id, $definition, $configuration);
$plugin->receiveFeedback($transaction_id, $payment, $status);
} catch (Exception $e) {
watchdog('multisafepay_payment', t('Error: @error'), array('@error' => $e->getMessage()), WATCHDOG_ERROR);
return NULL;
}
}
/**
* Checks access for the return URL.
*
* @return bool
*/
function multisafepay_payment_callback_access() {
if (!isset($_GET['transactionid']) && isset($_GET['payment_pid'])) {
return FALSE;
}
$transaction_id = $_GET['transactionid'];
$payment_pid = $_GET['payment_pid'];
$payment = entity_load_single('payment', $payment_pid);
if (!isset($payment)) {
return FALSE;
}
// TODO: check user has permission to access transaction.
return TRUE;
}
/**
* Retrieve MultiSafepay Order from server MultiSafepay.
*
* @param $transaction_id
* Transaction ID.
* @param \PaymentMethod $payment_method
* PaymentMethod used.
* @return Object|null
* Return, if found, a object contains MSP Order.
*/
function multisafepay_payment_get_order_from_multisafepay($transaction_id, PaymentMethod $payment_method) {
// Load the library 'multisafepay_php' and check.
if (($library = libraries_load('multisafepay_php')) && !empty($library['loaded'])) {
// Use MultiSafePay Libraries.
$msp = new Client();
$msp->setApiKey($payment_method->controller_data['api_key']);
$msp->setApiUrl($payment_method->controller->serverURL($payment_method->controller_data['api_server']));
try {
$msp_order = $msp->orders->get($endpoint = 'orders', $transaction_id, $body = array(), $query_string = FALSE);
return $msp_order;
} catch (Exception $e) {
drupal_set_message("Error in communication with MultiSafepay Server. Contact Support.", 'error');
watchdog('multisafepay_payment', t("Error: @error"), array('@error' => $e->getMessage()), WATCHDOG_ERROR);
return NULL;
}
}
return NULL;
}
/**
* @param null $msp_logo
* @return array|null|string
*/
function multisafepay_payment_logo($msp_logo = NULL) {
// TODO: include .gif
$files_logo = file_scan_directory(drupal_get_path('module', 'multisafepay_payment') . '/images', '([^\s]+(\.(?i)(jpg|png|gif|bmp))$)');
foreach ($files_logo as $file) {
$msp_maps_logos[$file->name] = array(
'name' => $file->name,
'filename' => $file->name,
'uri' => $file->uri,
);
}
if (isset($msp_logo)) {
$msp_logo = strtolower($msp_logo);
return isset($msp_maps_logos[$msp_logo]) ? $msp_maps_logos[$msp_logo] : NULL;
}
return $msp_maps_logos;
}