185 lines
6.4 KiB
PHP
185 lines
6.4 KiB
PHP
<?php
|
|
|
|
use Imoje\Payment\Util;
|
|
|
|
/**
|
|
* Class IMojeNotificationModuleFrontController
|
|
*
|
|
* @property bool display_column_left
|
|
* @property bool display_column_right
|
|
* @property bool display_footer
|
|
* @property bool display_header
|
|
*/
|
|
class IMojeNotificationModuleFrontController extends ModuleFrontController
|
|
{
|
|
|
|
/**
|
|
* Initialize controller.
|
|
*
|
|
* @see FrontController::init()
|
|
*/
|
|
public function init()
|
|
{
|
|
$this->display_column_left = false;
|
|
$this->display_column_right = false;
|
|
$this->display_footer = false;
|
|
$this->display_header = false;
|
|
parent::init();
|
|
}
|
|
|
|
/**
|
|
* Verify signature, json, create signature and change status if its okay
|
|
*
|
|
* @throws PrestaShopException
|
|
* @throws Exception
|
|
*/
|
|
public function process()
|
|
{
|
|
|
|
include_once(_PS_MODULE_DIR_ . 'imoje/libraries/payment-core/vendor/autoload.php');
|
|
|
|
$resultCheckRequestNotification = Util::checkRequestNotification(Configuration::get('IMOJE_SERVICE_KEY'), Configuration::get('IMOJE_SERVICE_ID'));
|
|
$isDebugMode = Configuration::get('IMOJE_DEBUG_MODE');
|
|
|
|
$createOrderArrangement = Configuration::get('IMOJE_CREATE_ORDER_ARRANGEMENT');
|
|
|
|
if (is_int($resultCheckRequestNotification)) {
|
|
echo Util::notificationResponse(Util::NS_ERROR, $resultCheckRequestNotification, $isDebugMode, null, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
$orderId = $resultCheckRequestNotification['transaction']['orderId'];
|
|
|
|
// 0 - after checkout, 1 - after ipn
|
|
if (!$createOrderArrangement) {
|
|
|
|
$order = new Order($orderId);
|
|
|
|
$orderId = $order->id;
|
|
|
|
if (!$orderId) {
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_ORDER_NOT_FOUND, $isDebugMode, null, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
$currentOrderState = $order->current_state;
|
|
|
|
$currencyInfo = Currency::getCurrency($order->id_currency);
|
|
|
|
switch ($resultCheckRequestNotification['transaction']['status']) {
|
|
case 'settled':
|
|
$stateToChange = _PS_OS_PAYMENT_;
|
|
break;
|
|
case 'rejected':
|
|
$stateToChange = _PS_OS_ERROR_;
|
|
break;
|
|
default:
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_ORDER_STATUS_NOT_CHANGED, $isDebugMode, $currentOrderState, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
if ($order->current_state === _PS_OS_REFUND_) {
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_ORDER_STATUS_NOT_CHANGED, $isDebugMode, $currentOrderState, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
if (!Util::checkRequestAmount(
|
|
$resultCheckRequestNotification,
|
|
Util::convertAmountToFractional(round($order->total_paid, 2)),
|
|
$currencyInfo["iso_code"])) {
|
|
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_AMOUNT_NOT_MATCH, $isDebugMode, $currentOrderState, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
$history = new OrderHistory();
|
|
$history->id_order = $orderId;
|
|
$history->changeIdOrderState($stateToChange, $orderId);
|
|
$history->add();
|
|
$history->save();
|
|
|
|
if (!$this->insertTransactionId($orderId, $resultCheckRequestNotification['transaction']['id'])) {
|
|
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_COULD_NOT_INSERT_TRANSACTION_ID_TO_DB, $isDebugMode, $currentOrderState, $stateToChange, $createOrderArrangement);
|
|
exit;
|
|
}
|
|
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_OK, $isDebugMode, $currentOrderState, $stateToChange, $createOrderArrangement);
|
|
exit;
|
|
}
|
|
|
|
if ($resultCheckRequestNotification['transaction']['status'] !== 'settled') {
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_ORDER_STATUS_IS_NOT_SETTLED_ORDER_ARRANGEMENT_AFTER_IPN, $isDebugMode, null, null, $createOrderArrangement);
|
|
exit;
|
|
}
|
|
|
|
$cart = new Cart($orderId);
|
|
|
|
if (!$cart->id) {
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_CART_NOT_FOUND, $isDebugMode, null, null, $createOrderArrangement);;
|
|
exit;
|
|
}
|
|
|
|
if (Order::getOrderByCartId($orderId)) {
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_ORDER_EXISTS_ORDER_ARRANGEMENT_AFTER_IPN, $isDebugMode, null, null, $createOrderArrangement);;
|
|
exit;
|
|
}
|
|
|
|
$currencyInfo = Currency::getCurrency($cart->id_currency);
|
|
|
|
$cartTotal = $cart->getOrderTotal();
|
|
|
|
if (!Util::checkRequestAmount(
|
|
$resultCheckRequestNotification,
|
|
Util::convertAmountToFractional($cartTotal),
|
|
$currencyInfo["iso_code"])) {
|
|
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_AMOUNT_NOT_MATCH, $isDebugMode, null, null, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
$customer = new Customer((int) $cart->id_customer);
|
|
$cartId = $cart->id;
|
|
|
|
$imoje = new iMoje();
|
|
$imoje->validateOrder($cartId,
|
|
_PS_OS_PAYMENT_,
|
|
$cartTotal,
|
|
$imoje->displayName,
|
|
null,
|
|
[],
|
|
null,
|
|
false,
|
|
$customer->secure_key
|
|
);
|
|
|
|
$history = new OrderHistory();
|
|
$history->id_order = intval($orderId);
|
|
|
|
$order = new Order(Order::getOrderByCartId($orderId));
|
|
|
|
if (!$this->insertTransactionId($order->id, $resultCheckRequestNotification['transaction']['id'])) {
|
|
|
|
echo Util::notificationResponse(Util::NS_ERROR, Util::NC_COULD_NOT_INSERT_TRANSACTION_ID_TO_DB, $isDebugMode, null, _PS_OS_PAYMENT_, $createOrderArrangement);
|
|
exit;
|
|
}
|
|
|
|
echo Util::notificationResponse(Util::NS_OK, Util::NC_OK, $isDebugMode, null, _PS_OS_PAYMENT_, $createOrderArrangement);
|
|
exit();
|
|
}
|
|
|
|
/**
|
|
* @param $idOrder
|
|
* @param $idTransaction
|
|
*
|
|
* @return mixed
|
|
*/
|
|
private function insertTransactionId($idOrder, $idTransaction)
|
|
{
|
|
return Db::getInstance()->insert('imoje_transaction_list', [
|
|
'id_order' => pSQL($idOrder),
|
|
'id_transaction' => pSQL($idTransaction),
|
|
]);
|
|
}
|
|
}
|