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-252 Hosted iframe fix #196

Merged
merged 19 commits into from
Oct 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private function displayConfigurationSettings()
'title' => $this->module->l('Description', self::FILE_NAME),
'type' => 'text',
'desc' => 'This description is visible in payment page also in payment confirmation email',
'class' => 'fixed-width-xxl'
'class' => 'fixed-width-xxl',
],
],
'buttons' => [
Expand Down
4 changes: 2 additions & 2 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function processGetStatus()
if (!$saferPayOrder->id || $saferPayOrder->canceled) {
$this->ajaxDie(json_encode([
'isFinished' => true,
'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId)
'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId),
]));
}

Expand All @@ -88,7 +88,7 @@ protected function processGetStatus()
'secureKey' => $secureKey,
'selectedCard' => $selectedCard,
]
)
),
]));
}

Expand Down
1 change: 0 additions & 1 deletion controllers/front/iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function initContent()
);

$redirectUrl = $checkoutController->execute($checkoutData);

} catch (\Exception $exception) {
$redirectUrl = $this->context->link->getModuleLink(
$this->module->name,
Expand Down
8 changes: 4 additions & 4 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public function postProcess()
die($this->module->l('Success', self::FILENAME));
}

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

Expand All @@ -217,10 +218,9 @@ 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);
}
// For PrestaShop 1.6 use the alternative method
return Order::getOrderByCartId($cartId);
}

protected function displayMaintenancePage()
Expand Down
115 changes: 106 additions & 9 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Processor\CheckoutProcessor;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization;
Expand All @@ -43,19 +45,96 @@ public function postProcess()
{
$cartId = (int) Tools::getValue('cartId');
$order = new Order($this->getOrderId($cartId));
$secureKey = Tools::getValue('secureKey');
$cart = new Cart($cartId);

if (!$order->id) {
return;
if (!Validate::isLoadedObject($cart)) {
$this->warning[] = $this->module->l('An unknown error error occurred. Please contact support', self::FILENAME);
$this->redirectWithNotifications($this->getRedirectionToControllerUrl('fail'));
}

if ($cart->secure_key !== $secureKey) {
$this->warning[] = $this->module->l('Error. Insecure cart', self::FILENAME);
$this->redirectWithNotifications($this->getRedirectionToControllerUrl('fail'));
}

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

try {
/** @var SaferPayTransactionAssertion $transactionAssert */
$transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class);
$transactionResponse = $transactionAssert->assert($cartId, false);
$assertResponseBody = $transactionAssert->assert($cartId);
$transactionStatus = $assertResponseBody->getTransaction()->getStatus();
} catch (Exception $e) {
\PrestaShopLogger::addLog($e->getMessage());
MarijusCoding marked this conversation as resolved.
Show resolved Hide resolved
$this->warning[] = $this->module->l('An error occurred. Please contact support', self::FILENAME);
$this->redirectWithNotifications($this->getRedirectionToControllerUrl('fail'));
}

/**
* NOTE: This flow is for hosted iframe payment method
*/
if (Tools::getValue('isBusinessLicence')) {
MarijusCoding marked this conversation as resolved.
Show resolved Hide resolved
try {
/** @var CheckoutProcessor $checkoutProcessor * */
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
MarijusCoding marked this conversation as resolved.
Show resolved Hide resolved

$checkoutData = CheckoutData::create(
(int) $cartId,
$assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod(),
(int) Configuration::get(SaferPayConfig::IS_BUSINESS_LICENCE)
);
$checkoutData->setOrderStatus($transactionStatus);

$checkoutProcessor->run($checkoutData);

$orderId = $this->getOrderId($cartId);

$order = new Order($orderId);
if (!$assertResponseBody->getLiability()->getLiabilityShift() &&
in_array($order->payment, SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS) &&
(int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D) === SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->cancel($order);
}

//NOTE to get latest information possible and not override new information.

$paymentMethod = $assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod();// if payment does not support order capture, it means it always auto-captures it (at least with accountToAccount payment),

// so in this case if status comes back "captured" we just update the order state accordingly
if (!SaferPayConfig::supportsOrderCapture($paymentMethod) &&
$transactionStatus === TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->setComplete($order);

return;
}

if (SaferPayConfig::supportsOrderCapture($paymentMethod) &&
(int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$transactionStatus !== TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->capture($order);
MarijusCoding marked this conversation as resolved.
Show resolved Hide resolved

return;
}
} catch (Exception $e) {
\PrestaShopLogger::addLog($e->getMessage());
$this->warning[] = $this->module->l('An error occurred. Please contact support', self::FILENAME);
$this->redirectWithNotifications($this->getRedirectionToControllerUrl('fail'));
}
}

try {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
if ($transactionResponse->getTransaction()->getStatus() === TransactionStatus::PENDING) {
if ($assertResponseBody->getTransaction()->getStatus() === TransactionStatus::PENDING) {
$orderStatusService->setPending($order);
}
} catch (SaferPayApiException $e) {
Expand Down Expand Up @@ -184,9 +263,27 @@ 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);
}
// For PrestaShop 1.6 use the alternative method
return Order::getOrderByCartId($cartId);
}

/**
* @param string $controllerName
*
* @return string
*/
private function getRedirectionToControllerUrl($controllerName)
{
return $this->context->link->getModuleLink(
$this->module->name,
$controllerName,
[
'cartId' => $this->context->cart->id,
'orderId' => Order::getOrderByCartId($this->context->cart->id),
'secureKey' => $this->context->cart->secure_key,
'moduleId' => $this->module->id,
]
);
}
}
3 changes: 0 additions & 3 deletions controllers/front/successHosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down
4 changes: 0 additions & 4 deletions controllers/front/successIFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down
6 changes: 3 additions & 3 deletions saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function hookPaymentOptions($params)
foreach ($paymentMethods as $paymentMethod) {
$paymentMethod['paymentMethod'] = str_replace(' ', '', $paymentMethod['paymentMethod']);

if(!in_array($paymentMethod['paymentMethod'], $activePaymentMethods)) {
if (!in_array($paymentMethod['paymentMethod'], $activePaymentMethods)) {
continue;
}

Expand Down Expand Up @@ -689,8 +689,8 @@ private function displayInAdminOrderPage(array $params)
);
} else {
$action = $this->context->link->getAdminLink(
self::ADMIN_ORDER_CONTROLLER
) . '&id_order=' . (int) $orderId;
self::ADMIN_ORDER_CONTROLLER
) . '&id_order=' . (int) $orderId;
}

$assertId = $orderRepo->getAssertIdBySaferPayOrderId($saferPayOrderId);
Expand Down
3 changes: 1 addition & 2 deletions src/Api/Request/AssertService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\EntityBuilder\SaferPayAssertBuilder;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Exception\Api\TransactionDeclinedException;
use Invertus\SaferPay\Service\Response\AssertResponseObjectCreator;
use SaferPayOrder;

Expand Down Expand Up @@ -84,7 +83,7 @@ public function assert(AssertRequest $assertRequest, $saferPayOrderId)
// also we call authorize method in some of the success controllers, so if we leave the logic here,
// we get an error with TRANSACTION_IN_WRONG_STATE
if ($saferPayOrder->is_transaction) {
$assertApi = self::ASSERT_API_TRANSACTION;
$assertApi = self::ASSERT_API_TRANSACTION;
}

try {
Expand Down
1 change: 0 additions & 1 deletion src/Controller/AbstractSaferPayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ protected function applyLock($resource)
$this->lock->create($resource);

if (!$this->lock->acquire()) {

if (!SaferPayConfig::isVersion17()) {
return http_response_code(409);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Front/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*@license SIX Payment Services
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
Expand Down
1 change: 0 additions & 1 deletion src/Core/Order/Action/UpdateOrderStatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ public function run($orderId, $orderStatusId)
} catch (\Exception $exception) {
throw CouldNotChangeOrderStatus::unknownError();
}

}
}
34 changes: 16 additions & 18 deletions src/Core/Payment/DTO/CheckoutData.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ class CheckoutData
private $status;

public function __construct(
$cartId,
$paymentMethod,
$isBusinessLicense,
$selectedCard = -1,
$fieldToken = null,
$successController = null,
$isTransaction = false
)
{
$cartId,
$paymentMethod,
$isBusinessLicense,
$selectedCard = -1,
$fieldToken = null,
$successController = null,
$isTransaction = false
) {
$this->cartId = $cartId;
$this->paymentMethod = $paymentMethod;
$this->isBusinessLicense = $isBusinessLicense;
Expand All @@ -65,15 +64,14 @@ public function __construct(
}

public static function create(
$cartId,
$paymentMethod,
$isBusinessLicense,
$selectedCard = -1,
$fieldToken = null,
$successController = null,
$isTransaction = false
)
{
$cartId,
$paymentMethod,
$isBusinessLicense,
$selectedCard = -1,
$fieldToken = null,
$successController = null,
$isTransaction = false
) {
return new self(
$cartId,
$paymentMethod,
Expand Down
9 changes: 6 additions & 3 deletions src/DTO/Request/Initialize/InitializeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,14 @@ public function getAsArray()
'AddressSource' => $this->deliveryAddressForm->getAddressSource(),
'MandatoryFields' => $this->deliveryAddressForm->getMandatoryFields(),
],
'CardForm' => [
'HolderName' => SaferPayConfig::SAFERPAY_CARDFORM_HOLDERNAME_REQUIRENCE,
],
];

if ($this->getPaymentMeansField() === []) {
$return['CardForm'] = [
'HolderName' => SaferPayConfig::SAFERPAY_CARDFORM_HOLDERNAME_REQUIRENCE,
];
}

if ($this->notification !== null) {
$return['Notification'] = [
'MerchantEmails' => [$this->notification->getMerchantEmail()],
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CouldNotProcessCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public static function failedToCreateSaferPayOrder($cartId)
]
);
}
}
}
5 changes: 2 additions & 3 deletions src/Exception/SaferPayException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ final public function __construct(
$internalMessage,
$code,
array $context = []
)
{
) {
parent::__construct($internalMessage, $code);
$this->context = $context;
}
Expand All @@ -53,4 +52,4 @@ public static function unknownError()
ExceptionCode::UNKNOWN_ERROR
);
}
}
}
Loading
Loading