Skip to content

Commit

Permalink
Merge pull request #147 from Invertus/SL-215-fail-authorization-fix
Browse files Browse the repository at this point in the history
SL-215: fail authorization fix
  • Loading branch information
mant02 authored Feb 7, 2024
2 parents 360bd52 + 4cc6997 commit 1d703c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 93 deletions.
17 changes: 4 additions & 13 deletions controllers/front/successHosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

class SaferPayOfficialSuccessHostedModuleFrontController extends AbstractSaferPayController
{
const FILENAME = 'successHosted';
const FILE_NAME = 'successHosted';

protected $display_header = false;
protected $display_footer = false;
Expand All @@ -56,7 +56,9 @@ public function postProcess()

$cart = new Cart($cartId);
if ($cart->secure_key !== $secureKey) {
Tools::redirect($this->getOrderLink());
$this->errors[] = $this->module->l('Failed to validate cart.', self::FILE_NAME);

$this->redirectWithNotifications($this->getOrderLink());
}

try {
Expand Down Expand Up @@ -91,17 +93,6 @@ public function postProcess()
}
}

public function initContent()
{
parent::initContent();
$cartId = Tools::getValue('cartId');
$moduleId = Tools::getValue('moduleId');
$orderId = Tools::getValue('orderId');
$secureKey = Tools::getValue('secureKey');

Tools::redirect($this->getOrderConfirmationLink($cartId, $moduleId, $orderId, $secureKey));
}

/**
* @param int $cartId
* @param int $moduleId
Expand Down
108 changes: 29 additions & 79 deletions controllers/front/successIFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class SaferPayOfficialSuccessIFrameModuleFrontController extends AbstractSaferPayController
{
const FILENAME = 'successIFrame';
const FILE_NAME = 'successIFrame';

protected $display_header = false;
protected $display_footer = false;
Expand All @@ -53,95 +53,45 @@ public function postProcess() // todo refactor this by the logic provided
$cartId = Tools::getValue('cartId');
$orderId = Tools::getValue('orderId');
$secureKey = Tools::getValue('secureKey');
$selectedCard = Tools::getValue('selectedCard');
$moduleId = Tools::getValue('moduleId');

$cart = new Cart($cartId);
if ($cart->secure_key !== $secureKey) {
Tools::redirect($this->getOrderLink());
}

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

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

$order = new Order($orderId);

try {
$authResponseBody = $saferPayTransactionAuthorization->authorize(
$orderId,
(int) $selectedCard === SaferPayConfig::CREDIT_CARD_OPTION_SAVE,
$selectedCard
);
} catch (SaferPayApiException $e) {
$this->warning[] = $this->module->l('We couldn\'t authorize your payment. Please try again.', self::FILENAME);
$this->redirectWithNotifications($this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL_IFRAME,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'orderId' => $orderId,
'moduleId' => $moduleId,
],
true
));
}
if ($cart->secure_key !== $secureKey) {
$this->errors[] = $this->module->l('Failed to validate cart.', self::FILE_NAME);

$paymentBehaviourWithout3DS = (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D);

if (
(!$authResponseBody->getLiability()->getLiabilityShift() &&
in_array($order->payment, SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS) &&
$paymentBehaviourWithout3DS === SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL) ||
$authResponseBody->getTransaction()->getStatus() === SaferPayConfig::TRANSACTION_STATUS_CANCELED
) {
$orderStatusService->cancel($order);

$this->warning[] = $this->module->l('We couldn\'t authorize your payment. Please try again.', self::FILENAME);

$this->redirectWithNotifications($this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL_IFRAME,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'orderId' => $orderId,
'moduleId' => $moduleId,
],
true
));
$this->redirectWithNotifications($this->getOrderLink());
}

try {
$orderStatusService->authorize($order);

Tools::redirect($this->getOrderConfirmationLink($cartId, $moduleId, $orderId, $secureKey));
} catch (Exception $exception) {
$this->warning[] = $this->module->l('We couldn\'t authorize your payment. Please try again.', self::FILENAME);
$this->redirectWithNotifications($this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL_IFRAME,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'orderId' => $orderId,
'moduleId' => $moduleId,
],
} catch (Exception $e) {
PrestaShopLogger::addLog(
sprintf(
'%s has caught an error: %s',
__CLASS__,
$e->getMessage()
),
1,
null,
null,
null,
true
));
}

$paymentBehaviour = (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR);
);

if (
$paymentBehaviour === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$authResponseBody->getLiability()->getThreeDs() &&
$authResponseBody->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
) {
$orderStatusService->capture($order);
Tools::redirect(
$this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL_IFRAME,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'orderId' => $orderId,
\Invertus\SaferPay\Config\SaferPayConfig::IS_BUSINESS_LICENCE => true,
],
true
)
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Enum/ControllerName.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ControllerName
const PENDING_NOTIFY = 'pendingNotify';
const SUCCESS = 'success';
const SUCCESS_HOSTED = 'successHosted';
const SUCCESS_IFRAME = 'successIframe';
const SUCCESS_IFRAME = 'successIFrame';
const VALIDATION = 'validation';
const RETURN_URL = 'return';
}

0 comments on commit 1d703c4

Please sign in to comment.