From c72a335cdd48bc678ed06c849d5e57df996e3a74 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 8 Nov 2023 09:46:52 +0200 Subject: [PATCH] Hide card button in previews when has acdc or separate gateway --- .../resources/js/gateway-settings.js | 17 +++++++++++++++++ .../src/Assets/SettingsPageAssets.php | 13 ++++++++++++- modules/ppcp-wc-gateway/src/WCGatewayModule.php | 3 ++- .../Assets/SettingsPagesAssetsTest.php | 3 ++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/resources/js/gateway-settings.js b/modules/ppcp-wc-gateway/resources/js/gateway-settings.js index afec3a426..8d703164f 100644 --- a/modules/ppcp-wc-gateway/resources/js/gateway-settings.js +++ b/modules/ppcp-wc-gateway/resources/js/gateway-settings.js @@ -96,6 +96,11 @@ document.addEventListener( renderPreview(settingsCallback, render); } + function currentTabId() { + const params = new URLSearchParams(location.search); + return params.has('ppcp-tab') ? params.get('ppcp-tab') : params.get('section'); + } + function shouldShowPayLaterButton() { const payLaterButtonLocations = document.querySelector('[name="ppcp[pay_later_button_locations][]"]'); @@ -106,6 +111,14 @@ document.addEventListener( return payLaterButtonInput.checked && payLaterButtonLocations.selectedOptions.length > 0 } + function shouldDisableCardButton() { + if (currentTabId() === 'ppcp-card-button-gateway') { + return false; + } + + return PayPalCommerceGatewaySettings.is_acdc_enabled || jQuery('#ppcp-allow_card_button_gateway').is(':checked'); + } + function getPaypalScriptSettings() { const disableFundingInput = jQuery('[name="ppcp[disable_funding][]"]'); let disabledSources = disableFundingInput.length > 0 ? disableFundingInput.val() : PayPalCommerceGatewaySettings.disabled_sources; @@ -130,6 +143,10 @@ document.addEventListener( disabledSources = disabledSources.concat('credit') } + if (shouldDisableCardButton()) { + disabledSources = disabledSources.concat('card'); + } + if (disabledSources?.length) { settings['disable-funding'] = disabledSources; } diff --git a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php index 8cfb07793..37571c184 100644 --- a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php @@ -96,6 +96,13 @@ class SettingsPageAssets { */ private $is_settings_page; + /** + * Whether the ACDC gateway is enabled. + * + * @var bool + */ + private $is_acdc_enabled; + /** * Assets constructor. * @@ -110,6 +117,7 @@ class SettingsPageAssets { * @param array $disabled_sources The list of disabled funding sources. * @param array $all_funding_sources The list of all existing funding sources. * @param bool $is_settings_page Whether it's a settings page of this plugin. + * @param bool $is_acdc_enabled Whether the ACDC gateway is enabled. */ public function __construct( string $module_url, @@ -122,7 +130,8 @@ class SettingsPageAssets { bool $is_pay_later_button_enabled, array $disabled_sources, array $all_funding_sources, - bool $is_settings_page + bool $is_settings_page, + bool $is_acdc_enabled ) { $this->module_url = $module_url; $this->version = $version; @@ -135,6 +144,7 @@ class SettingsPageAssets { $this->disabled_sources = $disabled_sources; $this->all_funding_sources = $all_funding_sources; $this->is_settings_page = $is_settings_page; + $this->is_acdc_enabled = $is_acdc_enabled; } /** @@ -223,6 +233,7 @@ class SettingsPageAssets { 'environment' => $this->environment->current_environment(), 'integration_date' => PAYPAL_INTEGRATION_DATE, 'is_pay_later_button_enabled' => $this->is_pay_later_button_enabled, + 'is_acdc_enabled' => $this->is_acdc_enabled, 'disabled_sources' => $this->disabled_sources, 'all_funding_sources' => $this->all_funding_sources, 'components' => array( 'buttons', 'funding-eligibility', 'messages' ), diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 4796c2d5b..66587d6af 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -185,7 +185,8 @@ class WCGatewayModule implements ModuleInterface { $settings_status->is_pay_later_button_enabled(), $settings->has( 'disable_funding' ) ? $settings->get( 'disable_funding' ) : array(), $c->get( 'wcgateway.settings.funding-sources' ), - $c->get( 'wcgateway.is-ppcp-settings-page' ) + $c->get( 'wcgateway.is-ppcp-settings-page' ), + $settings->has( 'dcc_enabled' ) && $settings->get( 'dcc_enabled' ) ); $assets->register_assets(); } diff --git a/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php b/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php index 95aa53e0c..f5066db40 100644 --- a/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php +++ b/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php @@ -28,7 +28,8 @@ class SettingsPagesAssetsTest extends TestCase true, array(), array(), - true + true, + false ); when('is_admin')