Skip to content

Commit

Permalink
add wait template and remove order creation from return page
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jun 27, 2024
1 parent 51bc5de commit 78958f0
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 137 deletions.
188 changes: 51 additions & 137 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;
use Invertus\SaferPay\Processor\CheckoutProcessor;
Expand All @@ -50,7 +51,6 @@ public function postProcess()
$fieldToken = Tools::getValue('fieldToken');
$moduleId = $this->module->id;
$selectedCard = Tools::getValue('selectedCard');
$orderId = Tools::getValue('orderId');

$cart = new Cart($cartId);

Expand All @@ -61,26 +61,6 @@ public function postProcess()
]));
}

$lockResult = $this->applyLock(
sprintf(
'%s-%s',
$cartId,
$secureKey
)
);

if (!$lockResult->isSuccessful()) {
$this->redirectWithNotifications($this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'moduleId' => $moduleId,
]
));
}

if ($cart->secure_key !== $secureKey) {
$this->ajaxDie(json_encode([
'error_type' => 'unknown_error',
Expand Down Expand Up @@ -114,103 +94,75 @@ public function postProcess()
]
));
}
}

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

$checkoutData = CheckoutData::create(
(int) $cartId,
$response->getPaymentMeans()->getBrand()->getPaymentMethod(),
(int) $isBusinessLicence
);

$checkoutData->setIsAuthorizedOrder(true);
$checkoutData->setOrderStatus($response->getTransaction()->getStatus());

/** @var CheckoutProcessor $checkoutProcessor **/
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
$checkoutProcessor->run($checkoutData);

if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}

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

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

$order = new Order($orderId);

if (
(!$response->getLiability()->getLiabilityShift() &&
in_array($order->payment, SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS) &&
$paymentBehaviourWithout3DS === SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL) ||
$response->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(
} else {
$this->context->smarty->assign(
'checkStatusEndpoint',
$this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
'return',
[
'cartId' => $cartId,
'ajax' => 1,
'action' => 'getStatus',
'secureKey' => $secureKey,
'orderId' => $orderId,
'moduleId' => $moduleId,
'cartId' => $cartId,
],
true
));
}
)
);
parent::setTemplate('saferpay_wait.tpl');
}
}

if ((int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$response->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
) {
$orderStatusService->capture(new Order($orderId));
}

Tools::redirect($this->context->link->getModuleLink(
/**
* @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->canceled || !$saferPayOrder->id_order) {
$href = $this->context->link->getModuleLink(
$this->module->name,
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
ControllerName::FAIL,
[
'cartId' => $cartId,
'orderId' => $orderId,
'moduleId' => $moduleId,
'secureKey' => $secureKey,
'selectedCard' => $selectedCard,
'moduleId' => $moduleId,
],
true
));
} catch (Exception $e) {
PrestaShopLogger::addLog(
sprintf(
'Failed to assert transaction. Message: %s. File name: %s',
$e->getMessage(),
self::FILENAME
)
);

Tools::redirect($this->context->link->getModuleLink(
} else {
$href = $this->context->link->getModuleLink(
$this->module->name,
'failValidation',
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
[
'cartId' => $cartId,
'orderId' => $orderId,
'orderId' => $saferPayOrder->id_order,
'moduleId' => $moduleId,
'secureKey' => $secureKey,
],
true
));
'selectedCard' => $selectedCard,
]
);
}

// @todo: this place needs attention after a2a pr merged - need to check if pending status works well
$this->ajaxDie(json_encode([
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || isset($saferPayOrder->pending),
'href' => $href
]));
}

private function getSuccessControllerName($isBusinessLicence, $fieldToken)
Expand All @@ -227,42 +179,4 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken)

return $successController;
}

/**
* @param int $orderId
* @param int $selectedCard
*
* @return \Invertus\SaferPay\DTO\Response\Assert\AssertBody
* @throws Exception
*/
private function executeTransaction($orderId, $selectedCard)
{
/** @var SaferPayTransactionAuthorization $saferPayTransactionAuthorization */
$saferPayTransactionAuthorization = $this->module->getService(SaferPayTransactionAuthorization::class);

$response = $saferPayTransactionAuthorization->authorize(
$orderId,
$selectedCard === SaferPayConfig::CREDIT_CARD_OPTION_SAVE,
$selectedCard
);

return $response;
}

/**
* @param int $cartId
* @param int $isBusinessLicence
*
* @return \Invertus\SaferPay\DTO\Response\Assert\AssertBody|null
* @throws Exception
*/
private function executePaymentPageAssertion($cartId, $isBusinessLicence)
{

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

return $assertionResponse;
}
}
118 changes: 118 additions & 0 deletions views/templates/front/saferpay_wait.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{**
*NOTICE OF LICENSE
*
*This source file is subject to the Open Software License (OSL 3.0)
*that is bundled with this package in the file LICENSE.txt.
*It is also available through the world-wide-web at this URL:
*http://opensource.org/licenses/osl-3.0.php
*If you did not receive a copy of the license and are unable to
*obtain it through the world-wide-web, please send an email
*to [email protected] so we can send you a copy immediately.
*
*DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
*versions in the future. If you wish to customize PrestaShop for your
*needs please refer to http://www.prestashop.com for more information.
*
*@author INVERTUS UAB www.invertus.eu <[email protected]>
*@copyright SIX Payment Services
*@license SIX Payment Services
*}
<h2>{l s='Awaiting payment status' mod='saferpay'}</h2>
<div class="saferpay-spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<style>
.saferpay-spinner {
margin: 100px auto;
width: 50px;
height: 40px;
text-align: center;
font-size: 10px;
}
.saferpay-spinner > div {
background-color: #333;
height: 100%;
width: 6px;
display: inline-block;
-webkit-animation: sk-stretchdelay 1.2s infinite ease-in-out;
animation: sk-stretchdelay 1.2s infinite ease-in-out;
}
.saferpay-spinner .rect2 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.saferpay-spinner .rect3 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
.saferpay-spinner .rect4 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.saferpay-spinner .rect5 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
@-webkit-keyframes sk-stretchdelay {
0%, 40%, 100% {
-webkit-transform: scaleY(0.4)
}
20% {
-webkit-transform: scaleY(1.0)
}
}
@keyframes sk-stretchdelay {
0%, 40%, 100% {
transform: scaleY(0.4);
-webkit-transform: scaleY(0.4);
}
20% {
transform: scaleY(1.0);
-webkit-transform: scaleY(1.0);
}
}
</style>
<script type="text/javascript">
(function awaitSaferpayPaymentStatus() {
var timeout = 3000;
var request = new XMLHttpRequest();
// nofilter is needed for url with variables
request.open('GET', '{$checkStatusEndpoint|escape:'javascript':'UTF-8' nofilter}', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
try {
var data = JSON.parse(request.responseText);
if (data.success && data.isFinished) {
window.location.href = data.href;
return;
}
} catch (e) {
}
}
setTimeout(awaitSaferpayPaymentStatus, timeout);
};
request.onerror = function() {
setTimeout(awaitSaferpayPaymentStatus, timeout);
};
request.send();
}());
</script>

0 comments on commit 78958f0

Please sign in to comment.