3
0

feat: refactor callback

This commit is contained in:
Ivan Sadovin 2026-05-19 18:21:39 +03:00 committed by Git Moneta
parent 23f73da573
commit 36838d136a
No known key found for this signature in database
GPG Key ID: 72A746BD7C9C5031
5 changed files with 63 additions and 63 deletions

View File

@ -5,7 +5,7 @@
![PHP](https://img.shields.io/badge/PHP-8.0%2B-purple)
![License](https://img.shields.io/badge/License-MIT-green)
> **📦 Версия модуля: 1.0.0**
> **📦 Версия модуля: 1.0.1**
> ** Совместимость версий OpenCart:**
> 4.0.0.0, 4.0.1.0, 4.0.1.1, 4.0.2.0, 4.0.2.1, 4.0.2.2, 4.0.2.3, 4.1.0.0, 4.1.0.1, 4.1.0.2, 4.1.0.3

View File

@ -13,7 +13,7 @@ class PayAnyWay extends \Opencart\System\Engine\Controller
{
public const CMS_NAME = 'OpenCart';
public const MODULE_NAME = 'payanyway';
public const MODULE_VERSION = '1.0.0';
public const MODULE_VERSION = '1.0.1';
public const MODULE_DEFAULT_SORT_ORDER = 0;
private const SUPPORTED_CURRENCIES = ['RUB'];

View File

@ -92,39 +92,21 @@ class Callback extends \Opencart\System\Engine\Controller
$this->sendResponse('FAIL');
}
$this->load->model('account/order');
$products = $this->model_account_order->getProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
[$resultCode, $description] = $this->determineCheckResult();
$responseData = $this->buildResponseData($resultCode, $description);
$xmlData = [
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
'MNT_AMOUNT' => $this->formatPrice((float)($this->orderInfo['total'] ?? 0)),
'MNT_CURRENCY_CODE' => $this->callbackData['MNT_CURRENCY_CODE'],
'inventory' => $this->getInventoryJson($products) ?: null,
'client' => $this->callbackData['MNT_SUBSCRIBER_ID'],
'sno' => null,
'delivery' => (null !== $deliveryPrice) ? $this->formatPrice($deliveryPrice) : null,
];
[$xmlData['MNT_RESULT_CODE'], $xmlData['MNT_DESCRIPTION']] = $this->determineCheckResult();
$xmlData['MNT_SIGNATURE'] = md5(
$xmlData['MNT_RESULT_CODE'] .
$xmlData['MNT_ID'] .
$xmlData['MNT_TRANSACTION_ID'] .
$this->integrityCode,
);
$this->sendResponse($this->buildXMLResponse($xmlData));
$this->sendResponse($this->buildXMLResponse($responseData));
}
/** @throws \Exception */
/**
* @return array<int|string>
* @throws \Exception
*/
private function determineCheckResult(): array
{
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
if (null === $orderStatusId) {
return [500, "Order status not set"];
return [500, 'Order status not set'];
}
$orderStatus = $this->getOrderStatusById($orderStatusId);
@ -150,8 +132,8 @@ class Callback extends \Opencart\System\Engine\Controller
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
'MNT_OPERATION_ID' => $this->callbackData['MNT_OPERATION_ID'],
'MNT_AMOUNT' => $this->formatPrice((float)$this->orderInfo['total']),
'MNT_CURRENCY_CODE' => $this->orderInfo['currency_code'],
'MNT_AMOUNT' => $this->formatPrice((float)($this->orderInfo['total'] ?? 0)),
'MNT_CURRENCY_CODE' => $this->orderInfo['currency_code'] ?? 'RUB',
'MNT_SUBSCRIBER_ID' => $this->callbackData['MNT_SUBSCRIBER_ID'],
'MNT_TEST_MODE' => $this->callbackData['MNT_TEST_MODE'],
];
@ -161,32 +143,50 @@ class Callback extends \Opencart\System\Engine\Controller
}
$isPayedOrder = $this->isOrderPaid();
$resultCode = 200;
$this->load->model('account/order');
$products = $this->model_account_order->getProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
$xmlData = [
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
'MNT_RESULT_CODE' => $resultCode,
'MNT_SIGNATURE' => md5($resultCode . $this->mntId . $this->callbackData['MNT_TRANSACTION_ID'] . $this->integrityCode),
'MNT_AMOUNT' => $this->callbackData['MNT_AMOUNT'],
'MNT_CURRENCY_CODE' => $this->callbackData['MNT_CURRENCY_CODE'],
'MNT_DESCRIPTION' => $isPayedOrder ? 'Order already paid' : 'Order success paid',
'inventory' => $this->getInventoryJson($products),
'client' => $this->callbackData['MNT_SUBSCRIBER_ID'],
'sno' => null,
'delivery' => $deliveryPrice !== null ? $this->formatPrice($deliveryPrice) : null,
];
if (!$isPayedOrder) {
$this->load->language('extension/payanyway/payment/payanyway');
$this->addOrderHistory($this->orderId, $this->successOrderStatusId, $this->language->get('text_payment_completed'),);
}
$this->sendResponse($this->buildXMLResponse($xmlData));
$resultCode = 200;
$description = $isPayedOrder ? 'Order already paid' : 'Order success paid';
$responseData = $this->buildResponseData($resultCode, $description);
$this->sendResponse($this->buildXMLResponse($responseData));
}
/**
* @param int $resultCode
* @param string $description
* @return array<string, mixed>
* @throws \Exception
*/
private function buildResponseData(int $resultCode, string $description): array
{
$orderTotal = (float)($this->orderInfo['total'] ?? 0);
$data = [
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
'MNT_RESULT_CODE' => $resultCode,
'MNT_DESCRIPTION' => $description,
'MNT_AMOUNT' => $this->formatPrice($orderTotal),
'MNT_CURRENCY_CODE' => $this->callbackData['MNT_CURRENCY_CODE'],
];
$signatureSource = $resultCode . $this->mntId . $this->callbackData['MNT_TRANSACTION_ID'] . $this->integrityCode;
$data['MNT_SIGNATURE'] = md5($signatureSource);
$this->load->model('account/order');
$products = $this->model_account_order->getProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
$data['inventory'] = $this->getInventoryJson($products) ?: null;
$data['client'] = $this->callbackData['MNT_SUBSCRIBER_ID'];
$data['sno'] = null;
$data['delivery'] = (null !== $deliveryPrice) ? $this->formatPrice($deliveryPrice) : null;
return $data;
}
private function isOrderPaid(): bool
@ -237,12 +237,16 @@ class Callback extends \Opencart\System\Engine\Controller
*/
private function getInventoryJson(array $products): false|string
{
if (empty($products)) {
return false;
}
$inventory = [];
foreach ($products as $product) {
$inventory[] = [
'name' => $this->validateString($product['name'] ?? '', self::INVENTORY_ITEM_NAME_MAX_LENGTH),
'price' => $this->formatPrice((float)($product['price'] ?? 0)),
'quantity' => $this->formatQuantity((string)($product['quantity'] ?? '0')),
'quantity' => $this->formatQuantity((float)($product['quantity'] ?? 0)),
'vatTag' => $this->vatRate,
'pm' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_METHOD,
'po' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_OBJECT,
@ -276,15 +280,10 @@ class Callback extends \Opencart\System\Engine\Controller
return $orderStatus;
}
private function formatQuantity(string $quantity): string
private function formatQuantity(float $quantity): string
{
$quantity = str_replace(',', '.', $quantity);
if (!str_contains($quantity, '.') && ctype_digit($quantity)) {
return (string)(int)$quantity;
}
return number_format((float)$quantity, 3, '.', '');
$quantityString = number_format($quantity, 3, '.', '');
return rtrim(rtrim($quantityString, '0'), '.');
}
/**
@ -334,7 +333,8 @@ class Callback extends \Opencart\System\Engine\Controller
}
}
return $dom->saveXML();
$xml = $dom->saveXML();
return (false !== $xml) ? $xml : 'FAIL';
}
/** @param array<string, mixed> $params */

View File

@ -66,7 +66,7 @@ trait PayAnyWay
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
return null;
}
$transactionIdData = explode($this->transactionIdDelimiter, $_REQUEST['MNT_TRANSACTION_ID']);
$transactionIdData = explode($this->transactionIdDelimiter, $_REQUEST['MNT_TRANSACTION_ID'], 2);
return isset($transactionIdData[0]) ? (int)$transactionIdData[0] : null;
}

View File

@ -1,7 +1,7 @@
{
"code": "payanyway",
"name": "PayAnyWay",
"version": "1.0.0",
"version": "1.0.1",
"author": "PayAnyWay",
"link": "https://www.payanyway.ru/"
}