-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProcessPendingOrders.php
146 lines (128 loc) · 5.28 KB
/
ProcessPendingOrders.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
<?php
/**
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2024 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class ProcessPendingOrders
{
public function __construct()
{
$this->loadConfigurationsForRequest();
$this->execute();
}
public function execute()
{
$ordersToProcess = $this->getPendingOrders();
foreach ($ordersToProcess as $orderId) {
$order = new Order($orderId);
if (_PS_VERSION_ >= '8') {
$orderPayments = $order->getOrderPayments();
} else {
$orderPayments = OrderPayment::getByOrderId($orderId);
}
$transactionId = $orderPayments[0]->transaction_id;
if (empty($transactionId)) {
continue;
}
$payment = \SatispayGBusiness\Payment::get($transactionId);
$orderId = Order::getOrderByCartId($payment->metadata->cart_id);
if ($order->current_state == (int)(Configuration::get('SATISPAY_PENDING_STATE'))) {
$history = new OrderHistory();
$history->id_order = (int)$orderId;
if ($payment->status === 'ACCEPTED') {
//using existing payment so it's not doubled
$history->changeIdOrderState((int)(Configuration::get('PS_OS_PAYMENT')), $orderId, true);
$orderMessage = new Message();
$orderMessage->id_order = (int)$orderId;
$orderMessage->message = 'The following order has been finalized by cron.';
$orderMessage->private = true;
$orderMessage->save();
$history->save();
}
if ($payment->status === 'CANCELED') {
$order->setCurrentState((int)(Configuration::get('PS_OS_CANCELED')));
$order->save();
}
}
}
exit;
}
public function loadConfigurationsForRequest()
{
$module = \Module::getInstanceByName('satispay');
$currentSandbox = Configuration::get('SATISPAY_SANDBOX', false);
$currentKeyId = Configuration::get('SATISPAY_KEY_ID', '');
$currentPrivateKey = Configuration::get('SATISPAY_PRIVATE_KEY', '');
$currentPublicKey = Configuration::get('SATISPAY_PUBLIC_KEY', '');
\SatispayGBusiness\Api::setSandbox($currentSandbox);
\SatispayGBusiness\Api::setKeyId($currentKeyId);
\SatispayGBusiness\Api::setPrivateKey($currentPrivateKey);
\SatispayGBusiness\Api::setPublicKey($currentPublicKey);
\SatispayGBusiness\Api::setPluginNameHeader('PrestaShop');
\SatispayGBusiness\Api::setPluginVersionHeader($module->version);
\SatispayGBusiness\Api::setPlatformVersionHeader(_PS_VERSION_);
\SatispayGBusiness\Api::setTypeHeader('ECOMMERCE-PLUGIN');
}
/**
* Get the start criteria for the scheduled datetime
*/
private function getStartDateScheduledTime()
{
$now = new \DateTime();
$scheduledTimeFrame = (int)(Configuration::get('SATISPAY_UNPROCESSED_TIME'));
if (!($scheduledTimeFrame)) {
$scheduledTimeFrame = Satispay::SATISPAY_DEFAULT_UNPROCESSED_TIME;
}
$tosub = new \DateInterval('PT'. $scheduledTimeFrame . 'H');
return $now->sub($tosub)->format('Y-m-d H:i:s');
}
/**
* Get the end criteria for the scheduled datetime
*/
private function getEndDateScheduledTime()
{
$now = new \DateTime();
// remove just 1 hour so normal transactions can still be processed
$tosub = new \DateInterval('PT'. 1 . 'H');
return $now->sub($tosub)->format('Y-m-d H:i:s');
}
private function getPendingOrders()
{
$rangeStart = $this->getStartDateScheduledTime();
$rangeEnd = $this->getEndDateScheduledTime();
$sql = 'SELECT id_order
FROM ' . _DB_PREFIX_ . 'orders
WHERE current_state = ' . (int)(Configuration::get('SATISPAY_PENDING_STATE')) . '
AND date_upd <= \'' . pSQL($rangeEnd) . '\' AND date_upd >= \'' . pSQL($rangeStart) . '\'
ORDER BY invoice_date ASC';
$result = Db::getInstance()->executeS($sql);
$ordersToProcess = [];
foreach ($result as $data) {
$ordersToProcess[] = (int) $data['id_order'];
}
return $ordersToProcess;
}
}