diff --git a/README.md b/README.md index ee15fde..6061e05 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/admin/controller/payment/payanyway.php b/admin/controller/payment/payanyway.php index d8f5624..69bd912 100755 --- a/admin/controller/payment/payanyway.php +++ b/admin/controller/payment/payanyway.php @@ -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']; diff --git a/catalog/controller/payment/callback.php b/catalog/controller/payment/callback.php index aa24520..17405da 100755 --- a/catalog/controller/payment/callback.php +++ b/catalog/controller/payment/callback.php @@ -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 + * @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 + * @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 $params */ diff --git a/catalog/library/payanyway/traits/payanyway.php b/catalog/library/payanyway/traits/payanyway.php index 449385d..dc8399c 100755 --- a/catalog/library/payanyway/traits/payanyway.php +++ b/catalog/library/payanyway/traits/payanyway.php @@ -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; } diff --git a/install.json b/install.json index 3a5821e..455ad68 100755 --- a/install.json +++ b/install.json @@ -1,7 +1,7 @@ { "code": "payanyway", "name": "PayAnyWay", - "version": "1.0.0", + "version": "1.0.1", "author": "PayAnyWay", "link": "https://www.payanyway.ru/" }