3
0

feat: refactor callback

This commit is contained in:
Ivan Sadovin 2026-05-19 18:13:11 +03:00 committed by Git Moneta
parent d57dba7f7c
commit 1e168ded8c
No known key found for this signature in database
GPG Key ID: 72A746BD7C9C5031
4 changed files with 85 additions and 90 deletions

View File

@ -4,7 +4,7 @@
![PHP](https://img.shields.io/badge/PHP-5.6%2B-purple)
![License](https://img.shields.io/badge/License-MIT-green)
> **📦 Версия модуля: 1.0.0**
> **📦 Версия модуля: 1.0.1**
> ** Совместимость версий OpenCart:**
> 3.0.0.0, 3.0.1.1, 3.0.1.2, 3.0.2.0, 3.0.3.0, 3.0.3.1, 3.0.3.2, 3.0.3.3, 3.0.3.4, 3.0.3.5, 3.0.3.6, 3.0.3.7, 3.0.3.8, 3.0.3.9, 3.0.4.0, 3.0.4.1, 3.0.5.0

View File

@ -2,7 +2,7 @@
<modification>
<name>PayAnyWay</name>
<code>payanyway</code>
<version>1.0.0</version>
<version>1.0.1</version>
<author>PayAnyWay</author>
<link>https://payanyway.ru/</link>
</modification>

View File

@ -9,7 +9,7 @@
class ControllerExtensionPaymentPayanyway extends Controller {
const CMS_NAME = 'OpenCart';
const MODULE_NAME = 'payanyway';
const MODULE_VERSION = '1.0.0';
const MODULE_VERSION = '1.0.1';
const MODULE_DEFAULT_SORT_ORDER = 0;
private $supportedCurrencies = array('RUB');

View File

@ -166,7 +166,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
/** @return string */
private function createTransactionId() {
$date = (new \DateTimeImmutable())->format('YmdHis');
$date = date('YmdHis');
return $this->orderId . self::TRANSACTION_ID_STRING_DELIMITER . $date;
}
@ -296,7 +296,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
return null;
}
$transactionIdData = explode(self::TRANSACTION_ID_STRING_DELIMITER, $_REQUEST['MNT_TRANSACTION_ID']);
$transactionIdData = explode(self::TRANSACTION_ID_STRING_DELIMITER, $_REQUEST['MNT_TRANSACTION_ID'], 2);
return isset($transactionIdData[0]) ? (int)$transactionIdData[0] : null;
}
@ -343,7 +343,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
? array_merge(['MNT_COMMAND'], $baseFields)
: $baseFields;
$signatureString = array_reduce($fields, static function ($carry, $field) use ($data) {
$signatureString = array_reduce($fields, function ($carry, $field) use ($data) {
return $carry . $data[$field];
}, '') . $this->integrityCode;
@ -360,59 +360,10 @@ class ControllerExtensionPaymentPayanyway extends Controller {
$this->sendResponse('FAIL');
}
$this->load->model('account/order');
$products = $this->model_account_order->getOrderProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
list($resultCode, $description) = $this->determineCheckResult($callbackData);
$responseData = $this->buildResponseData($callbackData, $resultCode, $description);
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
$xmlData = array(
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $callbackData['MNT_TRANSACTION_ID'],
'MNT_AMOUNT' => $this->formatPrice($orderTotal),
'MNT_CURRENCY_CODE' => $callbackData['MNT_CURRENCY_CODE'],
'inventory' => $this->getInventoryJson($products) ?: null,
'client' => $callbackData['MNT_SUBSCRIBER_ID'],
'sno' => null,
'delivery' => (null !== $deliveryPrice) ? $this->formatPrice($deliveryPrice) : null,
);
$orderStatusId = (int)(isset($this->orderInfo['order_status_id']) ? $this->orderInfo['order_status_id'] : 1);
list($xmlData['MNT_RESULT_CODE'], $xmlData['MNT_DESCRIPTION']) = $this->determineCheckResult($callbackData, $orderStatusId);
$xmlData['MNT_SIGNATURE'] = md5(
$xmlData['MNT_RESULT_CODE'] .
$xmlData['MNT_ID'] .
$xmlData['MNT_TRANSACTION_ID'] .
$this->integrityCode
);
$this->sendResponse($this->buildXMLResponse($xmlData));
}
/**
* @param array<string, mixed> $callbackData
* @param int $orderStatusId
* @return array
* @throws \Exception
*/
private function determineCheckResult(array $callbackData, $orderStatusId) {
$orderStatus = $this->getOrderStatusById($orderStatusId);
if (empty($callbackData['MNT_AMOUNT'])) {
return array(100, "Order status is '{$orderStatus}'");
}
if ($this->successOrderStatusId === $orderStatusId) {
return array(200, 'Order Paid');
}
if (in_array($orderStatusId, array(7, 9, 16), true)) {
return array(500, "Order status is '{$orderStatus}'");
}
return array(402, 'Order created, but not paid');
$this->sendResponse($this->buildXMLResponse($responseData));
}
/**
@ -422,7 +373,6 @@ class ControllerExtensionPaymentPayanyway extends Controller {
*/
private function handlePayCallback(array $callbackData) {
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
$currencyCode = isset($this->orderInfo['currency_code']) ? $this->orderInfo['currency_code'] : 'RUB';
$shopData = array(
'MNT_ID' => $this->mntId,
@ -439,33 +389,77 @@ class ControllerExtensionPaymentPayanyway extends Controller {
}
$isPayedOrder = $this->isOrderPaid();
$resultCode = 200;
$this->load->model('account/order');
$products = $this->model_account_order->getOrderProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
$xmlData = array(
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $callbackData['MNT_TRANSACTION_ID'],
'MNT_RESULT_CODE' => $resultCode,
'MNT_SIGNATURE' => md5($resultCode . $this->mntId . $callbackData['MNT_TRANSACTION_ID'] . $this->integrityCode),
'MNT_AMOUNT' => $callbackData['MNT_AMOUNT'],
'MNT_CURRENCY_CODE' => $callbackData['MNT_CURRENCY_CODE'],
'MNT_DESCRIPTION' => $isPayedOrder ? 'Order already paid' : 'Order success paid',
'inventory' => $this->getInventoryJson($products),
'client' => $callbackData['MNT_SUBSCRIBER_ID'],
'sno' => null,
'delivery' => $deliveryPrice !== null ? $this->formatPrice($deliveryPrice) : null,
);
if (!$isPayedOrder) {
$this->load->language('extension/payment/payanyway');
$this->addOrderHistory($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($callbackData, $resultCode, $description);
$this->sendResponse($this->buildXMLResponse($responseData));
}
/**
* @param array $callbackData
* @param int $resultCode
* @param string $description
* @return array<string, mixed>
* @throws \Exception
*/
private function buildResponseData(array $callbackData, $resultCode, $description) {
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
$data = array(
'MNT_ID' => $this->mntId,
'MNT_TRANSACTION_ID' => $callbackData['MNT_TRANSACTION_ID'],
'MNT_RESULT_CODE' => $resultCode,
'MNT_DESCRIPTION' => $description,
'MNT_AMOUNT' => $this->formatPrice($orderTotal),
'MNT_CURRENCY_CODE' => $callbackData['MNT_CURRENCY_CODE'],
);
$signatureSource = $resultCode . $this->mntId . $callbackData['MNT_TRANSACTION_ID'] . $this->integrityCode;
$data['MNT_SIGNATURE'] = md5($signatureSource);
$this->load->model('account/order');
$products = $this->model_account_order->getOrderProducts($this->orderId);
$deliveryPrice = $this->getDeliveryPrice();
$data['inventory'] = $this->getInventoryJson($products) ?: null;
$data['client'] = $callbackData['MNT_SUBSCRIBER_ID'];
$data['sno'] = null;
$data['delivery'] = (null !== $deliveryPrice) ? $this->formatPrice($deliveryPrice) : null;
return $data;
}
/**
* @param array<string, mixed> $callbackData
* @return array<int|string>
* @throws \Exception
*/
private function determineCheckResult(array $callbackData) {
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
if (null === $orderStatusId) {
return array(500, 'Order status not set');
}
$orderStatus = $this->getOrderStatusById($orderStatusId);
if (empty($callbackData['MNT_AMOUNT'])) {
return array(100, "Order status is '{$orderStatus}'");
}
if ($this->successOrderStatusId === $orderStatusId) {
return array(200, 'Order Paid');
}
if (in_array($orderStatusId, array(7, 9, 16), true)) {
return array(500, "Order status is '{$orderStatus}'");
}
return array(402, 'Order created, but not paid');
}
/** @return bool */
@ -498,11 +492,15 @@ class ControllerExtensionPaymentPayanyway extends Controller {
* @return false|string
*/
private function getInventoryJson(array $products) {
if (empty($products)) {
return false;
}
$inventory = array();
foreach ($products as $product) {
$name = isset($product['name']) ? $product['name'] : '';
$price = isset($product['price']) ? (float)$product['price'] : 0;
$quantity = isset($product['quantity']) ? (string)$product['quantity'] : '0';
$quantity = isset($product['quantity']) ? (float)$product['quantity'] : 0;
$inventory[] = array(
'name' => $this->validateString($name, self::INVENTORY_ITEM_NAME_MAX_LENGTH),
@ -541,16 +539,12 @@ class ControllerExtensionPaymentPayanyway extends Controller {
}
/**
* @param string $quantity
* @param float $quantity
* @return string
*/
private function formatQuantity($quantity) {
$quantity = str_replace(',', '.', $quantity);
if (strpos($quantity, '.') === false && ctype_digit($quantity)) {
return (string)(int)$quantity;
}
return number_format((float)$quantity, 3, '.', '');
$quantityString = number_format($quantity, 3, '.', '');
return rtrim(rtrim($quantityString, '0'), '.');
}
/**
@ -601,7 +595,8 @@ class ControllerExtensionPaymentPayanyway extends Controller {
}
}
return $dom->saveXML();
$xml = $dom->saveXML();
return (false !== $xml) ? $xml : 'FAIL';
}
/**