Skip to content

Commit

Permalink
fix pending status
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jun 28, 2024
1 parent b3bac09 commit 9595267
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
8 changes: 1 addition & 7 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
104 changes: 85 additions & 19 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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
]));
}
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -216,7 +217,6 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart)
}

$saferPayOrder->update();
$order->update();
return;
} catch (\Exception $exception) {
throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId());
Expand Down
9 changes: 9 additions & 0 deletions src/Service/SaferPayOrderStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions views/templates/front/saferpay_wait.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*@copyright SIX Payment Services
*@license SIX Payment Services
*}
<h2>{l s='Awaiting payment status' mod='saferpay'}</h2>
<h2>{l s='Awaiting payment status' mod='saferpayofficial'}</h2>
<div class="saferpay-spinner">
<div class="rect1"></div>
<div class="rect2"></div>
Expand Down Expand Up @@ -97,7 +97,7 @@
if (request.status >= 200 && request.status < 400) {
try {
var data = JSON.parse(request.responseText);
if (data.success && data.isFinished) {
if (data.isFinished && data.href) {
window.location.href = data.href;
return;
}
Expand Down

0 comments on commit 9595267

Please sign in to comment.