-
Notifications
You must be signed in to change notification settings - Fork 7
/
CancelCheckoutPro.php
187 lines (165 loc) · 5.42 KB
/
CancelCheckoutPro.php
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
<?php
/**
* Copyright © MercadoPago. All rights reserved.
*
* @author Mercado Pago
* @license See LICENSE for license details.
*/
namespace MercadoPago\AdbPayment\Cron;
use Magento\Framework\App\ResourceConnection;
use Magento\Payment\Model\Method\Logger;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use MercadoPago\AdbPayment\Gateway\Config\ConfigCheckoutPro;
use MercadoPago\AdbPayment\Model\Console\Command\Notification\FetchStatus;
/**
* CronTab for cancel Checkout Pro.
*/
class CancelCheckoutPro
{
/**
* @var Logger
*/
protected $logger;
/**
* @var FetchStatus
*/
protected $fetchStatus;
/**
* @var ConfigCheckoutPro
*/
protected $configCheckoutPro;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var ResourceConnection
*/
protected $resource;
/**
* Constructor.
*
* @param Logger $logger
* @param FetchStatus $fetchStatus
* @param ConfigCheckoutPro $configCheckoutPro
* @param CollectionFactory $collectionFactory
* @param ResourceConnection $resource;
*/
public function __construct(
Logger $logger,
FetchStatus $fetchStatus,
ConfigCheckoutPro $configCheckoutPro,
CollectionFactory $collectionFactory,
ResourceConnection $resource
) {
$this->logger = $logger;
$this->fetchStatus = $fetchStatus;
$this->configCheckoutPro = $configCheckoutPro;
$this->collectionFactory = $collectionFactory;
$this->resource = $resource;
}
/**
* Get sales_order_payment table name.
*
* @return string
*/
public function getSalesOrderPaymentTableName()
{
return $this->resource->getTableName('sales_order_payment');
}
/**
* Execute the cron.
*
* @return void
*/
public function execute()
{
$orders = $this->collectionFactory->create()
->addFieldToFilter('state', Order::STATE_NEW);
$orders->getSelect()
->join(
['sop' => $this->getSalesOrderPaymentTableName()],
'main_table.entity_id = sop.parent_id',
['method', 'additional_information']
)
->where(new \Zend_Db_Expr(
"sop.method = ?
AND TIME_TO_SEC(
TIMEDIFF(CURRENT_TIMESTAMP(),
STR_TO_DATE(
REPLACE(
SUBSTRING_INDEX(
JSON_UNQUOTE(JSON_EXTRACT(sop.additional_information, '$.date_of_expiration')),
'.',
1
),
'T', ' '
),
'%Y-%m-%d %H:%i:%s'
)
)
) >= 0"
), ConfigCheckoutPro::METHOD);
$this->logger->debug([
'Total orders to cancel: ' => $orders->count()
]);
$dateRange = [];
$orderId = null;
try {
$this->logger->debug([
'Cancel Start'
]);
foreach ($orders as $order) {
$orderAdditionalInformation = json_decode($order->getData('additional_information'));
$orderId = $order->getEntityId();
$amount = $order->getTotalDue();
$baseAmount = $order->getBaseTotalDue();
$payment = $order->getPayment();
$payment->setPreparedMessage(__('Order Canceled.'));
$payment->registerVoidNotification($amount);
$payment->setIsTransactionApproved(false);
$payment->setIsTransactionDenied(true);
$payment->setIsTransactionPending(false);
$payment->setIsInProcess(true);
$payment->setIsTransactionClosed(true);
$payment->setShouldCloseParentTransaction(true);
$payment->setAmountCanceled($amount);
$payment->setBaseAmountCanceled($baseAmount);
$order->cancel();
$order->save();
$dateRange[] = $orderAdditionalInformation->date_of_expiration;
$this->logger->debug([
'fetch' => 'Cancel Order ' . $order->getIncrementId() . ' successfully',
'order_date_of_expiration' => $orderAdditionalInformation->date_of_expiration
]);
}
$this->calculateExpirationRange($dateRange);
$this->logger->debug([
'Cancel Finish'
]);
} catch (\Exception $e) {
$this->logger->debug([
'Cancel order execution error' => $e->getMessage(),
'Order Id' => $orderId
]);
}
}
/**
* Calculate expiration range date
*
* @param array $dateRange
* @return void
*/
private function calculateExpirationRange(array $dateRange): void
{
usort($dateRange, function ($a, $b) {
return strtotime($a) - strtotime($b);
});
$initialDate = reset($dateRange);
$endDate = end($dateRange);
$this->logger->debug([
'Orders expiration range: ' => "from: $initialDate to: $endDate"
]);
}
}