Skip to content

Commit

Permalink
Merge pull request #140 from Invertus/1.2.0
Browse files Browse the repository at this point in the history
1.2.0
  • Loading branch information
mant02 authored Feb 9, 2024
2 parents 0b9dd14 + cbd826b commit 5700a45
Show file tree
Hide file tree
Showing 66 changed files with 1,809 additions and 503 deletions.
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,13 @@
## [1.1.7] - *
- BO : Added PrestaShop module security validations
- FO : Added PrestaShop module security validations

## [1.1.8] - *
- BO : Added a toggle setting for Saferpay email sending option
- BO : Added a configuration field for the customization of the description parameter
- BO : Increased module's API version
- BO : Added a more descriptive payment method name in invoices
- BO : Additional improvements and fixes

- ## [1.2.0] - *
- BO : Added order creation after authorization functionality
47 changes: 47 additions & 0 deletions controllers/admin/AdminSaferPayOfficialSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

class AdminSaferPayOfficialSettingsController extends ModuleAdminController
{
const FILE_NAME = 'AdminSaferPayOfficialSettingsController';

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -267,6 +269,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('Select the option to determine whether the order should be created'),
'form_group_class' => 'thumbs_chose',
],
],
'buttons' => [
'save_and_connect' => [
Expand Down Expand Up @@ -316,6 +329,13 @@ public function initOptions()
'cast' => 'intval',
'type' => 'bool',
],
SaferPayConfig::SAFERPAY_ALLOW_SAFERPAY_SEND_CUSTOMER_MAIL => [
'title' => $this->l('Send an email from Saferpay on payment completion'),
'desc' => $this->l('With this setting enabled an email from the Saferpay system will be sent to the customer'),
'validation' => 'isBool',
'cast' => 'intval',
'type' => 'bool',
],
SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL => [
'title' => $this->l('Send new order mail on authorization'),
'desc' => $this->l('Receive a notification when an order is authorized by Saferpay (Using the Mail alert module)'),
Expand All @@ -341,6 +361,7 @@ public function initOptions()
}

$this->fields_options[] = $this->getFieldOptionsOrderState();
$this->fields_options[] = $this->displayConfigurationSettings();
}

private function getFieldOptionsOrderState()
Expand Down Expand Up @@ -374,6 +395,32 @@ private function getFieldOptionsOrderState()
];
}

/**
* @return array
*/
private function displayConfigurationSettings()
{
return [
'title' => $this->module->l('Configuration', self::FILE_NAME),
'fields' => [
SaferPayConfig::SAFERPAY_PAYMENT_DESCRIPTION => [
'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'
],
],
'buttons' => [
'save_and_connect' => [
'title' => $this->module->l('Save', self::FILE_NAME),
'icon' => 'process-icon-save',
'class' => 'btn btn-default pull-right',
'type' => 'submit',
],
],
];
}

public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
Expand Down
99 changes: 25 additions & 74 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@
*/

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\CheckoutController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
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 +49,28 @@ public function postProcess()
private function submitHostedFields()
{
try {
if (!Order::getOrderByCartId($this->context->cart->id)) {
$this->validateOrder();
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(
// refactor it to create checkout data from validator request
$checkoutData = CheckoutData::create(
(int) $this->context->cart->id,
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);

/** @var CheckoutController $checkoutController */
$checkoutController = $this->module->getService(CheckoutController::class);
$redirectUrl = $checkoutController->execute($checkoutData);

if (empty($redirectUrl)) {
$redirectUrl = $this->getRedirectionToControllerUrl('successHosted');
Expand All @@ -84,40 +89,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 +108,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 || empty($this->secure_key)) {
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
Loading

0 comments on commit 5700a45

Please sign in to comment.