Skip to content

Commit

Permalink
fix loop by separating controller and allow asserting without update
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jul 10, 2024
1 parent 3c89b8b commit 55cebc4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 114 deletions.
75 changes: 75 additions & 0 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Invertus\SaferPay\Controller\Front\CheckoutController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -43,7 +44,81 @@ public function postProcess()
case 'submitHostedFields':
$this->submitHostedFields();
break;
case 'getStatus':
$this->processGetStatus();
break;
}
}

/**
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
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);
$fieldToken = Tools::getValue('fieldToken');
$moduleId = $this->module->id;
$selectedCard = Tools::getValue('selectedCard');
$saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);

if (!$saferPayOrder->id || $saferPayOrder->canceled) {
$this->ajaxDie(json_encode([
'isFinished' => true,
'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId)
]));
}

$this->ajaxDie(json_encode([
'saferpayOrder' => json_encode($saferPayOrder),
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending,
'href' => $this->context->link->getModuleLink(
$this->module->name,
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
[
'cartId' => $cartId,
'orderId' => $saferPayOrder->id_order,
'moduleId' => $moduleId,
'secureKey' => $secureKey,
'selectedCard' => $selectedCard,
]
)
]));
}

private function getFailControllerLink($cartId, $secureKey, $moduleId)
{
return $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'moduleId' => $moduleId,
],
true
);
}

private function getSuccessControllerName($isBusinessLicence, $fieldToken)
{
$successController = ControllerName::SUCCESS;

if ($isBusinessLicence) {
$successController = ControllerName::SUCCESS_IFRAME;
}

if ($fieldToken) {
$successController = ControllerName::SUCCESS_HOSTED;
}

return $successController;
}

private function submitHostedFields()
Expand Down
35 changes: 26 additions & 9 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public function postProcess()
die($this->module->l('Lock already exist', self::FILENAME));
}

if ($cart->orderExists()) {
$order = new Order($this->getOrderId($cartId));
$completed = (int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED);

if ((int) $order->current_state === $completed) {
die($this->module->l('Order already complete', self::FILENAME));
}
}

/** @var SaferPayOrderRepository $saferPayOrderRepository */
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);

Expand All @@ -86,6 +95,7 @@ public function postProcess()
);

$checkoutData->setOrderStatus($transactionStatus);

$checkoutProcessor->run($checkoutData);
$orderId = $this->getOrderId($cartId);

Expand Down Expand Up @@ -129,28 +139,34 @@ public function postProcess()
$orderStatusService->capture($order);
}
} catch (Exception $e) {
$this->releaseLock();
// this might be executed after pending transaction is declined (e.g. with accountToAccount payment)
if (!isset($order)) {
$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);
$order = new Order($this->getOrderId($cartId));
}

if ($order->id) {
$orderId = (int) $order->id;

if ($orderId) {
// assuming order transaction was declined
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);
$order->update();
$saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByOrderId($order->id));
} else {
// assuming order transaction was cancelled before ps order was even made
$saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByCartId($cartId));
}

// using cartId, because ps order might not be assigned yet
$saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByCartId($cartId));

if ($saferPayOrder->id) {
$saferPayOrder->authorized = false;
$saferPayOrder->pending = false;
$saferPayOrder->canceled = true;

if ($orderId) {
// assign ps order to saferpay order id in case it was not assigned previously
$saferPayOrder->id_order = $orderId;
}

$saferPayOrder->update();
$this->releaseLock();
die('canceled');
}

PrestaShopLogger::addLog(
Expand All @@ -166,6 +182,7 @@ public function postProcess()
true
);
$this->releaseLock();

die($this->module->l($e->getMessage(), self::FILENAME));
}

Expand Down
97 changes: 5 additions & 92 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization;
Expand All @@ -43,26 +42,20 @@ class SaferPayOfficialReturnModuleFrontController extends AbstractSaferPayContro
public function postProcess()
{
$cartId = (int) Tools::getValue('cartId');
$isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE);
$selectedCard = (int) Tools::getValue('selectedCard');
$order = new Order($this->getOrderId($cartId));

if (!$order->id) {
return;
}

try {
if ($isBusinessLicence) {
$response = $this->executeTransaction($cartId, $selectedCard);
} else {
$response = $this->assertTransaction($cartId);
}

\PrestaShopLogger::addLog($response->getTransaction()->getStatus());
/** @var SaferPayTransactionAssertion $transactionAssert */
$transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class);
$transactionResponse = $transactionAssert->assert($cartId, false);

/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
if ($response->getTransaction()->getStatus() === TransactionStatus::PENDING) {
if ($transactionResponse->getTransaction()->getStatus() === TransactionStatus::PENDING) {
$orderStatusService->setPending($order);
}
} catch (SaferPayApiException $e) {
Expand All @@ -75,13 +68,6 @@ 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);
Expand Down Expand Up @@ -136,7 +122,7 @@ public function initContent()
'checkStatusEndpoint',
$this->context->link->getModuleLink(
$this->module->name,
'return',
'ajax',
[
'ajax' => 1,
'action' => 'getStatus',
Expand All @@ -155,62 +141,6 @@ public function initContent()
$this->setTemplate('saferpay_wait_16.tpl');
}

protected function processAjax()
{
if (empty($this->context->customer->id)) {
return;
}

switch (Tools::getValue('action')) {
case 'getStatus':
$this->processGetStatus();
break;
}

exit;
}

/**
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
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);
$fieldToken = Tools::getValue('fieldToken');
$moduleId = $this->module->id;
$selectedCard = Tools::getValue('selectedCard');
$saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);

if (!$saferPayOrder->id || $saferPayOrder->canceled) {
$this->ajaxDie(json_encode([
'isFinished' => true,
'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId)
]));
}

$this->ajaxDie(json_encode([
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending,
'href' => $this->context->link->getModuleLink(
$this->module->name,
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
[
'cartId' => $cartId,
'orderId' => $saferPayOrder->id_order,
'moduleId' => $moduleId,
'secureKey' => $secureKey,
'selectedCard' => $selectedCard,
]
)
]));
}

private function getSuccessControllerName($isBusinessLicence, $fieldToken)
{
$successController = ControllerName::SUCCESS;
Expand All @@ -226,20 +156,6 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken)
return $successController;
}

private function getFailControllerLink($cartId, $secureKey, $moduleId)
{
return $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'moduleId' => $moduleId,
],
true
);
}

/**
* @param int $orderId
* @param int $selectedCard
Expand All @@ -260,10 +176,7 @@ private function executeTransaction($orderId, $selectedCard)
}

private function assertTransaction($cartId) {
/** @var SaferPayTransactionAssertion $transactionAssert */
$transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class);

return $transactionAssert->assert($cartId);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ public function hookDisplayOrderConfirmation($params)
return '';
}

//@todo: translate and move to template if needed when requirements are clear
return 'Your payment is still being processed by your bank. This can take up to 5 days (120 hours). Once we receive the final status, we will notify you immediately.
Thank you for your patience!';
return $this->l('Your payment is still being processed by your bank. This can take up to 5 days (120 hours). Once we receive the final status, we will notify you immediately.
Thank you for your patience!');
}

public function hookActionObjectOrderPaymentAddAfter($params)
Expand Down
18 changes: 12 additions & 6 deletions src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Order;
use PrestaShopException;
use SaferPayOrder;
use Validate;

class CheckoutProcessor
{
Expand Down Expand Up @@ -78,7 +79,12 @@ public function run(CheckoutData $data) {
$this->processCreateOrder($cart, $data->getPaymentMethod());
}

if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED || $data->getOrderStatus() === TransactionStatus::CAPTURED) {
$authorizedStates = [
TransactionStatus::AUTHORIZED,
TransactionStatus::CAPTURED,
];

if (in_array($data->getOrderStatus(), $authorizedStates)) {
$this->processAuthorizedOrder($data, $cart);
return '';
}
Expand Down Expand Up @@ -183,19 +189,19 @@ private function processCreateSaferPayOrder($initializeBody, $cartId, $customerI
private function processAuthorizedOrder(CheckoutData $data, Cart $cart)
{
try {
$saferPayOrder = new SaferPayOrder($this->saferPayOrderRepository->getIdByCartId($cart->id));
$this->processCreateOrder($cart, $data->getPaymentMethod());
$order = $this->getOrder($cart->id);
$saferPayOrder = new SaferPayOrder($this->saferPayOrderRepository->getIdByCartId($cart->id));

$saferPayOrder->id_order = $order->id;
if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED) {
$saferPayOrder->authorized = true;
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_);
$saferPayOrder->authorized = 1;
} elseif ($data->getOrderStatus() === TransactionStatus::CAPTURED) {
} else {
$saferPayOrder->captured = true;
$order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_);
$saferPayOrder->captured = 1;
}

$saferPayOrder->id_order = $order->id;
$saferPayOrder->update();
} catch (\Exception $exception) {
throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId());
Expand Down
Loading

0 comments on commit 55cebc4

Please sign in to comment.