Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SL-215: fail authorization fix #147

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Loading