-
Notifications
You must be signed in to change notification settings - Fork 7
/
CancelCheckoutCredits.php
131 lines (119 loc) · 3.99 KB
/
CancelCheckoutCredits.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
<?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\ConfigCheckoutCredits;
use MercadoPago\AdbPayment\Model\Console\Command\Notification\FetchStatus;
use MercadoPago\AdbPayment\Cron\CancelCheckoutPro as CancelCheckoutPro;
/**
* CronTab for cancel Checkout Credits.
*/
class CancelCheckoutCredits extends CancelCheckoutPro
{
/**
* @var Logger
*/
protected $logger;
/**
* @var FetchStatus
*/
protected $fetchStatus;
/**
* @var ConfigCheckoutCredits
*/
protected $configCheckoutCredits;
/**
* @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,
ConfigCheckoutCredits $configCheckoutCredits,
CollectionFactory $collectionFactory,
ResourceConnection $resource
) {
$this->logger = $logger;
$this->fetchStatus = $fetchStatus;
$this->configCheckoutCredits = $configCheckoutCredits;
$this->collectionFactory = $collectionFactory;
$this->resource = $resource;
}
/**
* 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']
)
->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"
), ConfigCheckoutCredits::METHOD);
foreach ($orders as $order) {
$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();
$this->logger->debug([
'fetch' => 'Cancel Order Id ' . $orderId,
]);
}
}
}