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-204/ Order creation after authorization #136

Merged
merged 18 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@
## [1.1.7] - *
- BO : Added PrestaShop module security validations
- FO : Added PrestaShop module security validations
-
- ## [1.2.0] - *
- BO : TODO
GytisZum marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions controllers/admin/AdminSaferPayOfficialSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ public function initOptions()
'desc' => $this->l('If set to true, the refund will be rejected if the sum of authorized refunds exceeds the capture value.'),
'form_group_class' => 'thumbs_chose',
],
SaferPayConfig::SAFERPAY_ORDER_CREATION_AFTER_AUTHORIZATION => [
'type' => 'radio',
'title' => $this->l('Order creation rule'),
'validation' => 'isInt',
'choices' => [
1 => $this->l('After authorization'),
0 => $this->l('Before authorization'),
],
'desc' => $this->l('TODO'),
GytisZum marked this conversation as resolved.
Show resolved Hide resolved
'form_group_class' => 'thumbs_chose',
],
],
'buttons' => [
'save_and_connect' => [
Expand Down
98 changes: 24 additions & 74 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
*/

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\EntityBuilder\SaferPayOrderBuilder;
use Invertus\SaferPay\Repository\SaferPayCardAliasRepository;
use Invertus\SaferPay\Service\SaferPayInitialize;
use Invertus\SaferPay\Controller\Front\PaymentFrontController;
use Invertus\SaferPay\Enum\ControllerName;

if (!defined('_PS_VERSION_')) {
exit;
}

class SaferPayOfficialAjaxModuleFrontController extends ModuleFrontController
{
const FILE_NAME = 'ajax';

/** @var SaferPayOfficial */
public $module;

public function postProcess()
{
switch (Tools::getValue('action')) {
Expand All @@ -44,28 +48,28 @@ public function postProcess()
private function submitHostedFields()
{
try {
if (!Order::getOrderByCartId($this->context->cart->id)) {
$this->validateOrder();
/** @var PaymentFrontController $paymentFrontController */
$paymentFrontController = $this->module->getService(PaymentFrontController::class);

if (Order::getOrderByCartId($this->context->cart->id)) {
$this->ajaxDie(json_encode([
'error' => true,
'message' => $this->module->l('Order already exists', self::FILE_NAME),
'url' => $this->getRedirectionToControllerUrl('fail'),
]));
}

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

$selectedCard = Tools::getValue('selectedCard');

$alias = $cardAliasRep->getSavedCardAliasFromId($selectedCard);

/** @var SaferPayInitialize $initializeService */
$initializeService = $this->module->getService(SaferPayInitialize::class);
$initializeBody = $initializeService->initialize(
$initializeResponse = $paymentFrontController->create(
$this->context->cart,
Tools::getValue('paymentMethod'),
(int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE),
$selectedCard,
$alias,
Tools::getValue('fieldToken')
Tools::getValue('selectedCard'),
Tools::getValue('fieldToken'),
ControllerName::SUCCESS_HOSTED,
true
);
$this->createSaferPayOrder($initializeBody);
$redirectUrl = $this->getRedirectionUrl($initializeBody);

$redirectUrl = $paymentFrontController->getRedirectionUrl($initializeResponse);
GytisZum marked this conversation as resolved.
Show resolved Hide resolved

if (empty($redirectUrl)) {
$redirectUrl = $this->getRedirectionToControllerUrl('successHosted');
Expand All @@ -84,40 +88,6 @@ private function submitHostedFields()
}
}

/**
* @param object $initializeBody
*
* @return string
*/
private function getRedirectionUrl($initializeBody)
{
if (isset($initializeBody->RedirectUrl)) {
return $initializeBody->RedirectUrl;
}

if (isset($initializeBody->Redirect->RedirectUrl)) {
return $initializeBody->Redirect->RedirectUrl;
}

return '';
}

/**
* @param object $initializeBody
*/
private function createSaferPayOrder($initializeBody)
{
/** @var Invertus\SaferPay\EntityBuilder\SaferPayOrderBuilder $saferPayOrderBuilder */
$saferPayOrderBuilder = $this->module->getService(SaferPayOrderBuilder::class);
$saferPayOrderBuilder->create(
$initializeBody,
$this->context->cart,
$this->context->customer,
true,
Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE)
);
}

/**
* @param string $controllerName
*
Expand All @@ -137,24 +107,4 @@ private function getRedirectionToControllerUrl($controllerName)
true
);
}

/**
* @throws Exception
*/
private function validateOrder()
{
$customer = new Customer($this->context->cart->id_customer);

$this->module->validateOrder(
$this->context->cart->id,
Configuration::get(SaferPayConfig::SAFERPAY_ORDER_STATE_CHOICE_AWAITING_PAYMENT),
(float) $this->context->cart->getOrderTotal(),
Tools::getValue('paymentMethod'),
null,
[],
(int) $this->context->currency->id,
false,
$customer->secure_key
);
}
}
24 changes: 7 additions & 17 deletions controllers/front/fail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ class SaferPayOfficialFailModuleFrontController extends AbstractSaferPayControll
{
const FILENAME = 'fail';

/**
* ID Order Variable Declaration.
*
* @var
*/
private $id_order;

/**
* Security Key Variable Declaration.
*
Expand All @@ -67,31 +60,28 @@ public function init()
if (!SaferPayConfig::isVersion17()) {
return parent::init();
}

parent::init();

$this->id_cart = (int) Tools::getValue('cartId', 0);

$redirectLink = 'index.php?controller=history';

$this->id_order = Order::getOrderByCartId((int) $this->id_cart);
$this->secure_key = Tools::getValue('secureKey');
$order = new Order((int) $this->id_order);

if (!$this->id_order || !$this->module->id || !$this->secure_key || empty($this->secure_key)) {
$cart = new Cart($this->id_cart);

if (!$this->module->id || !$this->secure_key || empty($this->secure_key)) {
GytisZum marked this conversation as resolved.
Show resolved Hide resolved
Tools::redirect($redirectLink . (Tools::isSubmit('slowvalidation') ? '&slowvalidation' : ''));
}

if ((string) $this->secure_key !== (string) $order->secure_key ||
(int) $order->id_customer !== (int) $this->context->customer->id ||
!Validate::isLoadedObject($order)
if ((string) $this->secure_key !== (string) $cart->secure_key ||
(int) $cart->id_customer !== (int) $this->context->customer->id ||
!Validate::isLoadedObject($cart)
) {
Tools::redirect($redirectLink);
}

if ($order->module !== $this->module->name) {
Tools::redirect($redirectLink);
}

/** @var CartDuplicationService $cartDuplicationService */
$cartDuplicationService = $this->module->getService(CartDuplicationService::class);
$cartDuplicationService->restoreCart($this->id_cart);
Expand Down
3 changes: 2 additions & 1 deletion controllers/front/failIFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Enum\ControllerName;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function setMedia()

$failUrl = $this->context->link->getModuleLink(
$this->module->name,
'fail',
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
Expand Down
10 changes: 8 additions & 2 deletions controllers/front/failValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ public function postProcess()

Tools::redirect($redirectLink);
}

$order = new Order($orderId);
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);

if (Validate::isLoadedObject($order)) {
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);
}

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

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

$saferPayOrderId = $orderRepo->getIdByOrderId($orderId);
$saferPayOrderId = $orderRepo->getIdByCartId($cartId);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);
$saferPayOrder->canceled = 1;
$saferPayOrder->update();
Expand Down
62 changes: 20 additions & 42 deletions controllers/front/iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\EntityBuilder\SaferPayOrderBuilder;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Repository\SaferPayCardAliasRepository;
use Invertus\SaferPay\Service\SaferPayInitialize;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -73,48 +70,33 @@ public function postProcess()
if (!Validate::isLoadedObject($customer)) {
Tools::redirect($redirectLink);
}

$currency = $this->context->currency;
$total = (float) $cart->getOrderTotal();

$orderId = Order::getOrderByCartId($cart->id);
if (!$orderId) {
$paymentMethod = Tools::getValue('saved_card_method');
$this->module->validateOrder(
$cart->id,
Configuration::get(SaferPayConfig::SAFERPAY_ORDER_STATE_CHOICE_AWAITING_PAYMENT),
$total,
$paymentMethod,
null,
[],
(int) $currency->id,
false,
$customer->secure_key
);
}
}

public function initContent()
{
parent::initContent();

$paymentMethod = Tools::getValue('saved_card_method');
$selectedCard = Tools::getValue("selectedCreditCard_{$paymentMethod}");

if (!SaferPayConfig::isVersion17()) {
$selectedCard = Tools::getValue("saved_card_{$paymentMethod}");
}
/** @var SaferPayOrderBuilder $saferPayOrderBuilder */
$saferPayOrderBuilder = $this->module->getService(SaferPayOrderBuilder::class);
$isBusinessLicence = Tools::getValue(\Invertus\SaferPay\Config\SaferPayConfig::IS_BUSINESS_LICENCE);

/** @var SaferPayInitialize $initializeService */
$initializeService = $this->module->getService(SaferPayInitialize::class);
try {
/** @var SaferPayCardAliasRepository $cardAliasRep */
$cardAliasRep = $this->module->getService(SaferPayCardAliasRepository::class);
$alias = $cardAliasRep->getSavedCardAliasFromId($selectedCard);
$response = $initializeService->initialize($paymentMethod, $isBusinessLicence, $selectedCard, $alias);
} catch (Exception $e) {
$redirectLink = $this->context->link->getModuleLink(
/** @var \Invertus\SaferPay\Controller\Front\PaymentFrontController $paymentFrontController */
$paymentFrontController = $this->module->getModuleContainer()->get(\Invertus\SaferPay\Controller\Front\PaymentFrontController::class);

$initializeResponse = $paymentFrontController->create(
$this->context->cart,
$paymentMethod,
Tools::getValue(\Invertus\SaferPay\Config\SaferPayConfig::IS_BUSINESS_LICENCE),
$selectedCard
);

$redirectUrl = $paymentFrontController->getRedirectionUrl($initializeResponse);
} catch (\Exception $exception) {
$redirectUrl = $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
Expand All @@ -125,22 +107,18 @@ public function initContent()
],
true
);
$this->redirectWithNotifications($redirectLink);
$this->redirectWithNotifications($redirectUrl);
}
$saferPayOrderBuilder->create(
$response,
$this->context->cart,
$this->context->customer,
true,
$isBusinessLicence
);

$this->context->smarty->assign([
'redirect' => $response->Redirect->RedirectUrl,
'redirect' => $redirectUrl,
]);

if (SaferPayConfig::isVersion17()) {
$this->setTemplate(SaferPayConfig::SAFERPAY_TEMPLATE_LOCATION . '/front/saferpay_iframe.tpl');
return;
}

$this->setTemplate('saferpay_iframe_16.tpl');
}

Expand Down
Loading