diff --git a/saferpayofficial.php b/saferpayofficial.php index 32ab76de..13b77747 100755 --- a/saferpayofficial.php +++ b/saferpayofficial.php @@ -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() diff --git a/src/Config/SaferPayConfig.php b/src/Config/SaferPayConfig.php index 0490cfad..8b640b0c 100755 --- a/src/Config/SaferPayConfig.php +++ b/src/Config/SaferPayConfig.php @@ -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 diff --git a/src/Presentation/Loader/PaymentFormAssetLoader.php b/src/Presentation/Loader/PaymentFormAssetLoader.php index 84f6f12c..c6f826b4 100755 --- a/src/Presentation/Loader/PaymentFormAssetLoader.php +++ b/src/Presentation/Loader/PaymentFormAssetLoader.php @@ -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; @@ -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' => [ @@ -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()) { @@ -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'); + } } diff --git a/src/Provider/OpcModulesProvider.php b/src/Provider/OpcModulesProvider.php new file mode 100644 index 00000000..9740cbe6 --- /dev/null +++ b/src/Provider/OpcModulesProvider.php @@ -0,0 +1,47 @@ + + *@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 ''; + } +} \ No newline at end of file diff --git a/src/Validation/ValidateIsAssetsRequired.php b/src/Validation/ValidateIsAssetsRequired.php new file mode 100644 index 00000000..d62beff5 --- /dev/null +++ b/src/Validation/ValidateIsAssetsRequired.php @@ -0,0 +1,56 @@ + + *@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; + } +} \ No newline at end of file diff --git a/views/css/front/saferpay_checkout.css b/views/css/front/saferpay_checkout.css index 7a88f011..a100ae4c 100755 --- a/views/css/front/saferpay_checkout.css +++ b/views/css/front/saferpay_checkout.css @@ -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; +} diff --git a/views/js/front/opc/onepagecheckoutps/hosted_fields.js b/views/js/front/opc/onepagecheckoutps/hosted_fields.js new file mode 100644 index 00000000..2b8162f7 --- /dev/null +++ b/views/js/front/opc/onepagecheckoutps/hosted_fields.js @@ -0,0 +1,59 @@ +/** + *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 license@prestashop.com 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 + *@copyright SIX Payment Services + *@license SIX Payment Services + */ + +$(document).ready(function () { + $('body').on('change', "input[name^='saved_card_']", function () { + var $selectedCard = $(this); + var method = $selectedCard.closest('div.saved_cards').find('.saved_card_method').val(); + $("input[name='selectedCreditCard_" + method + "']").val($selectedCard.val()); + }); +}); + +$('body').on('submit', '[id^=pay-with-][id$=-form] form', function (event) { + event.preventDefault(); + + var selectedCardMethod = $(this).find("[name=saved_card_method]").val(); + var selectedCard = $(this).find("[name=selectedCreditCard_" + selectedCardMethod + "]").val(); + + //NOTE: not saved card chosen, continuing with normal procedures. + if (selectedCard <= 0) { + event.target.submit(); + + return; + } + + $.ajax(saferpay_official_ajax_url, { + method: 'POST', + data: { + action: 'submitHostedFields', + paymentMethod: selectedCardMethod, + selectedCard: selectedCard, + isBusinessLicence: 1, + ajax: 1 + }, + success: function (response) { + var data = jQuery.parseJSON(response); + + window.location = data.url; + }, + }); +}); \ No newline at end of file diff --git a/views/js/front/opc/supercheckout/hosted_fields.js b/views/js/front/opc/supercheckout/hosted_fields.js new file mode 100644 index 00000000..2b8162f7 --- /dev/null +++ b/views/js/front/opc/supercheckout/hosted_fields.js @@ -0,0 +1,59 @@ +/** + *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 license@prestashop.com 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 + *@copyright SIX Payment Services + *@license SIX Payment Services + */ + +$(document).ready(function () { + $('body').on('change', "input[name^='saved_card_']", function () { + var $selectedCard = $(this); + var method = $selectedCard.closest('div.saved_cards').find('.saved_card_method').val(); + $("input[name='selectedCreditCard_" + method + "']").val($selectedCard.val()); + }); +}); + +$('body').on('submit', '[id^=pay-with-][id$=-form] form', function (event) { + event.preventDefault(); + + var selectedCardMethod = $(this).find("[name=saved_card_method]").val(); + var selectedCard = $(this).find("[name=selectedCreditCard_" + selectedCardMethod + "]").val(); + + //NOTE: not saved card chosen, continuing with normal procedures. + if (selectedCard <= 0) { + event.target.submit(); + + return; + } + + $.ajax(saferpay_official_ajax_url, { + method: 'POST', + data: { + action: 'submitHostedFields', + paymentMethod: selectedCardMethod, + selectedCard: selectedCard, + isBusinessLicence: 1, + ajax: 1 + }, + success: function (response) { + var data = jQuery.parseJSON(response); + + window.location = data.url; + }, + }); +}); \ No newline at end of file diff --git a/views/js/front/opc/thecheckout/hosted_fields.js b/views/js/front/opc/thecheckout/hosted_fields.js new file mode 100644 index 00000000..ed2299f3 --- /dev/null +++ b/views/js/front/opc/thecheckout/hosted_fields.js @@ -0,0 +1,95 @@ +/** + *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 license@prestashop.com 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 + *@copyright SIX Payment Services + *@license SIX Payment Services + */ + +var selectedCard = null; + +$(document).ready(function () { + let savedCardMethod = $('input[name="saved_card_method"]'); + + if (!savedCardMethod.length) { + return; + } +}); + +$(document).on('change', 'input[name^="saved_card_"]', function () { + var method = $('[data-module-name*="saferpayofficial"]:checked').closest('div').find('.h6').text().toUpperCase(); + updateCheckedCardValue(); + $("input[name='selectedCreditCard_" + method + "']").val(selectedCard); +}); + +$('body').on('submit', '[id^=pay-with-][id$=-form] form', function (e) { + var idPayment = $(this).parent('div').attr('id').match(/\d+/)[0]; + handleSubmit(e, idPayment); +}); + +function handleSubmit(event, idPayment) { + event.preventDefault(); + + let selectedCardMethod = $('#' + "payment-option-" + idPayment + "-additional-information").find('input[type="hidden"]').val(); + let form = $(document).find("[name=selectedCreditCard_" + selectedCardMethod + "]").closest('form'); + let hiddenInput = form.find("input[name='selectedCreditCard_" + selectedCardMethod + "']"); + + /* + * NOTE: + * when user just press payment method + * but not touched what to do with card + */ + if (selectedCard === null) { + selectedCard = hiddenInput.val(); + } + + hiddenInput.val(selectedCard); + + /* + * NOTE: + * not saved card chosen, continuing with normal procedures. + */ + if (parseInt(selectedCard) <= 0 || selectedCard === null || selectedCard === undefined) { + event.target.submit(); + + return; + } + + $.ajax(saferpay_official_ajax_url, { + method: 'POST', + data: { + action: 'submitHostedFields', + paymentMethod: selectedCardMethod, + selectedCard: parseInt(selectedCard), + isBusinessLicence: 1, + ajax: 1 + }, + success: function (response) { + var data = jQuery.parseJSON(response); + + window.location = data.url; + }, + }); +} + +function updateCheckedCardValue() { + $('input[name^="saved_card_"]:checked').each(function() { + if ($(this).is(':visible')) { + selectedCard = $(this).val(); + } + }); +} \ No newline at end of file