1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalOrigin\Pmt\Controller\Notify; |
4
|
|
|
|
5
|
|
|
use Magento\Quote\Model\QuoteManagement; |
6
|
|
|
use Magento\Quote\Api\Data\PaymentInterface; |
7
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
8
|
|
|
use Magento\Sales\Api\OrderRepositoryInterface; |
9
|
|
|
use Magento\Quote\Model\Quote; |
10
|
|
|
use Magento\Quote\Model\QuoteRepository; |
11
|
|
|
use Magento\Framework\App\Action\Context; |
12
|
|
|
use Magento\Framework\App\Action\Action; |
13
|
|
|
use PagaMasTarde\ModuleUtils\Exception\MerchantOrderNotFoundException; |
14
|
|
|
use PagaMasTarde\OrdersApiClient\Client; |
15
|
|
|
use DigitalOrigin\Pmt\Helper\Config; |
16
|
|
|
use DigitalOrigin\Pmt\Helper\ExtraConfig; |
17
|
|
|
use Magento\Framework\App\ResourceConnection; |
18
|
|
|
use Magento\Checkout\Model\Session; |
19
|
|
|
use Magento\Framework\DB\Ddl\Table; |
20
|
|
|
use PagaMasTarde\ModuleUtils\Exception\AmountMismatchException; |
21
|
|
|
use PagaMasTarde\ModuleUtils\Exception\ConcurrencyException; |
22
|
|
|
use PagaMasTarde\ModuleUtils\Exception\NoIdentificationException; |
23
|
|
|
use PagaMasTarde\ModuleUtils\Exception\OrderNotFoundException; |
24
|
|
|
use PagaMasTarde\ModuleUtils\Exception\QuoteNotFoundException; |
25
|
|
|
use PagaMasTarde\ModuleUtils\Exception\UnknownException; |
26
|
|
|
use PagaMasTarde\ModuleUtils\Exception\WrongStatusException; |
27
|
|
|
use PagaMasTarde\ModuleUtils\Model\Response\JsonSuccessResponse; |
28
|
|
|
use PagaMasTarde\ModuleUtils\Model\Response\JsonExceptionResponse; |
29
|
|
|
use PagaMasTarde\ModuleUtils\Exception\AlreadyProcessedException; |
30
|
|
|
use PagaMasTarde\ModuleUtils\Model\Log\LogEntry; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class Index |
34
|
|
|
* @package DigitalOrigin\Pmt\Controller\Notify |
35
|
|
|
*/ |
36
|
|
|
class Index extends Action |
37
|
|
|
{ |
38
|
|
|
/** Orders tablename */ |
39
|
|
|
const ORDERS_TABLE = 'cart_process'; |
40
|
|
|
|
41
|
|
|
/** Concurrency tablename */ |
42
|
|
|
const CONCURRENCY_TABLE = 'pmt_orders'; |
43
|
|
|
|
44
|
|
|
/** Concurrency tablename */ |
45
|
|
|
const LOGS_TABLE = 'pmt_logs'; |
46
|
|
|
|
47
|
|
|
/** Payment code */ |
48
|
|
|
const PAYMENT_METHOD = 'paylater'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* EXCEPTION RESPONSES |
52
|
|
|
*/ |
53
|
|
|
const CPO_ERR_MSG = 'Order not confirmed'; |
54
|
|
|
const CPO_OK_MSG = 'Order confirmed'; |
55
|
|
|
|
56
|
|
|
/** @var QuoteManagement */ |
57
|
|
|
protected $quoteManagement; |
58
|
|
|
|
59
|
|
|
/** @var PaymentInterface $paymentInterface */ |
60
|
|
|
protected $paymentInterface; |
61
|
|
|
|
62
|
|
|
/** @var OrderRepositoryInterface $orderRepositoryInterface */ |
63
|
|
|
protected $orderRepositoryInterface; |
64
|
|
|
|
65
|
|
|
/** @var Quote $quote */ |
66
|
|
|
protected $quote; |
67
|
|
|
|
68
|
|
|
/** @var QuoteRepository $quoteRepository */ |
69
|
|
|
protected $quoteRepository; |
70
|
|
|
|
71
|
|
|
/** @var mixed $config */ |
72
|
|
|
protected $config; |
73
|
|
|
|
74
|
|
|
/** @var mixed $quoteId */ |
75
|
|
|
protected $quoteId; |
76
|
|
|
|
77
|
|
|
/** @var array $notifyResult */ |
78
|
|
|
protected $notifyResult; |
79
|
|
|
|
80
|
|
|
/** @var mixed $magentoOrderId */ |
81
|
|
|
protected $magentoOrderId; |
82
|
|
|
|
83
|
|
|
/** @var mixed $pmtOrder */ |
84
|
|
|
protected $pmtOrder; |
85
|
|
|
|
86
|
|
|
/** @var ResourceConnection $dbObject */ |
87
|
|
|
protected $dbObject; |
88
|
|
|
|
89
|
|
|
/** @var Session $checkoutSession */ |
90
|
|
|
protected $checkoutSession; |
91
|
|
|
|
92
|
|
|
/** @var Client $orderClient */ |
93
|
|
|
protected $orderClient; |
94
|
|
|
|
95
|
|
|
/** @var mixed $pmtOrderId */ |
96
|
|
|
protected $pmtOrderId; |
97
|
|
|
|
98
|
|
|
/** @var OrderInterface $magentoOrder */ |
99
|
|
|
protected $magentoOrder; |
100
|
|
|
|
101
|
|
|
/** @var ExtraConfig $extraConfig */ |
102
|
|
|
protected $extraConfig; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Index constructor. |
106
|
|
|
* |
107
|
|
|
* @param Context $context |
108
|
|
|
* @param Quote $quote |
109
|
|
|
* @param QuoteManagement $quoteManagement |
110
|
|
|
* @param PaymentInterface $paymentInterface |
111
|
|
|
* @param Config $config |
112
|
|
|
* @param QuoteRepository $quoteRepository |
113
|
|
|
* @param OrderRepositoryInterface $orderRepositoryInterface |
114
|
|
|
* @param ResourceConnection $dbObject |
115
|
|
|
* @param Session $checkoutSession |
116
|
|
|
* @param ExtraConfig $extraConfig |
117
|
|
|
*/ |
118
|
|
|
public function __construct( |
119
|
|
|
Context $context, |
120
|
|
|
Quote $quote, |
121
|
|
|
QuoteManagement $quoteManagement, |
122
|
|
|
PaymentInterface $paymentInterface, |
123
|
|
|
Config $config, |
124
|
|
|
QuoteRepository $quoteRepository, |
125
|
|
|
OrderRepositoryInterface $orderRepositoryInterface, |
126
|
|
|
ResourceConnection $dbObject, |
127
|
|
|
Session $checkoutSession, |
128
|
|
|
ExtraConfig $extraConfig |
129
|
|
|
) { |
130
|
|
|
parent::__construct($context); |
131
|
|
|
$this->quote = $quote; |
132
|
|
|
$this->quoteManagement = $quoteManagement; |
133
|
|
|
$this->paymentInterface = $paymentInterface; |
134
|
|
|
$this->extraConfig = $extraConfig->getExtraConfig(); |
|
|
|
|
135
|
|
|
$this->config = $config->getConfig(); |
136
|
|
|
$this->quoteRepository = $quoteRepository; |
137
|
|
|
$this->orderRepositoryInterface = $orderRepositoryInterface; |
138
|
|
|
$this->dbObject = $dbObject; |
139
|
|
|
$this->checkoutSession = $checkoutSession; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void |
144
|
|
|
* @throws UnknownException |
145
|
|
|
*/ |
146
|
|
|
public function execute() |
147
|
|
|
{ |
148
|
|
|
try { |
149
|
|
|
$this->checkConcurrency(); |
150
|
|
|
$this->getMerchantOrder(); |
151
|
|
|
$this->getPmtOrderId(); |
152
|
|
|
$this->getPmtOrder(); |
153
|
|
|
$this->checkOrderStatus(); |
154
|
|
|
$this->checkMerchantOrderStatus(); |
155
|
|
|
$this->validateAmount(); |
156
|
|
|
$this->processMerchantOrder(); |
157
|
|
|
} catch (\Exception $exception) { |
158
|
|
|
$jsonResponse = new JsonExceptionResponse(); |
159
|
|
|
$jsonResponse->setMerchantOrderId($this->magentoOrderId); |
160
|
|
|
$jsonResponse->setPmtOrderId($this->pmtOrderId); |
161
|
|
|
$jsonResponse->setException($exception); |
162
|
|
|
$response = $jsonResponse->toJson(); |
163
|
|
|
$this->insertLog($exception); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
try { |
167
|
|
|
if (!isset($response)) { |
168
|
|
|
$this->confirmPmtOrder(); |
169
|
|
|
$jsonResponse = new JsonSuccessResponse(); |
170
|
|
|
$jsonResponse->setMerchantOrderId($this->magentoOrderId); |
171
|
|
|
$jsonResponse->setPmtOrderId($this->pmtOrderId); |
172
|
|
|
} |
173
|
|
|
} catch (\Exception $exception) { |
174
|
|
|
$this->rollbackMerchantOrder(); |
175
|
|
|
$jsonResponse = new JsonExceptionResponse(); |
176
|
|
|
$jsonResponse->setMerchantOrderId($this->magentoOrderId); |
177
|
|
|
$jsonResponse->setPmtOrderId($this->pmtOrderId); |
178
|
|
|
$jsonResponse->setException($exception); |
179
|
|
|
$jsonResponse->toJson(); |
180
|
|
|
$this->insertLog($exception); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->unblockConcurrency(true); |
184
|
|
|
|
185
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
186
|
|
|
$jsonResponse->printResponse(); |
|
|
|
|
187
|
|
|
} else { |
188
|
|
|
$returnUrl = $this->getRedirectUrl(); |
189
|
|
|
$this->_redirect($returnUrl); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* COMMON FUNCTIONS |
195
|
|
|
*/ |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @throws QuoteNotFoundException |
199
|
|
|
* @throws UnknownException |
200
|
|
|
*/ |
201
|
|
|
private function checkConcurrency() |
202
|
|
|
{ |
203
|
|
|
$this->getQuoteId(); |
204
|
|
|
$this->checkDbTable(); |
205
|
|
|
$this->unblockConcurrency(); |
206
|
|
|
$this->blockConcurrency(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @throws MerchantOrderNotFoundException |
211
|
|
|
*/ |
212
|
|
|
private function getMerchantOrder() |
213
|
|
|
{ |
214
|
|
|
try { |
215
|
|
|
/** @var Quote quote */ |
216
|
|
|
$this->quote = $this->quoteRepository->get($this->quoteId); |
217
|
|
|
} catch (\Exception $e) { |
218
|
|
|
throw new MerchantOrderNotFoundException(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @throws UnknownException |
224
|
|
|
*/ |
225
|
|
|
private function getPmtOrderId() |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
229
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
230
|
|
|
$tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
231
|
|
|
$query = "select order_id from $tableName where id='$this->quoteId'"; |
232
|
|
|
$queryResult = $dbConnection->fetchRow($query); |
233
|
|
|
$this->pmtOrderId = $queryResult['order_id']; |
234
|
|
|
if ($this->pmtOrderId == '') { |
235
|
|
|
throw new NoIdentificationException(); |
236
|
|
|
} |
237
|
|
|
} catch (\Exception $e) { |
238
|
|
|
throw new UnknownException($e->getMessage()); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @throws OrderNotFoundException |
244
|
|
|
*/ |
245
|
|
|
private function getPmtOrder() |
246
|
|
|
{ |
247
|
|
|
try { |
248
|
|
|
$this->orderClient = new Client($this->config['pmt_public_key'], $this->config['pmt_private_key']); |
249
|
|
|
$this->pmtOrder = $this->orderClient->getOrder($this->pmtOrderId); |
250
|
|
|
} catch (\Exception $e) { |
251
|
|
|
throw new OrderNotFoundException(); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @throws AlreadyProcessedException |
257
|
|
|
* @throws WrongStatusException |
258
|
|
|
*/ |
259
|
|
|
private function checkOrderStatus() |
260
|
|
|
{ |
261
|
|
|
try { |
262
|
|
|
$this->checkPmtStatus(array('AUTHORIZED')); |
263
|
|
|
} catch (\Exception $e) { |
264
|
|
|
$this->getMagentoOrderId(); |
265
|
|
|
if ($this->magentoOrderId!='') { |
266
|
|
|
throw new AlreadyProcessedException(); |
267
|
|
|
} else { |
268
|
|
|
throw new WrongStatusException($this->pmtOrder->getStatus()); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @throws AlreadyProcessedException |
275
|
|
|
*/ |
276
|
|
|
private function checkMerchantOrderStatus() |
277
|
|
|
{ |
278
|
|
|
if ($this->quote->getIsActive()=='0') { |
279
|
|
|
$this->getMagentoOrderId(); |
280
|
|
|
throw new AlreadyProcessedException(); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @throws AmountMismatchException |
286
|
|
|
*/ |
287
|
|
|
private function validateAmount() |
288
|
|
|
{ |
289
|
|
|
$pmtAmount = $this->pmtOrder->getShoppingCart()->getTotalAmount(); |
290
|
|
|
$merchantAmount = intval(strval(100 * $this->quote->getGrandTotal())); |
291
|
|
|
if ($pmtAmount != $merchantAmount) { |
292
|
|
|
throw new AmountMismatchException($pmtAmount, $merchantAmount); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @throws UnknownException |
298
|
|
|
*/ |
299
|
|
|
private function processMerchantOrder() |
300
|
|
|
{ |
301
|
|
|
try { |
302
|
|
|
$this->saveOrder(); |
303
|
|
|
$this->updateBdInfo(); |
304
|
|
|
} catch (\Exception $e) { |
305
|
|
|
throw new UnknownException($e->getMessage()); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return false|string |
311
|
|
|
* @throws UnknownException |
312
|
|
|
*/ |
313
|
|
|
private function confirmPmtOrder() |
314
|
|
|
{ |
315
|
|
|
try { |
316
|
|
|
$this->pmtOrder = $this->orderClient->confirmOrder($this->pmtOrderId); |
317
|
|
|
} catch (\Exception $e) { |
318
|
|
|
throw new UnknownException($e->getMessage()); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$jsonResponse = new JsonSuccessResponse(); |
322
|
|
|
$jsonResponse->setStatusCode(200); |
323
|
|
|
$jsonResponse->setMerchantOrderId($this->magentoOrderId); |
324
|
|
|
$jsonResponse->setPmtOrderId($this->pmtOrderId); |
325
|
|
|
$jsonResponse->setResult(self::CPO_OK_MSG); |
326
|
|
|
return $jsonResponse->toJson(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* UTILS FUNCTIONS |
331
|
|
|
*/ |
332
|
|
|
|
333
|
|
|
/** STEP 1 CC - Check concurrency |
334
|
|
|
* @throws QuoteNotFoundException |
335
|
|
|
*/ |
336
|
|
|
private function getQuoteId() |
337
|
|
|
{ |
338
|
|
|
$this->quoteId = $this->getRequest()->getParam('quoteId'); |
339
|
|
|
if ($this->quoteId == '') { |
340
|
|
|
throw new QuoteNotFoundException(); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return \Zend_Db_Statement_Interface |
346
|
|
|
* @throws UnknownException |
347
|
|
|
*/ |
348
|
|
|
private function checkDbTable() |
349
|
|
|
{ |
350
|
|
|
try { |
351
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
352
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
353
|
|
|
$tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
354
|
|
|
$query = "CREATE TABLE IF NOT EXISTS $tableName(`id` int not null,`timestamp` int not null,PRIMARY KEY (`id`))"; |
355
|
|
|
|
356
|
|
|
return $dbConnection->query($query); |
357
|
|
|
} catch (\Exception $e) { |
358
|
|
|
throw new UnknownException($e->getMessage()); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @return void|\Zend_Db_Statement_Interface |
364
|
|
|
* @throws UnknownException |
365
|
|
|
*/ |
366
|
|
|
private function checkDbLogTable() |
367
|
|
|
{ |
368
|
|
|
try { |
369
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
370
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
371
|
|
|
$tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
372
|
|
|
if (!$dbConnection->isTableExists($tableName)) { |
373
|
|
|
$table = $dbConnection |
374
|
|
|
->newTable($tableName) |
375
|
|
|
->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)) |
376
|
|
|
->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false)) |
377
|
|
|
->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT)); |
378
|
|
|
return $dbConnection->createTable($table); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return; |
382
|
|
|
} catch (\Exception $e) { |
383
|
|
|
throw new UnknownException($e->getMessage()); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param bool $mode |
389
|
|
|
* |
390
|
|
|
* @throws \Exception |
391
|
|
|
*/ |
392
|
|
|
private function unblockConcurrency($mode = false) |
393
|
|
|
{ |
394
|
|
|
try { |
395
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
396
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
397
|
|
|
$tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
398
|
|
|
if ($mode == false) { |
|
|
|
|
399
|
|
|
$dbConnection->delete($tableName, "timestamp<".(time() - 5)); |
400
|
|
|
} elseif ($this->quoteId!='') { |
401
|
|
|
$dbConnection->delete($tableName, "id=".$this->quoteId); |
402
|
|
|
} |
403
|
|
|
} catch (Exception $exception) { |
|
|
|
|
404
|
|
|
throw new ConcurrencyException(); |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @throws \Exception |
410
|
|
|
*/ |
411
|
|
|
private function blockConcurrency() |
412
|
|
|
{ |
413
|
|
|
try { |
414
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
415
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
416
|
|
|
$tableName = $this->dbObject->getTableName(self::CONCURRENCY_TABLE); |
417
|
|
|
$dbConnection->insert($tableName, array('id'=>$this->quoteId, 'timestamp'=>time())); |
418
|
|
|
} catch (Exception $exception) { |
419
|
|
|
throw new ConcurrencyException(); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** STEP 2 GMO - Get Merchant Order */ |
424
|
|
|
/** STEP 3 GPOI - Get Pmt OrderId */ |
425
|
|
|
/** STEP 4 GPO - Get Pmt Order */ |
426
|
|
|
/** STEP 5 COS - Check Order Status */ |
427
|
|
|
/** |
428
|
|
|
* @param $statusArray |
429
|
|
|
* |
430
|
|
|
* @throws \Exception |
431
|
|
|
*/ |
432
|
|
|
private function checkPmtStatus($statusArray) |
433
|
|
|
{ |
434
|
|
|
$pmtStatus = array(); |
435
|
|
|
foreach ($statusArray as $status) { |
436
|
|
|
$pmtStatus[] = constant("\PagaMasTarde\OrdersApiClient\Model\Order::STATUS_$status"); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$payed = in_array($this->pmtOrder->getStatus(), $pmtStatus); |
440
|
|
|
if (!$payed) { |
441
|
|
|
throw new WrongStatusException($this->pmtOrder->getStatus()); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** STEP 6 CMOS - Check Merchant Order Status */ |
446
|
|
|
/** |
447
|
|
|
* @throws \Exception |
448
|
|
|
*/ |
449
|
|
|
private function getMagentoOrderId() |
450
|
|
|
{ |
451
|
|
|
try { |
452
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
453
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
454
|
|
|
$tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
455
|
|
|
$pmtOrderId = $this->pmtOrderId; |
456
|
|
|
|
457
|
|
|
$query = "select mg_order_id from $tableName where id='$this->quoteId' and order_id='$pmtOrderId'"; |
458
|
|
|
$queryResult = $dbConnection->fetchRow($query); |
459
|
|
|
$this->magentoOrderId = $queryResult['mg_order_id']; |
460
|
|
|
} catch (\Exception $e) { |
461
|
|
|
throw new UnknownException($e->getMessage()); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** STEP 7 VA - Validate Amount */ |
466
|
|
|
/** STEP 8 PMO - Process Merchant Order */ |
467
|
|
|
/** |
468
|
|
|
* @throws UnknownException |
469
|
|
|
*/ |
470
|
|
|
private function saveOrder() |
471
|
|
|
{ |
472
|
|
|
try { |
473
|
|
|
$this->paymentInterface->setMethod(self::PAYMENT_METHOD); |
474
|
|
|
$this->magentoOrderId = $this->quoteManagement->placeOrder($this->quoteId, $this->paymentInterface); |
475
|
|
|
/** @var \Magento\Sales\Api\Data\OrderInterface magentoOrder */ |
476
|
|
|
$this->magentoOrder = $this->orderRepositoryInterface->get($this->magentoOrderId); |
477
|
|
|
|
478
|
|
|
if ($this->magentoOrderId == '') { |
479
|
|
|
throw new UnknownException('Order can not be saved'); |
480
|
|
|
} |
481
|
|
|
} catch (\Exception $e) { |
482
|
|
|
throw new UnknownException($e->getMessage()); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @throws UnknownException |
488
|
|
|
*/ |
489
|
|
|
private function updateBdInfo() |
490
|
|
|
{ |
491
|
|
|
try { |
492
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
493
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
494
|
|
|
$tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
495
|
|
|
$pmtOrderId = $this->pmtOrder->getId(); |
496
|
|
|
$dbConnection->update( |
497
|
|
|
$tableName, |
498
|
|
|
array('mg_order_id' => $this->magentoOrderId), |
499
|
|
|
"order_id='$pmtOrderId' and id='$this->quoteId'" |
500
|
|
|
); |
501
|
|
|
} catch (\Exception $e) { |
502
|
|
|
throw new UnknownException($e->getMessage()); |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** STEP 9 CPO - Confirmation Pmt Order */ |
507
|
|
|
/** |
508
|
|
|
* @throws UnknownException |
509
|
|
|
*/ |
510
|
|
|
private function rollbackMerchantOrder() |
511
|
|
|
{ |
512
|
|
|
try { |
513
|
|
|
$this->magentoOrder->setState(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT, true); |
|
|
|
|
514
|
|
|
$this->magentoOrder->setStatus(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT); |
515
|
|
|
$this->magentoOrder->save(); |
516
|
|
|
} catch (\Exception $e) { |
517
|
|
|
throw new UnknownException($e->getMessage()); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* @return string |
523
|
|
|
*/ |
524
|
|
|
private function getRedirectUrl() |
525
|
|
|
{ |
526
|
|
|
//$returnUrl = 'checkout/#payment'; |
527
|
|
|
$returnUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
528
|
|
|
if ($this->magentoOrderId!='') { |
529
|
|
|
/** @var Order $this->magentoOrder */ |
530
|
|
|
$this->magentoOrder = $this->orderRepositoryInterface->get($this->magentoOrderId); |
531
|
|
|
if (!$this->_objectManager->get(\Magento\Checkout\Model\Session\SuccessValidator::class)->isValid()) { |
532
|
|
|
$this->checkoutSession |
533
|
|
|
->setLastOrderId($this->magentoOrderId) |
534
|
|
|
->setLastRealOrderId($this->magentoOrder->getIncrementId()) |
535
|
|
|
->setLastQuoteId($this->quoteId) |
536
|
|
|
->setLastSuccessQuoteId($this->quoteId) |
537
|
|
|
->setLastOrderStatus($this->magentoOrder->getStatus()); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
//Magento status flow => https://docs.magento.com/m2/ce/user_guide/sales/order-status-workflow.html |
541
|
|
|
//Order Workflow => https://docs.magento.com/m2/ce/user_guide/sales/order-workflow.html |
542
|
|
|
$orderStatus = strtolower($this->magentoOrder->getStatus()); |
543
|
|
|
$acceptedStatus = array('processing', 'completed'); |
544
|
|
|
if (in_array($orderStatus, $acceptedStatus)) { |
545
|
|
|
if (isset($this->extraConfig['PMT_OK_URL']) && $this->extraConfig['PMT_OK_URL']!= '') { |
546
|
|
|
$returnUrl = $this->extraConfig['PMT_OK_URL']; |
547
|
|
|
} else { |
548
|
|
|
$returnUrl = 'checkout/onepage/success'; |
549
|
|
|
} |
550
|
|
|
} else { |
551
|
|
|
if (isset($this->extraConfig['PMT_KO_URL']) && $this->extraConfig['PMT_KO_URL'] != '') { |
552
|
|
|
$returnUrl = $this->extraConfig['PMT_KO_URL']; |
553
|
|
|
} else { |
554
|
|
|
//$returnUrl = 'checkout/#payment'; |
555
|
|
|
$returnUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
} |
559
|
|
|
return $returnUrl; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param $exceptionMessage |
564
|
|
|
* |
565
|
|
|
* @throws UnknownException |
566
|
|
|
*/ |
567
|
|
|
private function insertLog($exceptionMessage) |
568
|
|
|
{ |
569
|
|
|
try { |
570
|
|
|
if ($exceptionMessage instanceof \Exception) { |
571
|
|
|
$this->checkDbLogTable(); |
572
|
|
|
$logEntry = new LogEntry(); |
573
|
|
|
$logEntryJson = $logEntry->error($exceptionMessage)->toJson(); |
574
|
|
|
|
575
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
576
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
577
|
|
|
$tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
578
|
|
|
$dbConnection->insert($tableName, array('log' => $logEntryJson)); |
579
|
|
|
} |
580
|
|
|
} catch (\Exception $e) { |
581
|
|
|
throw new UnknownException($e->getMessage()); |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..