Skip to content

Commit

Permalink
Merge pull request #208 from Invertus/SV-28/thecheckout
Browse files Browse the repository at this point in the history
TheCheckout compatibility
  • Loading branch information
MarijusCoding authored Oct 22, 2024
2 parents a53468a + f34b434 commit 642d17c
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 25 deletions.
28 changes: 8 additions & 20 deletions saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,31 +344,19 @@ public function hookDisplayAdminOrder(array $params)

public function hookActionFrontControllerSetMedia()
{
/** @var \Invertus\SaferPay\Validation\ValidateIsAssetsRequired $validateIsAssetsRequired */
$validateIsAssetsRequired = $this->getService(\Invertus\SaferPay\Validation\ValidateIsAssetsRequired::class);

if (!$validateIsAssetsRequired->run($this->context->controller)) {
return;
}

/** @var \Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader $paymentFormAssetsLoader */
$paymentFormAssetsLoader = $this->getService(\Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader::class);

$paymentFormAssetsLoader->register($this->context->controller);

if ($this->context->controller instanceof OrderController) {
if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) {
$this->context->controller->registerJavascript(
'saved-card',
'modules/' . $this->name . '/views/js/front/saferpay_saved_card.js'
);

$this->context->controller->addCSS("{$this->getPathUri()}views/css/front/saferpay_checkout.css");
} else {
$this->context->controller->addCSS("{$this->getPathUri()}views/css/front/saferpay_checkout_16.css");
$this->context->controller->addJS("{$this->getPathUri()}views/js/front/saferpay_saved_card_16.js");
$fieldsLibrary = \Invertus\SaferPay\Config\SaferPayConfig::FIELDS_LIBRARY;
$configSuffix = \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix();
$this->context->controller->addJs(Configuration::get($fieldsLibrary . $configSuffix));
}

/** @var \Invertus\SaferPay\Service\SaferPayErrorDisplayService $errorDisplayService */
$errorDisplayService = $this->getService(\Invertus\SaferPay\Service\SaferPayErrorDisplayService::class);
$errorDisplayService->showCookieError('saferpay_payment_canceled_error');
}
$paymentFormAssetsLoader->registerErrorBags();
}

public function hookDisplayCustomerAccount()
Expand Down
9 changes: 9 additions & 0 deletions src/Config/SaferPayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ class SaferPayConfig
const LOG_SEVERITY_LEVEL_MAJOR = 4;
const TRANSACTION_ALREADY_CAPTURED = 'TRANSACTION_ALREADY_CAPTURED';

const ONE_PAGE_CHECKOUT_MODULE = 'onepagecheckoutps';
const THE_CHECKOUT_MODULE = 'thecheckout';
const SUPER_CHECKOUT_MODULE = 'supercheckout';
const OPC_MODULE_LIST = [
self::ONE_PAGE_CHECKOUT_MODULE,
self::THE_CHECKOUT_MODULE,
self::SUPER_CHECKOUT_MODULE,
];

public static function supportsOrderCapture($paymentMethod)
{
//payments that DOES NOT SUPPORT capture
Expand Down
121 changes: 116 additions & 5 deletions src/Presentation/Loader/PaymentFormAssetLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@

namespace Invertus\SaferPay\Presentation\Loader;

use Configuration;
use Invertus\SaferPay\Adapter\LegacyContext;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\DTO\Request\Order;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Enum\PaymentType;
use Invertus\SaferPay\Factory\ModuleFactory;
use Invertus\SaferPay\Provider\OpcModulesProvider;
use Media;
use OrderControllerCore;
use SaferPayOfficial;
Expand All @@ -41,19 +45,18 @@ class PaymentFormAssetLoader
private $module;
/** @var LegacyContext */
private $context;
/** @var OpcModulesProvider $opcModuleProvider */
private $opcModulesProvider;

public function __construct(ModuleFactory $module, LegacyContext $context)
public function __construct(ModuleFactory $module, LegacyContext $context, OpcModulesProvider $opcModulesProvider)
{
$this->module = $module->getModule();
$this->context = $context;
$this->opcModulesProvider = $opcModulesProvider;
}

public function register($controller)
{
if (!$controller instanceof OrderControllerCore) {
return;
}

Media::addJsDef([
'saferpay_official_ajax_url' => $this->context->getLink()->getModuleLink('saferpayofficial', ControllerName::AJAX),
'saferpay_payment_types' => [
Expand All @@ -63,17 +66,117 @@ public function register($controller)
],
]);

$opcModule = $this->opcModulesProvider->get();

switch ($opcModule) {
case SaferPayConfig::ONE_PAGE_CHECKOUT_MODULE:
$this->registerOnePageCheckoutAssets($controller);
break;
case SaferPayConfig::THE_CHECKOUT_MODULE:
$this->registerTheCheckoutAssets($controller);
break;
case SaferPayConfig::SUPER_CHECKOUT_MODULE:
$this->registerSuperCheckoutAssets($controller);
break;
default:
$this->registerDefaultCheckoutAssets($controller);
}
}

private function registerOnePageCheckoutAssets($controller)
{
if (!$controller instanceof \OrderControllerCore) {
return;
}

$controller->addCSS("{$this->module->getPathUri()}views/css/front/saferpay_checkout.css");

if (method_exists($controller, 'registerJavascript')) {
$controller->registerJavascript(
'saved_card_hosted_fields_opc',
"modules/saferpayofficial/views/js/front/opc/onepagecheckoutps/hosted_fields.js"
);
} else {
$controller->addJs(
$this->module->getPathUri() . 'views/js/front/opc/onepagecheckoutps/hosted_fields.js',
false
);
}
}

private function registerTheCheckoutAssets($controller)
{
if (!$controller instanceof \TheCheckoutModuleFrontController) {
return;
}

$controller->addCSS("{$this->module->getPathUri()}views/css/front/saferpay_checkout.css");

if (method_exists($controller, 'registerJavascript')) {
$controller->registerJavascript(
'saved_card_hosted_fields_opc',
"modules/saferpayofficial/views/js/front/opc/thecheckout/hosted_fields.js"
);
} else {
$controller->addJs(
$this->module->getPathUri() . 'views/js/front/opc/thecheckout/hosted_fields.js',
false
);
}
}

private function registerSuperCheckoutAssets($controller)
{
if (!$controller instanceof \SupercheckoutSupercheckoutModuleFrontController) {
return;
}

$controller->addCSS("{$this->module->getPathUri()}views/css/front/saferpay_checkout.css");

if (method_exists($controller, 'registerJavascript')) {
$controller->registerJavascript(
'saved_card_hosted_fields_opc',
"modules/saferpayofficial/views/js/front/opc/supercheckout/hosted_fields.js"
);
} else {
$controller->addJs(
$this->module->getPathUri() . 'views/js/front/opc/supercheckout/hosted_fields.js',
false
);
}
}

private function registerDefaultCheckoutAssets($controller)
{
if (!$controller instanceof OrderControllerCore) {
return;
}

if (method_exists($controller, 'registerJavascript')) {
if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) {
$controller->registerJavascript(
'saved_card_hosted_fields',
"modules/saferpayofficial/views/js/front/hosted-templates/hosted_fields.js"
);

$controller->registerJavascript(
'saved-card',
'modules/' . $this->module->name . '/views/js/front/saferpay_saved_card.js'
);

$controller->registerStylesheet("",
"{$this->module->getPathUri()}views/css/front/saferpay_checkout.css");
} else {
$controller->registerJavascript(
'saved_card_hosted_fields',
"modules/saferpayofficial/views/js/front/hosted-templates/hosted_fields_16.js"
);

$controller->addCSS("{$this->module->getPathUri()}views/css/front/saferpay_checkout_16.css");
$controller->addJS("{$this->module->getPathUri()}views/js/front/saferpay_saved_card_16.js");
$fieldsLibrary = \Invertus\SaferPay\Config\SaferPayConfig::FIELDS_LIBRARY;
$configSuffix = \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix();
$controller->addJs(Configuration::get($fieldsLibrary . $configSuffix));
}
} else {
if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) {
Expand All @@ -89,4 +192,12 @@ public function register($controller)
}
}
}

public function registerErrorBags()
{
/** @var \Invertus\SaferPay\Service\SaferPayErrorDisplayService $errorDisplayService */
$errorDisplayService = $this->module->getService(\Invertus\SaferPay\Service\SaferPayErrorDisplayService::class);

$errorDisplayService->showCookieError('saferpay_payment_canceled_error');
}
}
47 changes: 47 additions & 0 deletions src/Provider/OpcModulesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
*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
*/

namespace Invertus\SaferPay\Provider;

use Invertus\SaferPay\Config\SaferPayConfig;

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

class OpcModulesProvider
{
/**
* @return string
*/
public function get()
{
foreach (SaferPayConfig::OPC_MODULE_LIST as $opcModule){
if (\Module::isInstalled($opcModule) && \Module::isEnabled($opcModule)) {
return $opcModule;
}
}

return '';
}
}
56 changes: 56 additions & 0 deletions src/Validation/ValidateIsAssetsRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
*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
*/

namespace Invertus\SaferPay\Validation;

use Invertus\SaferPay\Provider\OpcModulesProvider;
use FrontController;

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

class ValidateIsAssetsRequired
{
private $opcModulesProvider;

public function __construct(OpcModulesProvider $opcModulesProvider)
{
$this->opcModulesProvider = $opcModulesProvider;
}

/**
* It returns true if it's an OPC controller or an OrderController with products in the cart. Otherwise, it returns false.
*/
public function run(FrontController $controller)
{
$isOrderController = $controller instanceof \OrderControllerCore
|| $controller instanceof \ModuleFrontController && isset($controller->php_self) && $controller->php_self === 'order';

if (!empty($this->opcModulesProvider->get($controller))) {
return $isOrderController && !empty(\Context::getContext()->cart->getProducts());
}

return true;
}
}
21 changes: 21 additions & 0 deletions views/css/front/saferpay_checkout.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@
.payment-option img {
height: 24px;
}

.payment-logo {
max-width: 40px;
}

.payment-logo {
max-width: 40px;
}

input[type="radio"][name^="saved_card_"] {
opacity: 1 !important;
width: 15px !important;
position: relative !important;
vertical-align: middle !important;
margin: 0 !important;
}
.saved_credit_cards span {
padding-left: 3px !important;
vertical-align: middle !important;
margin: 0 !important;
}
Loading

0 comments on commit 642d17c

Please sign in to comment.