-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuntopago.pages.inc
executable file
·454 lines (394 loc) · 12.8 KB
/
puntopago.pages.inc
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* @file
* This file contains the primary pages of this module.
*
* The primary pages are the failure page and the success page.
*/
/**
* Processes and HTML response on a failure.
*
* @return string
* HTML output of the page
*/
function puntopago_failure() {
$exists_order = isset($_POST['TBK_ORDEN_COMPRA']);
$exists_session = isset($_POST['TBK_ID_SESION']);
if (!$exists_order || !$exists_session) {
drupal_goto(variable_get('puntopago_redirect_no_order_failure', ''));
}
$session = puntopago_prepare_session();
$order_id = $_POST['TBK_ORDEN_COMPRA'];
$commerce_infos = module_invoke_all('puntopago_commerce_system');
$function = $commerce_infos[$session['commerce']]['failure'];
$transaction = puntopago_get_transaction_by_order($order_id, $session['commerce']);
if (function_exists($function)) {
$output = $function($order_id, $transaction);
}
else {
$args = array('!commerce' => $session['commerce']);
$message = t('<em>Failure</em> function is not defined by !commerce', $args);
drupal_set_message(check_plain($message), 'error');
$output = '...';
}
return $output;
}
/**
* Processes and HTML response on a success case.
*
* @return string
* HTML output of the page
*/
function puntopago_success() {
$exists_order = isset($_POST['TBK_ORDEN_COMPRA']);
$exists_session = isset($_POST['TBK_ID_SESION']);
if (!$exists_order || !$exists_session) {
drupal_goto(variable_get('puntopago_redirect_no_order_success', ''));
}
$session = puntopago_prepare_session();
$order_id = $_POST['TBK_ORDEN_COMPRA'];
$transaction = puntopago_get_transaction_by_order($order_id, $session['commerce']);
$commerce_infos = module_invoke_all('puntopago_commerce_system');
$function = $commerce_infos[$session['commerce']]['success'];
if (function_exists($function)) {
$output = $function($order_id, $transaction);
// If the system detects an irregularity can return FALSE.
// Then it is a failed transaction.
if ($output === FALSE) {
drupal_set_title(t('Order failed'));
return puntopago_failure();
}
}
else {
$args = array('!commerce' => $session['commerce']);
$message = t('<em>Success</em> function is not defined by !commerce.', $args);
drupal_set_message(check_plain($message), 'error');
$output = '...';
}
return $output;
}
/**
* Processes and transactions for the closing page.
*/
function puntopago_close() {
$puntopago_commerce_system = module_invoke_all('puntopago_commerce_system');
/* Registers on Drupal's dlog , the parameters from $_SERVER variable */
if (variable_get('puntopago_debug_puntopago_close', FALSE)) {
puntopago_debug_watchdog_server_variable();
}
$currency = arg(2);
if (!$currency || !puntopago_validate_currency($currency)) {
echo PUNTOPAGO_REJECTED;
drupal_exit();
}
if (!isset($_POST['TBK_ORDEN_COMPRA']) ||
!isset($_POST['TBK_ID_TRANSACCION']) ||
!isset($_POST['TBK_MAC'])) {
echo PUNTOPAGO_REJECTED;
drupal_exit();
}
$root = puntopago_document_root();
$session = puntopago_prepare_session();
$commerce = $session['commerce'];
$uid = $session['id'];
$transaction = puntopago_prepare_transaction();
$callbacks = $puntopago_commerce_system[$commerce];
if (!puntopago_save_transaction($transaction, $callbacks, $uid)) {
echo PUNTOPAGO_REJECTED;
drupal_exit();
}
$message = array();
if ($_POST['TBK_RESPUESTA'] == 0) {
$puntopago_settings = puntopago_get_settings_kcc();
$path_log = $root . '/' . $puntopago_settings[$currency]['puntopago_ROOT_CGI'] . '/log';
$path_check = $root . '/' . $puntopago_settings[$currency]['puntopago_ROOT_CGI'] . '/tbk_check_mac.';
$temporal = $path_log . "/temporal.txt";
$filename = $path_log . "/log_" . $_POST['TBK_ID_TRANSACCION'] . ".txt";
puntopago_save_authorization_code($temporal);
puntopago_save_post_variables($filename);
$result = puntopago_invoke_tbk_check_mac($filename, $commerce, $path_check, $message);
if (isset($result[0]) && $result[0] == PUNTOPAGO_CORRECT) {
$order = puntopago_order_load($_POST['TBK_ORDEN_COMPRA'], $callbacks);
$validation_order = puntopago_check_order($order, $callbacks, $message);
if ($validation_order == PUNTOPAGO_ACCEPTED) {
$validation_amount = puntopago_check_amount($order, $currency, $message);
if ($validation_amount == PUNTOPAGO_ACCEPTED) {
/*
After all validation, the order is in the state pending. Just in case
that the process is aborted by the user
*/
puntopago_accept_transaction($order, $callbacks);
echo PUNTOPAGO_ACCEPTED;
}
else {
echo PUNTOPAGO_REJECTED;/* amount not validated */
}
}
else {
echo PUNTOPAGO_REJECTED;/* order not validated */
}
}
else {
echo PUNTOPAGO_REJECTED;/* MAC not validated */
}
}
else {
$message[] = t('Transaction not allowed');
/*
* Rejected by Transbank but still OK for commerce.
*/
echo PUNTOPAGO_ACCEPTED;
}
puntopago_save_log($message);
drupal_exit();
}
/**
* Helper function to have an array with the transaction information.
*
* @return array
* An array with Punto pago parameters ready to be persisted.
*/
function puntopago_prepare_transaction() {
return array(
'tbk_transaccion' => $_POST['TBK_TIPO_TRANSACCION'],
'tbk_respuesta' => $_POST['TBK_RESPUESTA'],
'tbk_orden_compra' => $_POST['TBK_ORDEN_COMPRA'],
'tbk_id_session' => $_POST['TBK_ID_SESION'],
'tbk_cod_autorizacion' => $_POST['TBK_CODIGO_AUTORIZACION'],
'tbk_monto' => $_POST['TBK_MONTO'],
'tbk_nro_final_tarjeta' => $_POST['TBK_FINAL_NUMERO_TARJETA'],
'tbk_fecha_expiracion' => isset($_POST['TBK_FECHA_EXPIRACION']) ? $_POST['TBK_FECHA_EXPIRACION'] : '',
'tbk_fecha_contable' => $_POST['TBK_FECHA_CONTABLE'],
'tbk_fecha_transaccion' => $_POST['TBK_FECHA_TRANSACCION'],
'tbk_hora_transaccion' => $_POST['TBK_HORA_TRANSACCION'],
'tbk_id_transaccion' => $_POST['TBK_ID_TRANSACCION'],
'tbk_tipo_pago' => $_POST['TBK_TIPO_PAGO'],
'tbk_nro_cuotas' => $_POST['TBK_NUMERO_CUOTAS'],
'tbk_mac' => $_POST['TBK_MAC'],
'tbk_tasa_interes_max' => isset($_POST['TBK_TASA_INTERES_MAX']) ? $_POST['TBK_TASA_INTERES_MAX'] : '1',
'tbk_vci' => isset($_POST['TBK_VCI']) ? $_POST['TBK_VCI'] : '',
'currency' => arg(2),
'timestamp_transaction' => time(),
);
}
/**
* Saves the authorization code into a log file.
*
* @param string $temporal
* absolute path to the temporal log text file.
*/
function puntopago_save_authorization_code($temporal) {
if ($fp = fopen($temporal, "w")) {
fwrite($fp, $_POST['TBK_CODIGO_AUTORIZACION']);
fclose($fp);
}
}
/**
* Saves $_POST variables into a log file.
*
* @param string $filename
* absolute path to the log file
*/
function puntopago_save_post_variables($filename) {
$fp = fopen($filename, "w");
reset($_POST);
while (list($key, $val) = each($_POST)) {
fwrite($fp, "$key=$val&");
}
fclose($fp);
}
/**
* Validates MAC through a KCC script.
*
* @param string $filename
* file path and name for the KCC script that validates the MAC.
* @param string $commerce
* Current used commerce's name.
* @param string $path_check
* The tbk_check_mac file path.
* @param array $message
* an array with all the steps followed in the close page, as strings.
*
* @return array
* command result.
*/
function puntopago_invoke_tbk_check_mac($filename, $commerce, $path_check, &$message) {
$root = puntopago_document_root();
$extension_path = variable_get('puntopago_tbk_check_mac_extension', 'cgi');
$cmdline = $path_check . $extension_path . ' ' . $filename;
$message[] = t('Commerce used: !commerce', array('!commerce' => $commerce));
$message[] = t('Command: !cmd', array('!cmd' => $cmdline));
/* MAC validation through the script*/
exec($cmdline, $result, $retint);
$cmd_result = (!is_array($result) || !isset($result[0]));
if (!$cmd_result) {
$message[] = t('Command result: !result', array('!result' => $result[0]));
}
else {
$message[] = t('Command failed: !cmd', array('!cmd' => $cmdline));
}
return $result;
}
/**
* Validates the order by its status.
*
* @param array $order
* Generic Punto pago's order.
* @param array $message
* an array with all the steps followed in the close page, as strings.
*
* @return string
* - PUNTOPAGO_ACCEPTED if it is validated.
* - PUNTOPAGO_REJECTED if it is not validated.
*/
function puntopago_check_order($order, $commerce, &$message) {
$validation_order = PUNTOPAGO_REJECTED;
if (puntopago_validate_order_status($order, $commerce)) {
$validation_order = PUNTOPAGO_ACCEPTED;
}
$args = array(
'!validate_order' => $validation_order,
);
$message[] = t('Validates the order: !validate_order', $args);
return $validation_order;
}
/**
* Validates that the amount on Punto pago's order and commerce order are the same.
*
* @param array $order
* Generic Punto pago's order.
* @param string $currency
* Currency code
* @param array $message
* an array with all the steps followed in the close page, as strings.
*
* @return string
* - PUNTOPAGO_ACCEPTED if it is validated
* - PUNTOPAGO_REJECTED if it is not validated
*/
function puntopago_check_amount($order, $currency, &$message) {
$amount = puntopago_amount($order['order_total'], $currency);
$args = array(
'!TBK_MONTO' => $_POST['TBK_MONTO'],
'!order_total' => $amount,
);
$message[] = t('Compares TBK_MONTO: !TBK_MONTO with order_total: !order_total', $args);
$validation_amount = PUNTOPAGO_REJECTED;
if ($_POST['TBK_MONTO'] == $amount) {
$validation_amount = PUNTOPAGO_ACCEPTED;
}
$args_2 = array('!validate' => $validation_amount);
$message[] = t('Validates the amount: !validate', $args_2);
return $validation_amount;
}
/**
* Helper function to parse Punto pago information about session.
*
* @return array
* The array has the following keys.
* - commerce
* - id
*/
function puntopago_prepare_session() {
$explode = explode(':', $_POST['TBK_ID_SESION']);
return array(
'commerce' => $explode[0],
'id' => $explode[1],
);
}
/**
* Declares a pseudo hook that loads the order.
*
* @param int $order_id
* order id.
* @param array $callbacks
* It is an array with commerce system's callbacks.
*
* @return array
* an array with the order and the other parameters the commerce may give.
*/
function puntopago_order_load($order_id, $callbacks) {
$function = $callbacks['order load'];
$order = array(
'order_id' => $order_id,
);
if (function_exists($function)) {
$function($order);
}
else {
return FALSE;
}
return $order;
}
/**
* Implements a pseudo hook.
*
* This validates the order status. This function must be declared depending
* on the specific commerce system.
*
* @param array $order
* Order created with puntopago_order_load.
* @param array $callbacks
* It is an array with commerce system's callbacks.
*
* @return bool
* true if the order exists and the function has beed declared, false
* otherwise.
*/
function puntopago_validate_order_status($order, $callbacks) {
$function = $callbacks['close validate'];
if (!$order) {
return FALSE;
}
if (function_exists($function)) {
return $function($order);
}
return FALSE;
}
/**
* Function to invoke after all validations are ok.
*
* @param array $order
* Order created with puntopago_order_load.
* @param array $callbacks
* It is an array with commerce system's callbacks.
*/
function puntopago_accept_transaction($order, $callbacks) {
if (isset($callbacks['accept transaction'])) {
$function = $callbacks['accept transaction'];
if (function_exists($function)) {
$function($order);
}
}
}
/**
* Saves on Punto pago's log the transaction.
*
* Also invokes save_transaction function from the commerce submodule.
*
* @param array $transaction
* Corresponde a la transaccion enviada por puntopago.
* @param array $callbacks
* It is an array with commerce system's callbacks.
*/
function puntopago_save_transaction($transaction, $callbacks, $uid) {
$query = db_insert('puntopago')
->fields($transaction);
try {
$wid = $query->execute();
$transaction['wid'] = $wid;
if (isset($callbacks['save transaction'])) {
$function = $callbacks['save transaction'];
if (function_exists($function)) {
$function($transaction, $uid);
}
}
return TRUE;
}
catch (PDOException $err) {
$watchdog_message = 'Failed to save the transaction in the database: @err';
$watchdog_variable = array('@err' => $err->getMessage());
watchdog('PUNTOPAGO', $watchdog_message, $watchdog_variable, WATCHDOG_ERROR);
return FALSE;
}
}