diff --git a/controllers/front/notify.php b/controllers/front/notify.php index 5bfd644d..e6031a71 100755 --- a/controllers/front/notify.php +++ b/controllers/front/notify.php @@ -112,13 +112,7 @@ public function postProcess() $checkoutData->setOrderStatus($assertResponseBody->getTransaction()->getStatus()); $checkoutProcessor->run($checkoutData); - - if (method_exists('Order', 'getIdByCartId')) { - $orderId = Order::getIdByCartId($cartId); - } else { - // For PrestaShop 1.6 or lower, use the alternative method - $orderId = Order::getOrderByCartId($cartId); - } + $orderId = $this->getOrderId($cartId); } //TODO look into pipeline design pattern to use when object is modified in multiple places to avoid this issue. diff --git a/controllers/front/return.php b/controllers/front/return.php index 652c8dc6..6d62e477 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -25,6 +25,8 @@ use Invertus\SaferPay\Controller\AbstractSaferPayController; use Invertus\SaferPay\Enum\ControllerName; use Invertus\SaferPay\Repository\SaferPayOrderRepository; +use Invertus\SaferPay\Service\SaferPayOrderStatusService; +use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion; if (!defined('_PS_VERSION_')) { exit; @@ -34,18 +36,40 @@ class SaferPayOfficialReturnModuleFrontController extends AbstractSaferPayContro { const FILENAME = 'return'; + public function postProcess() + { + $cartId = Tools::getValue('cartId'); + + $transactionResponse = $this->assertTransaction($cartId); + if ($transactionResponse->getTransaction()->getStatus() === 'PENDING') { + $orderId = $this->getOrderId($cartId); + if (!$orderId) { + return; + } + + /** @var SaferPayOrderStatusService $orderStatusService */ + $orderStatusService = $this->module->getService(SaferPayOrderStatusService::class); + $orderStatusService->pending(new \Order($orderId)); + } + } /** * @throws PrestaShopException */ - public function postProcess() + public function initContent() { + if (Tools::getValue('ajax')) { + $this->processAjax(); + exit; + } + + parent::initContent(); + $cartId = Tools::getValue('cartId'); $secureKey = Tools::getValue('secureKey'); $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); $fieldToken = Tools::getValue('fieldToken'); $moduleId = $this->module->id; $selectedCard = Tools::getValue('selectedCard'); - $cart = new Cart($cartId); if (!Validate::isLoadedObject($cart)) { @@ -88,22 +112,39 @@ public function postProcess() ] )); } - } else { - $this->context->smarty->assign( - 'checkStatusEndpoint', - $this->context->link->getModuleLink( - $this->module->name, - 'return', - [ - 'ajax' => 1, - 'action' => 'getStatus', - 'secureKey' => $secureKey, - 'cartId' => $cartId, - ], - true - )); - parent::setTemplate('saferpay_wait.tpl'); } + + $this->context->smarty->assign( + 'checkStatusEndpoint', + $this->context->link->getModuleLink( + $this->module->name, + 'return', + [ + 'ajax' => 1, + 'action' => 'getStatus', + 'secureKey' => $secureKey, + 'cartId' => $cartId, + ], + true + ) + ); + + $this->setTemplate(SaferPayConfig::SAFERPAY_TEMPLATE_LOCATION . '/front/saferpay_wait.tpl'); + } + + protected function processAjax() + { + if (empty($this->context->customer->id)) { + return; + } + + switch (Tools::getValue('action')) { + case 'getStatus': + $this->processGetStatus(); + break; + } + + exit; } /** @@ -115,7 +156,6 @@ protected function processGetStatus() header('Content-Type: application/json;charset=UTF-8'); /** @var SaferPayOrderRepository $saferPayOrderRepository */ $saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class); - $cartId = Tools::getValue('cartId'); $secureKey = Tools::getValue('secureKey'); $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); @@ -151,7 +191,11 @@ protected function processGetStatus() } $this->ajaxDie(json_encode([ - 'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || isset($saferPayOrder->pending), + 'authorized' => $saferPayOrder->authorized, + 'captured' => $saferPayOrder->captured, + 'canceled' => $saferPayOrder->canceled, + 'pending' => $saferPayOrder->pending, + 'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || $saferPayOrder->pending, 'href' => $href ])); } @@ -170,4 +214,26 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken) return $successController; } + + private function assertTransaction($cartId) { + /** @var SaferPayTransactionAssertion $transactionAssert */ + $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); + + return $transactionAssert->assert($cartId); + } + + /** + * @param int $cartId + * + * @return bool|int + */ + private function getOrderId($cartId) + { + if (method_exists('Order', 'getIdByCartId')) { + return Order::getIdByCartId($cartId); + } else { + // For PrestaShop 1.6 use the alternative method + return Order::getOrderByCartId($cartId); + } + } } diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index 5b6912f1..a335844c 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -203,6 +203,7 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart) $order = new Order($orderId); } + \PrestaShopLogger::addLog(sprintf('processr. %s', $data->getOrderStatus())); $saferPayOrder->id_order = $order->id; if ($data->getOrderStatus() === 'AUTHORIZED') { $order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_); @@ -216,7 +217,6 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart) } $saferPayOrder->update(); - $order->update(); return; } catch (\Exception $exception) { throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId()); diff --git a/src/Service/SaferPayOrderStatusService.php b/src/Service/SaferPayOrderStatusService.php index 0894beef..b1800f83 100755 --- a/src/Service/SaferPayOrderStatusService.php +++ b/src/Service/SaferPayOrderStatusService.php @@ -111,6 +111,15 @@ public function __construct( $this->module = $module->getModule(); } + public function pending(Order $order) + { + $saferPayOrder = $this->orderRepository->getByOrderId($order->id); + $saferPayOrder->pending = 1; + + $saferPayOrder->update(); + $order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_); + } + /** * @param Order $order * diff --git a/views/templates/front/saferpay_wait.tpl b/views/templates/front/saferpay_wait.tpl index 2981c63c..28d37257 100644 --- a/views/templates/front/saferpay_wait.tpl +++ b/views/templates/front/saferpay_wait.tpl @@ -19,7 +19,7 @@ *@copyright SIX Payment Services *@license SIX Payment Services *} -