-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.php
65 lines (53 loc) · 2.33 KB
/
cron.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
<?php
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
include dirname(__FILE__) . '/../../config/config.inc.php';
include dirname(__FILE__) . '/sdk/LightningClient.php';
include dirname(__FILE__) . '/classes/PeachCommerceSql.php';
include dirname(__FILE__) . '/peachcommerce.php';
$defToken = Tools::substr(Tools::hash('peachCommerce/cron'), 0, 10);
$moduleManagerBuilder = ModuleManagerBuilder::getInstance();
$moduleManager = $moduleManagerBuilder->build();
if ($defToken !== Tools::getValue('token') || !$moduleManager->isInstalled(PeachCommerce::NAME)) {
die('Bad token');
}
$config = Configuration::getMultiple(array(PeachCommerce::HOST, PeachCommerce::MERCHANT_ID));
if (!empty($config[PeachCommerce::HOST]) && !empty($config[PeachCommerce::MERCHANT_ID])) {
$api = new \LightningHub\Hub\LightningClient($config[PeachCommerce::HOST], $config[PeachCommerce::MERCHANT_ID]);
} else {
$api = null;
}
$waitStatus = (int)Configuration::get(PeachCommerce::OS_WAITING);
$canceledStatus = (int)Configuration::get('PS_OS_CANCELED');
$payedStatus = (int)Configuration::get('PS_OS_PAYMENT');
$sql = new DbQuery();
$sql->select('o.id_order, o.current_state, l.r_hash, l.creation_time, l.expiry');
$sql->leftJoin('peach_commerce', 'l', 'o.id_order = l.order_id');
$sql->from('orders', 'o');
$sql->where('o.module = "' . pSQL(PeachCommerce::NAME) . '"');
$sql->where('o.current_state = ' . pSQL($waitStatus));
$orders = Db::getInstance()->executeS($sql);
if (empty($orders)) {
die(1);
}
try {
foreach ($orders as $order) {
$prestaOrder = new Order($order['id_order']);
$expiryAt = (int)$order['creation_time'] + (int)$order['expiry'];
$now = new \DateTime();
if ($expiryAt <= $now->getTimestamp()) {
$prestaOrder->setCurrentState($canceledStatus);
$prestaOrder->save();
} elseif ($order['current_state'] === $waitStatus && !empty($api)) {
$invoice = $api->fetch($order['r_hash']);
if ($invoice && $invoice->settled) {
$prestaOrder->setCurrentState($payedStatus);
$prestaOrder->save();
$peachOrder = PeachCommerceSql::loadByOrderId($prestaOrder->id_order);
$peachOrder->settled = true;
$peachOrder->save();
}
}
}
} catch (Exception $ex) {
die($ex);
}