feat: refactor callback
This commit is contained in:
parent
23f73da573
commit
36838d136a
@ -5,7 +5,7 @@
|
|||||||

|

|
||||||

|

|
||||||
|
|
||||||
> **📦 Версия модуля: 1.0.0**
|
> **📦 Версия модуля: 1.0.1**
|
||||||
|
|
||||||
> **ℹ️ Совместимость версий OpenCart:**
|
> **ℹ️ Совместимость версий 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
|
> 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
|
||||||
|
|||||||
@ -13,7 +13,7 @@ class PayAnyWay extends \Opencart\System\Engine\Controller
|
|||||||
{
|
{
|
||||||
public const CMS_NAME = 'OpenCart';
|
public const CMS_NAME = 'OpenCart';
|
||||||
public const MODULE_NAME = 'payanyway';
|
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;
|
public const MODULE_DEFAULT_SORT_ORDER = 0;
|
||||||
|
|
||||||
private const SUPPORTED_CURRENCIES = ['RUB'];
|
private const SUPPORTED_CURRENCIES = ['RUB'];
|
||||||
|
|||||||
@ -92,39 +92,21 @@ class Callback extends \Opencart\System\Engine\Controller
|
|||||||
$this->sendResponse('FAIL');
|
$this->sendResponse('FAIL');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->load->model('account/order');
|
[$resultCode, $description] = $this->determineCheckResult();
|
||||||
$products = $this->model_account_order->getProducts($this->orderId);
|
$responseData = $this->buildResponseData($resultCode, $description);
|
||||||
$deliveryPrice = $this->getDeliveryPrice();
|
|
||||||
|
|
||||||
$xmlData = [
|
$this->sendResponse($this->buildXMLResponse($responseData));
|
||||||
'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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @throws \Exception */
|
/**
|
||||||
|
* @return array<int|string>
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
private function determineCheckResult(): array
|
private function determineCheckResult(): array
|
||||||
{
|
{
|
||||||
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
|
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
|
||||||
if (null === $orderStatusId) {
|
if (null === $orderStatusId) {
|
||||||
return [500, "Order status not set"];
|
return [500, 'Order status not set'];
|
||||||
}
|
}
|
||||||
$orderStatus = $this->getOrderStatusById($orderStatusId);
|
$orderStatus = $this->getOrderStatusById($orderStatusId);
|
||||||
|
|
||||||
@ -150,8 +132,8 @@ class Callback extends \Opencart\System\Engine\Controller
|
|||||||
'MNT_ID' => $this->mntId,
|
'MNT_ID' => $this->mntId,
|
||||||
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
|
'MNT_TRANSACTION_ID' => $this->callbackData['MNT_TRANSACTION_ID'],
|
||||||
'MNT_OPERATION_ID' => $this->callbackData['MNT_OPERATION_ID'],
|
'MNT_OPERATION_ID' => $this->callbackData['MNT_OPERATION_ID'],
|
||||||
'MNT_AMOUNT' => $this->formatPrice((float)$this->orderInfo['total']),
|
'MNT_AMOUNT' => $this->formatPrice((float)($this->orderInfo['total'] ?? 0)),
|
||||||
'MNT_CURRENCY_CODE' => $this->orderInfo['currency_code'],
|
'MNT_CURRENCY_CODE' => $this->orderInfo['currency_code'] ?? 'RUB',
|
||||||
'MNT_SUBSCRIBER_ID' => $this->callbackData['MNT_SUBSCRIBER_ID'],
|
'MNT_SUBSCRIBER_ID' => $this->callbackData['MNT_SUBSCRIBER_ID'],
|
||||||
'MNT_TEST_MODE' => $this->callbackData['MNT_TEST_MODE'],
|
'MNT_TEST_MODE' => $this->callbackData['MNT_TEST_MODE'],
|
||||||
];
|
];
|
||||||
@ -161,32 +143,50 @@ class Callback extends \Opencart\System\Engine\Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$isPayedOrder = $this->isOrderPaid();
|
$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) {
|
if (!$isPayedOrder) {
|
||||||
$this->load->language('extension/payanyway/payment/payanyway');
|
$this->load->language('extension/payanyway/payment/payanyway');
|
||||||
$this->addOrderHistory($this->orderId, $this->successOrderStatusId, $this->language->get('text_payment_completed'),);
|
$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
|
private function isOrderPaid(): bool
|
||||||
@ -237,12 +237,16 @@ class Callback extends \Opencart\System\Engine\Controller
|
|||||||
*/
|
*/
|
||||||
private function getInventoryJson(array $products): false|string
|
private function getInventoryJson(array $products): false|string
|
||||||
{
|
{
|
||||||
|
if (empty($products)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$inventory = [];
|
$inventory = [];
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
$inventory[] = [
|
$inventory[] = [
|
||||||
'name' => $this->validateString($product['name'] ?? '', self::INVENTORY_ITEM_NAME_MAX_LENGTH),
|
'name' => $this->validateString($product['name'] ?? '', self::INVENTORY_ITEM_NAME_MAX_LENGTH),
|
||||||
'price' => $this->formatPrice((float)($product['price'] ?? 0)),
|
'price' => $this->formatPrice((float)($product['price'] ?? 0)),
|
||||||
'quantity' => $this->formatQuantity((string)($product['quantity'] ?? '0')),
|
'quantity' => $this->formatQuantity((float)($product['quantity'] ?? 0)),
|
||||||
'vatTag' => $this->vatRate,
|
'vatTag' => $this->vatRate,
|
||||||
'pm' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_METHOD,
|
'pm' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_METHOD,
|
||||||
'po' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_OBJECT,
|
'po' => self::INVENTORY_ITEM_DEFAULT_PAYMENT_OBJECT,
|
||||||
@ -276,15 +280,10 @@ class Callback extends \Opencart\System\Engine\Controller
|
|||||||
return $orderStatus;
|
return $orderStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatQuantity(string $quantity): string
|
private function formatQuantity(float $quantity): string
|
||||||
{
|
{
|
||||||
$quantity = str_replace(',', '.', $quantity);
|
$quantityString = number_format($quantity, 3, '.', '');
|
||||||
|
return rtrim(rtrim($quantityString, '0'), '.');
|
||||||
if (!str_contains($quantity, '.') && ctype_digit($quantity)) {
|
|
||||||
return (string)(int)$quantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return number_format((float)$quantity, 3, '.', '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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 */
|
/** @param array<string, mixed> $params */
|
||||||
|
|||||||
@ -66,7 +66,7 @@ trait PayAnyWay
|
|||||||
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
|
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
|
||||||
return null;
|
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;
|
return isset($transactionIdData[0]) ? (int)$transactionIdData[0] : null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"code": "payanyway",
|
"code": "payanyway",
|
||||||
"name": "PayAnyWay",
|
"name": "PayAnyWay",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"author": "PayAnyWay",
|
"author": "PayAnyWay",
|
||||||
"link": "https://www.payanyway.ru/"
|
"link": "https://www.payanyway.ru/"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user