feat: refactor callback
This commit is contained in:
parent
d57dba7f7c
commit
1e168ded8c
@ -4,7 +4,7 @@
|
|||||||

|

|
||||||

|

|
||||||
|
|
||||||
> **📦 Версия модуля: 1.0.0**
|
> **📦 Версия модуля: 1.0.1**
|
||||||
|
|
||||||
> **ℹ️ Совместимость версий OpenCart:**
|
> **ℹ️ Совместимость версий 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
|
> 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
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
<modification>
|
<modification>
|
||||||
<name>PayAnyWay</name>
|
<name>PayAnyWay</name>
|
||||||
<code>payanyway</code>
|
<code>payanyway</code>
|
||||||
<version>1.0.0</version>
|
<version>1.0.1</version>
|
||||||
<author>PayAnyWay</author>
|
<author>PayAnyWay</author>
|
||||||
<link>https://payanyway.ru/</link>
|
<link>https://payanyway.ru/</link>
|
||||||
</modification>
|
</modification>
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
class ControllerExtensionPaymentPayanyway extends Controller {
|
class ControllerExtensionPaymentPayanyway extends Controller {
|
||||||
const CMS_NAME = 'OpenCart';
|
const CMS_NAME = 'OpenCart';
|
||||||
const MODULE_NAME = 'payanyway';
|
const MODULE_NAME = 'payanyway';
|
||||||
const MODULE_VERSION = '1.0.0';
|
const MODULE_VERSION = '1.0.1';
|
||||||
const MODULE_DEFAULT_SORT_ORDER = 0;
|
const MODULE_DEFAULT_SORT_ORDER = 0;
|
||||||
|
|
||||||
private $supportedCurrencies = array('RUB');
|
private $supportedCurrencies = array('RUB');
|
||||||
|
|||||||
@ -166,7 +166,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
|
|
||||||
/** @return string */
|
/** @return string */
|
||||||
private function createTransactionId() {
|
private function createTransactionId() {
|
||||||
$date = (new \DateTimeImmutable())->format('YmdHis');
|
$date = date('YmdHis');
|
||||||
return $this->orderId . self::TRANSACTION_ID_STRING_DELIMITER . $date;
|
return $this->orderId . self::TRANSACTION_ID_STRING_DELIMITER . $date;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,7 +296,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
|
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
|
||||||
return null;
|
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;
|
return isset($transactionIdData[0]) ? (int)$transactionIdData[0] : null;
|
||||||
}
|
}
|
||||||
@ -343,7 +343,7 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
? array_merge(['MNT_COMMAND'], $baseFields)
|
? array_merge(['MNT_COMMAND'], $baseFields)
|
||||||
: $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];
|
return $carry . $data[$field];
|
||||||
}, '') . $this->integrityCode;
|
}, '') . $this->integrityCode;
|
||||||
|
|
||||||
@ -360,59 +360,10 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
$this->sendResponse('FAIL');
|
$this->sendResponse('FAIL');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->load->model('account/order');
|
list($resultCode, $description) = $this->determineCheckResult($callbackData);
|
||||||
$products = $this->model_account_order->getOrderProducts($this->orderId);
|
$responseData = $this->buildResponseData($callbackData, $resultCode, $description);
|
||||||
$deliveryPrice = $this->getDeliveryPrice();
|
|
||||||
|
|
||||||
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
|
$this->sendResponse($this->buildXMLResponse($responseData));
|
||||||
|
|
||||||
$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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -422,7 +373,6 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
*/
|
*/
|
||||||
private function handlePayCallback(array $callbackData) {
|
private function handlePayCallback(array $callbackData) {
|
||||||
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
|
$orderTotal = (float)(isset($this->orderInfo['total']) ? $this->orderInfo['total'] : 0);
|
||||||
|
|
||||||
$currencyCode = isset($this->orderInfo['currency_code']) ? $this->orderInfo['currency_code'] : 'RUB';
|
$currencyCode = isset($this->orderInfo['currency_code']) ? $this->orderInfo['currency_code'] : 'RUB';
|
||||||
$shopData = array(
|
$shopData = array(
|
||||||
'MNT_ID' => $this->mntId,
|
'MNT_ID' => $this->mntId,
|
||||||
@ -439,33 +389,77 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$isPayedOrder = $this->isOrderPaid();
|
$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) {
|
if (!$isPayedOrder) {
|
||||||
$this->load->language('extension/payment/payanyway');
|
$this->load->language('extension/payment/payanyway');
|
||||||
$this->addOrderHistory($this->successOrderStatusId, $this->language->get('text_payment_completed'));
|
$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 */
|
/** @return bool */
|
||||||
@ -498,11 +492,15 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
* @return false|string
|
* @return false|string
|
||||||
*/
|
*/
|
||||||
private function getInventoryJson(array $products) {
|
private function getInventoryJson(array $products) {
|
||||||
|
if (empty($products)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$inventory = array();
|
$inventory = array();
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
$name = isset($product['name']) ? $product['name'] : '';
|
$name = isset($product['name']) ? $product['name'] : '';
|
||||||
$price = isset($product['price']) ? (float)$product['price'] : 0;
|
$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(
|
$inventory[] = array(
|
||||||
'name' => $this->validateString($name, self::INVENTORY_ITEM_NAME_MAX_LENGTH),
|
'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
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function formatQuantity($quantity) {
|
private function formatQuantity($quantity) {
|
||||||
$quantity = str_replace(',', '.', $quantity);
|
$quantityString = number_format($quantity, 3, '.', '');
|
||||||
if (strpos($quantity, '.') === false && ctype_digit($quantity)) {
|
return rtrim(rtrim($quantityString, '0'), '.');
|
||||||
return (string)(int)$quantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return number_format((float)$quantity, 3, '.', '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -601,7 +595,8 @@ class ControllerExtensionPaymentPayanyway extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dom->saveXML();
|
$xml = $dom->saveXML();
|
||||||
|
return (false !== $xml) ? $xml : 'FAIL';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user