107 lines
3.1 KiB
PHP
Executable File
107 lines
3.1 KiB
PHP
Executable File
<?php
|
|
|
|
class ModelExtensionPaymentPayanyway extends Model {
|
|
/**
|
|
* @param array<string, mixed> $address
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getMethod(array $address) {
|
|
$this->load->language('extension/payment/payanyway');
|
|
|
|
if (!$this->isModuleEnabled() ||
|
|
!$this->isCurrencySupported() ||
|
|
!$this->isGeoZoneValid($address)
|
|
) {
|
|
return array();
|
|
}
|
|
|
|
return array(
|
|
'code' => 'payanyway',
|
|
'title' => $this->language->get('text_title'),
|
|
'terms' => '',
|
|
'sort_order' => $this->config->get('payment_payanyway_sort_order'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Блокировка заказа на время Pay URL (GET_LOCK MySQL/MariaDB): два уведомления подряд
|
|
* не обрабатываются параллельно. Снимается releaseOrderLock() или с закрытием соединения.
|
|
*
|
|
* @param int $orderId
|
|
* @param int $timeoutSeconds
|
|
* @return bool
|
|
*/
|
|
public function acquireOrderLock($orderId, $timeoutSeconds) {
|
|
$query = $this->db->query("SELECT GET_LOCK('" . $this->db->escape($this->getOrderLockName($orderId)) . "', " . (int)$timeoutSeconds . ") AS acquired");
|
|
|
|
return isset($query->row['acquired']) && (1 === (int)$query->row['acquired']);
|
|
}
|
|
|
|
/**
|
|
* @param int $orderId
|
|
* @return void
|
|
*/
|
|
public function releaseOrderLock($orderId) {
|
|
$this->db->query("SELECT RELEASE_LOCK('" . $this->db->escape($this->getOrderLockName($orderId)) . "')");
|
|
}
|
|
|
|
/**
|
|
* Заказ когда-либо получал этот статус — по истории заказа.
|
|
*
|
|
* @param int $orderId
|
|
* @param int $orderStatusId
|
|
* @return bool
|
|
*/
|
|
public function hasOrderStatusInHistory($orderId, $orderStatusId) {
|
|
$query = $this->db->query("SELECT 1 FROM `" . DB_PREFIX . "order_history`
|
|
WHERE `order_id` = '" . (int)$orderId . "'
|
|
AND `order_status_id` = '" . (int)$orderStatusId . "'
|
|
LIMIT 1");
|
|
|
|
return $query->num_rows > 0;
|
|
}
|
|
|
|
/**
|
|
* Имя блокировки — не длиннее 64 символов и своё для каждой БД магазина.
|
|
*
|
|
* @param int $orderId
|
|
* @return string
|
|
*/
|
|
private function getOrderLockName($orderId) {
|
|
return 'payanyway_' . substr(md5(DB_DATABASE . '.' . DB_PREFIX), 0, 16) . '_' . (int)$orderId;
|
|
}
|
|
|
|
/** @return bool */
|
|
private function isModuleEnabled() {
|
|
return (bool)$this->config->get('payment_payanyway_status');
|
|
}
|
|
|
|
/** @return bool */
|
|
private function isCurrencySupported() {
|
|
$shopCurrency = isset($this->session->data['currency']) ? $this->session->data['currency'] : '';
|
|
|
|
return $shopCurrency === $this->config->get('payment_payanyway_currency');
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $address
|
|
* @return bool
|
|
*/
|
|
private function isGeoZoneValid(array $address) {
|
|
$geoZoneId = (int)$this->config->get('payment_payanyway_geo_zone_id');
|
|
|
|
if ($geoZoneId === 0) {
|
|
return true;
|
|
}
|
|
|
|
$query = $this->db->query(
|
|
"SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone
|
|
WHERE geo_zone_id = '" . $geoZoneId . "'
|
|
AND country_id = '" . (int)$address['country_id'] . "'
|
|
AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"
|
|
);
|
|
|
|
return $query->num_rows > 0;
|
|
}
|
|
}
|