From bfbdde74903e64734a6eca136b78c15ff92993e8 Mon Sep 17 00:00:00 2001 From: RokPopov Date: Mon, 30 Sep 2024 10:51:22 +0200 Subject: [PATCH] validate terms&conditions, make sure button is disabled if they are not checked --- Model/Ui/AdyenGenericConfigProvider.php | 5 +++ .../web/js/model/adyen-configuration.js | 3 ++ .../method-renderer/adyen-paypal-method.js | 37 ++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Model/Ui/AdyenGenericConfigProvider.php b/Model/Ui/AdyenGenericConfigProvider.php index bc6690ff6..4ed62c397 100644 --- a/Model/Ui/AdyenGenericConfigProvider.php +++ b/Model/Ui/AdyenGenericConfigProvider.php @@ -18,6 +18,7 @@ use Magento\Framework\App\RequestInterface; use Magento\Framework\UrlInterface; use Magento\Store\Model\StoreManagerInterface; +use Magento\CheckoutAgreements\Model\AgreementsConfigProvider; class AdyenGenericConfigProvider implements ConfigProviderInterface { @@ -28,6 +29,7 @@ class AdyenGenericConfigProvider implements ConfigProviderInterface protected RequestInterface $request; protected UrlInterface $url; private Config $adyenConfigHelper; + private AgreementsConfigProvider $agreementsConfigProvider; /** * This data member will be passed to the js frontend. It will be used to map the method code (adyen_ideal) to the * corresponding txVariant (ideal). The txVariant will then be used to instantiate the component @@ -45,6 +47,7 @@ public function __construct( StoreManagerInterface $storeManager, RequestInterface $request, UrlInterface $url, + AgreementsConfigProvider $agreementsConfigProvider, array $txVariants = [], array $customMethodRenderers = [] ) { @@ -53,6 +56,7 @@ public function __construct( $this->storeManager = $storeManager; $this->request = $request; $this->url = $url; + $this->agreementsConfigProvider = $agreementsConfigProvider; $this->txVariants = $txVariants; $this->customMethodRenderers = $customMethodRenderers; } @@ -88,6 +92,7 @@ public function getConfig(): array 'checkout/onepage/success', ['_secure' => $this->request->isSecure()] ); + $config['payment']['adyen']['agreementsConfig'] = $this->agreementsConfigProvider->getConfig(); return $config; } diff --git a/view/frontend/web/js/model/adyen-configuration.js b/view/frontend/web/js/model/adyen-configuration.js index 7cb6443d5..7b96a8f61 100644 --- a/view/frontend/web/js/model/adyen-configuration.js +++ b/view/frontend/web/js/model/adyen-configuration.js @@ -42,6 +42,9 @@ define( getCustomerStreetLinesEnabled: function () { return window.checkoutConfig.payment.customerStreetLinesEnabled; }, + getAgreementsConfig: function () { + return window.checkoutConfig.payment.adyen.agreementsConfig; + } }; }, ); diff --git a/view/frontend/web/js/view/payment/method-renderer/adyen-paypal-method.js b/view/frontend/web/js/view/payment/method-renderer/adyen-paypal-method.js index 7cda6b53f..c52322518 100644 --- a/view/frontend/web/js/view/payment/method-renderer/adyen-paypal-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/adyen-paypal-method.js @@ -11,12 +11,16 @@ define( [ 'Magento_Checkout/js/model/quote', 'Adyen_Payment/js/view/payment/method-renderer/adyen-pm-method', + 'Adyen_Payment/js/model/adyen-configuration', 'Magento_Checkout/js/model/full-screen-loader', + 'jquery' ], function( quote, adyenPaymentMethod, - fullScreenLoader + adyenConfiguration, + fullScreenLoader, + $ ) { return adyenPaymentMethod.extend({ placeOrderButtonVisible: false, @@ -27,6 +31,25 @@ define( let baseComponentConfiguration = this._super(); let paypalConfiguration = Object.assign(baseComponentConfiguration, paymentMethodsExtraInfo[paymentMethod.type].configuration); paypalConfiguration.showPayButton = true; + let agreementsConfig = adyenConfiguration.getAgreementsConfig(); + + if (null !== agreementsConfig) { + let self = this; + + paypalConfiguration.onInit = function (data, actions) { + actions.disable() + + $(document).off('change', '.checkout-agreements input').on('change', '.checkout-agreements input', function () { + self.updatePayPalButton(actions); + }); + } + + paypalConfiguration.onClick = function (data, actions) { + if(!self.validate()) { + console.error('Agreements configuration failed'); + } + } + } return paypalConfiguration }, @@ -38,8 +61,18 @@ define( handleOnFailure: function(response, component) { this.isPlaceOrderAllowed(true); fullScreenLoader.stopLoader(); + if (response && response.error) { + console.error('Error details:', response.error); + } component.handleReject(response); }, - }) + updatePayPalButton: function (actions) { + if (this.validate()) { + actions.enable(); + } else { + actions.disable(); + } + }, + }) } );