1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Pagantis\OrdersApiClient\Client; |
4
|
|
|
use Pagantis\ModuleUtils\Exception\AlreadyProcessedException; |
5
|
|
|
use Pagantis\ModuleUtils\Exception\AmountMismatchException; |
6
|
|
|
use Pagantis\ModuleUtils\Exception\MerchantOrderNotFoundException; |
7
|
|
|
use Pagantis\ModuleUtils\Exception\NoIdentificationException; |
8
|
|
|
use Pagantis\ModuleUtils\Exception\OrderNotFoundException; |
9
|
|
|
use Pagantis\ModuleUtils\Exception\QuoteNotFoundException; |
10
|
|
|
use Pagantis\ModuleUtils\Exception\UnknownException; |
11
|
|
|
use Pagantis\ModuleUtils\Exception\WrongStatusException; |
12
|
|
|
use Pagantis\ModuleUtils\Model\Response\JsonSuccessResponse; |
13
|
|
|
use Pagantis\ModuleUtils\Model\Response\JsonExceptionResponse; |
14
|
|
|
use Pagantis\ModuleUtils\Exception\ConcurrencyException; |
15
|
|
|
use Pagantis\ModuleUtils\Model\Log\LogEntry; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
define('TABLE_PAGANTIS_LOG', 'pagantis_log'); |
19
|
|
|
define('TABLE_PAGANTIS_CONFIG', 'pagantis_config'); |
20
|
|
|
define('TABLE_PAGANTIS_ORDERS', 'pagantis_order'); |
21
|
|
|
define('TABLE_PAGANTIS_CONCURRENCY', 'pagantis_concurrency'); |
22
|
|
|
|
23
|
|
|
class notifyController |
24
|
|
|
{ |
25
|
|
|
/** @var mixed $pagantisOrder */ |
26
|
|
|
protected $pagantisOrder; |
27
|
|
|
|
28
|
|
|
/** @var $string $origin */ |
|
|
|
|
29
|
|
|
public $origin; |
30
|
|
|
|
31
|
|
|
/** @var $string */ |
|
|
|
|
32
|
|
|
public $order; |
33
|
|
|
|
34
|
|
|
/** @var mixed $oscommerceOrderId */ |
35
|
|
|
protected $oscommerceOrderId = ''; |
36
|
|
|
|
37
|
|
|
/** @var mixed $cfg */ |
38
|
|
|
protected $cfg; |
39
|
|
|
|
40
|
|
|
/** @var Client $orderClient */ |
41
|
|
|
protected $orderClient; |
42
|
|
|
|
43
|
|
|
/** @var Order $oscommerceOrder */ |
|
|
|
|
44
|
|
|
protected $oscommerceOrder; |
45
|
|
|
|
46
|
|
|
/** @var mixed $pagantisOrderId */ |
47
|
|
|
protected $pagantisOrderId = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Validation vs PagantisClient |
51
|
|
|
* |
52
|
|
|
* @return array|Array_ |
|
|
|
|
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
public function processInformation() |
56
|
|
|
{ |
57
|
|
|
require_once('vendor/autoload.php'); |
58
|
|
|
try { |
59
|
|
|
$this->checkConcurrency(); |
60
|
|
|
$this->getMerchantOrder(); |
61
|
|
|
$this->getPagantisOrderId(); |
62
|
|
|
$this->getPagantisOrder(); |
63
|
|
|
$this->checkOrderStatus(); |
64
|
|
|
$this->checkMerchantOrderStatus(); |
65
|
|
|
$this->validateAmount(); |
66
|
|
|
//$this->processMerchantOrder(); //ESTE PASO SE HACE EN EL CHECKOUT_PROCESS |
67
|
|
|
} catch (\Exception $exception) { |
68
|
|
|
$this->unblockConcurrency($this->oscommerceOrderId); |
69
|
|
|
$jsonResponse = new JsonExceptionResponse(); |
70
|
|
|
$jsonResponse->setMerchantOrderId($this->merchantOrderId); |
71
|
|
|
$jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
72
|
|
|
$jsonResponse->setException($exception); |
73
|
|
|
$this->insertLog($exception); |
74
|
|
|
$shippingUrl = trim(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL', false)); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($this->origin == 'notify') { |
77
|
|
|
$jsonResponse->printResponse(); |
78
|
|
|
} else { |
79
|
|
|
if ($exception->getMessage() == AlreadyProcessedException::ERROR_MESSAGE) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
header("Location: $shippingUrl"); |
84
|
|
|
exit; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function confirmInformation() |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$this->confirmPagantisOrder(); |
93
|
|
|
$this->updateBdInfo(); |
94
|
|
|
$jsonResponse = new JsonSuccessResponse(); |
95
|
|
|
$jsonResponse->setMerchantOrderId($this->merchantOrderId); |
96
|
|
|
$jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
97
|
|
|
$this->unblockConcurrency($this->oscommerceOrderId); |
98
|
|
|
} catch (\Exception $exception) { |
99
|
|
|
$this->rollbackMerchantOrder(); |
100
|
|
|
$this->unblockConcurrency($this->oscommerceOrderId); |
101
|
|
|
$jsonResponse = new JsonExceptionResponse(); |
102
|
|
|
$jsonResponse->setMerchantOrderId($this->merchantOrderId); |
103
|
|
|
$jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
104
|
|
|
$jsonResponse->setException($exception); |
105
|
|
|
$jsonResponse->toJson(); |
106
|
|
|
$this->insertLog($exception); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->origin == 'notify') { |
110
|
|
|
$jsonResponse->printResponse(); |
111
|
|
|
} else { |
112
|
|
|
return $jsonResponse; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* COMMON FUNCTIONS |
118
|
|
|
*/ |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws Exception |
122
|
|
|
*/ |
123
|
|
|
private function checkConcurrency() |
124
|
|
|
{ |
125
|
|
|
$this->getQuoteId(); |
126
|
|
|
$this->checkConcurrencyTable(); |
127
|
|
|
$this->unblockConcurrency(); |
128
|
|
|
$this->blockConcurrency(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @throws MerchantOrderNotFoundException |
133
|
|
|
*/ |
134
|
|
|
private function getMerchantOrder() |
135
|
|
|
{ |
136
|
|
|
global $order; |
137
|
|
|
$this->oscommerceOrder = $order; |
138
|
|
|
if (!isset($order->info)) { |
139
|
|
|
throw new MerchantOrderNotFoundException(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @throws NoIdentificationException |
145
|
|
|
*/ |
146
|
|
|
private function getPagantisOrderId() |
147
|
|
|
{ |
148
|
|
|
$query = "select pagantis_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_reference='".$this->oscommerceOrderId."'"; |
149
|
|
|
$resultsSelect = tep_db_query($query); |
|
|
|
|
150
|
|
|
while ($orderRow = tep_db_fetch_array($resultsSelect)) { |
|
|
|
|
151
|
|
|
$this->pagantisOrderId = $orderRow['pagantis_order_id']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($this->pagantisOrderId == '') { |
155
|
|
|
throw new NoIdentificationException(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @throws OrderNotFoundException |
161
|
|
|
*/ |
162
|
|
|
private function getPagantisOrder() |
163
|
|
|
{ |
164
|
|
|
try { |
165
|
|
|
$publicKey = trim(MODULE_PAYMENT_PAGANTIS_PK); |
|
|
|
|
166
|
|
|
$secretKey = trim(MODULE_PAYMENT_PAGANTIS_SK); |
|
|
|
|
167
|
|
|
$this->orderClient = new \Pagantis\OrdersApiClient\Client($publicKey, $secretKey); |
168
|
|
|
$this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
169
|
|
|
} catch (\Exception $e) { |
170
|
|
|
throw new OrderNotFoundException(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @throws AlreadyProcessedException |
176
|
|
|
* @throws WrongStatusException |
177
|
|
|
*/ |
178
|
|
|
private function checkOrderStatus() |
179
|
|
|
{ |
180
|
|
|
try { |
181
|
|
|
$this->checkPagantisStatus(array('AUTHORIZED')); |
182
|
|
|
} catch (\Exception $e) { |
183
|
|
|
if ($this->findOscommerceOrderId()!='') { |
184
|
|
|
throw new AlreadyProcessedException(); |
185
|
|
|
} else { |
186
|
|
|
if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
187
|
|
|
$status = $this->pagantisOrder->getStatus(); |
188
|
|
|
} else { |
189
|
|
|
$status = '-'; |
190
|
|
|
} |
191
|
|
|
throw new WrongStatusException($status); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @throws AlreadyProcessedException |
198
|
|
|
*/ |
199
|
|
|
private function checkMerchantOrderStatus() |
200
|
|
|
{ |
201
|
|
|
global $order; |
202
|
|
|
|
203
|
|
|
if ($order->info['order_status']!=='1') { |
204
|
|
|
throw new AlreadyProcessedException(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @throws AmountMismatchException |
210
|
|
|
*/ |
211
|
|
|
private function validateAmount() |
212
|
|
|
{ |
213
|
|
|
$pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); |
214
|
|
|
$ocAmount = intval($this->oscommerceOrder->info['total'] * 100); |
215
|
|
|
|
216
|
|
|
if ($pagantisAmount != $ocAmount) { |
217
|
|
|
throw new AmountMismatchException($pagantisAmount, $ocAmount); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return false|string |
223
|
|
|
* @throws UnknownException |
224
|
|
|
*/ |
225
|
|
|
private function confirmPagantisOrder() |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
|
|
$this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); |
229
|
|
|
} catch (\Exception $e) { |
230
|
|
|
throw new UnknownException($e->getMessage()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$jsonResponse = new JsonSuccessResponse(); |
234
|
|
|
return $jsonResponse->toJson(); |
235
|
|
|
} |
236
|
|
|
/** |
237
|
|
|
* UTILS FUNCTIONS |
238
|
|
|
*/ |
239
|
|
|
|
240
|
|
|
/** STEP 1 CC - Check concurrency */ |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @throws QuoteNotFoundException |
244
|
|
|
*/ |
245
|
|
|
private function getQuoteId() |
246
|
|
|
{ |
247
|
|
|
if ($this->oscommerceOrderId == "") { |
248
|
|
|
throw new QuoteNotFoundException(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Check if concurrency table exists |
254
|
|
|
*/ |
255
|
|
|
private function checkConcurrencyTable() |
256
|
|
|
{ |
257
|
|
|
$checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONCURRENCY."'"); |
|
|
|
|
258
|
|
|
if (tep_db_num_rows($checkTable) == 0) { |
|
|
|
|
259
|
|
|
$sql = "CREATE TABLE IF NOT EXISTS ".TABLE_PAGANTIS_CONCURRENCY." ( |
260
|
|
|
id int NOT NULL, |
261
|
|
|
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
262
|
|
|
UNIQUE KEY id(id))"; |
263
|
|
|
tep_db_query($sql); |
264
|
|
|
} |
265
|
|
|
return; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Unlock the concurrency |
270
|
|
|
* |
271
|
|
|
* @param null $orderId |
|
|
|
|
272
|
|
|
* @throws Exception |
273
|
|
|
*/ |
274
|
|
|
private function unblockConcurrency($orderId = null) |
275
|
|
|
{ |
276
|
|
|
try { |
277
|
|
|
if ($orderId == null) { |
|
|
|
|
278
|
|
|
$query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where timestamp<".(time() - 5); |
279
|
|
|
tep_db_query($query); |
|
|
|
|
280
|
|
|
} elseif ($orderId!='') { |
281
|
|
|
$query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where id='$orderId'"; |
282
|
|
|
tep_db_query($query); |
283
|
|
|
} |
284
|
|
|
} catch (Exception $exception) { |
285
|
|
|
throw new ConcurrencyException(); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @throws \Exception |
291
|
|
|
*/ |
292
|
|
|
private function blockConcurrency() |
293
|
|
|
{ |
294
|
|
|
try { |
295
|
|
|
$query = "SELECT timestamp FROM ".TABLE_PAGANTIS_CONCURRENCY." where id='$this->oscommerceOrderId'"; |
296
|
|
|
$resultsSelect = tep_db_query($query); |
|
|
|
|
297
|
|
|
$orderRow = tep_db_fetch_array($resultsSelect); |
|
|
|
|
298
|
|
|
|
299
|
|
|
if (isset($orderRow['timestamp'])) { |
300
|
|
|
throw new ConcurrencyException(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$query = "INSERT INTO ".TABLE_PAGANTIS_CONCURRENCY." (id) VALUES ('$this->oscommerceOrderId')"; |
304
|
|
|
tep_db_query($query); |
305
|
|
|
|
306
|
|
|
} catch (Exception $exception) { |
307
|
|
|
throw new ConcurrencyException(); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** STEP 2 GMO - Get Merchant Order */ |
312
|
|
|
/** STEP 3 GPOI - Get Pagantis OrderId */ |
313
|
|
|
/** STEP 4 GPO - Get Pagantis Order */ |
314
|
|
|
/** STEP 5 COS - Check Order Status */ |
315
|
|
|
/** |
316
|
|
|
* @param $statusArray |
317
|
|
|
* |
318
|
|
|
* @throws \Exception |
319
|
|
|
*/ |
320
|
|
|
private function checkPagantisStatus($statusArray) |
321
|
|
|
{ |
322
|
|
|
$pagantisStatus = array(); |
323
|
|
|
foreach ($statusArray as $status) { |
324
|
|
|
$pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
328
|
|
|
$payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); |
329
|
|
|
if (!$payed) { |
330
|
|
|
if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
|
|
|
|
331
|
|
|
$status = $this->pagantisOrder->getStatus(); |
332
|
|
|
} else { |
333
|
|
|
$status = '-'; |
334
|
|
|
} |
335
|
|
|
throw new WrongStatusException($status); |
336
|
|
|
} |
337
|
|
|
} else { |
338
|
|
|
throw new OrderNotFoundException(); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return mixed |
344
|
|
|
*/ |
345
|
|
|
private function findOscommerceOrderId() |
346
|
|
|
{ |
347
|
|
|
$query = "select os_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_reference='".$this->oscommerceOrderId."'"; |
348
|
|
|
$resultsSelect = tep_db_query($query); |
|
|
|
|
349
|
|
|
$orderRow = tep_db_fetch_array($resultsSelect); |
|
|
|
|
350
|
|
|
$this->merchantOrderId = $orderRow['os_order_id']; |
|
|
|
|
351
|
|
|
|
352
|
|
|
return $orderRow['os_order_id']; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** STEP 6 CMOS - Check Merchant Order Status */ |
356
|
|
|
/** STEP 7 VA - Validate Amount */ |
357
|
|
|
/** STEP 8 PMO - Process Merchant Order */ |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Save the order status with the related identification |
361
|
|
|
*/ |
362
|
|
|
private function updateBdInfo() |
363
|
|
|
{ |
364
|
|
|
global $insert_id; |
365
|
|
|
$this->merchantOrderId = $insert_id; |
|
|
|
|
366
|
|
|
$query = "update ".TABLE_PAGANTIS_ORDERS." set os_order_id='$insert_id' where os_order_reference='$this->oscommerceOrderId'"; |
367
|
|
|
tep_db_query($query); |
|
|
|
|
368
|
|
|
|
369
|
|
|
$comment = "Pagantis id=$this->pagantisOrderId/Via=".ucfirst($this->origin); |
370
|
|
|
$query = "insert into ".TABLE_ORDERS_STATUS_HISTORY ."(comments, orders_id, orders_status_id, customer_notified, date_added) values |
|
|
|
|
371
|
|
|
('$comment', ".$insert_id.", '2', -1, now() )"; |
372
|
|
|
tep_db_query($query); |
373
|
|
|
|
374
|
|
|
$query = "update ".TABLE_ORDERS." set orders_status='2' where orders_id='$insert_id'"; |
|
|
|
|
375
|
|
|
tep_db_query($query); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** STEP 9 CPO - Confirmation Pagantis Order */ |
379
|
|
|
private function rollbackMerchantOrder() |
380
|
|
|
{ |
381
|
|
|
global $insert_id; |
382
|
|
|
$query = "update orders set orders_status='1' where orders_id='$insert_id' "; |
383
|
|
|
tep_db_query($query); |
|
|
|
|
384
|
|
|
|
385
|
|
|
$query = "update ".TABLE_PAGANTIS_ORDERS." set os_order_id='' where os_order_reference='$this->oscommerceOrderId'"; |
386
|
|
|
tep_db_query($query); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param $exception |
391
|
|
|
*/ |
392
|
|
|
private function insertLog($exception) |
393
|
|
|
{ |
394
|
|
|
if ($exception instanceof \Exception) { |
395
|
|
|
$logEntry= new LogEntry(); |
396
|
|
|
$logEntryJson = $logEntry->error($exception)->toJson(); |
397
|
|
|
|
398
|
|
|
$query = "insert into ".TABLE_PAGANTIS_LOG."(log) values ('$logEntryJson')"; |
399
|
|
|
tep_db_query($query); |
|
|
|
|
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/*** |
404
|
|
|
* SETTERS Y GETTERS |
405
|
|
|
*/ |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @return mixed |
409
|
|
|
*/ |
410
|
|
|
public function getOscommerceOrderId() |
411
|
|
|
{ |
412
|
|
|
return $this->oscommerceOrderId; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param $oscommerceOrderId |
417
|
|
|
*/ |
418
|
|
|
public function setOscommerceOrderId($oscommerceOrderId) |
419
|
|
|
{ |
420
|
|
|
$this->oscommerceOrderId = $oscommerceOrderId; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return mixed |
425
|
|
|
*/ |
426
|
|
|
public function getOrigin() |
427
|
|
|
{ |
428
|
|
|
return $this->origin; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param mixed $origin |
433
|
|
|
*/ |
434
|
|
|
public function setOrigin($origin) |
435
|
|
|
{ |
436
|
|
|
$this->origin = $origin; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return String |
441
|
|
|
*/ |
442
|
|
|
public function getOrderStatus() |
443
|
|
|
{ |
444
|
|
|
return $this->orderStatus; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @param String $orderStatus |
449
|
|
|
*/ |
450
|
|
|
public function setOrderStatus($orderStatus) |
451
|
|
|
{ |
452
|
|
|
$this->orderStatus = $orderStatus; |
|
|
|
|
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|