From 8e71665ec4013f6422d386f7d0775cc601ddd443 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Thu, 4 Jan 2024 15:23:01 +0000 Subject: [PATCH 1/2] Add status message to feature availability reload. --- .../ppcp-wc-gateway/resources/css/common.scss | 14 ++++++++++ .../resources/js/gateway-settings.js | 28 +++++++++++++++---- .../src/Assets/SettingsPageAssets.php | 4 +++ .../Settings/Fields/connection-tab-fields.php | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/modules/ppcp-wc-gateway/resources/css/common.scss b/modules/ppcp-wc-gateway/resources/css/common.scss index aa9ec7f43..6ab2f4a56 100644 --- a/modules/ppcp-wc-gateway/resources/css/common.scss +++ b/modules/ppcp-wc-gateway/resources/css/common.scss @@ -25,3 +25,17 @@ .ppcp-button-apm { @include apm-button.button; } + +.ppcp-status-text { + padding-top: 4px; + + .error { + color: red; + font-weight: bold; + } + + .success { + color: green; + font-weight: bold; + } +} diff --git a/modules/ppcp-wc-gateway/resources/js/gateway-settings.js b/modules/ppcp-wc-gateway/resources/js/gateway-settings.js index 7d8132cfa..ed50902e6 100644 --- a/modules/ppcp-wc-gateway/resources/js/gateway-settings.js +++ b/modules/ppcp-wc-gateway/resources/js/gateway-settings.js @@ -355,11 +355,25 @@ document.addEventListener( } // Logic to handle the "Check available features" button. - ((props) => { - const $btn = jQuery(props.button); + (($, props, anchor) => { + const $btn = $(props.button); + + const printStatus = (message, showSpinner) => { + const html = message + (showSpinner ? '' : ''); + $btn.siblings('.ppcp-status-text').html(html); + }; + + // If the reload comes from a successful refresh. + if (typeof URLSearchParams === 'function' && (new URLSearchParams(window.location.search)).get('feature-refreshed')) { + printStatus('✔️ ' + props.messages.success + ''); + $('html, body').animate({ + scrollTop: $(anchor).offset().top + }, 500); + } $btn.click(async () => { $btn.prop('disabled', true); + printStatus(props.messages.waiting, true); const response = await fetch( props.endpoint, @@ -380,14 +394,18 @@ document.addEventListener( const responseData = await response.json(); if (!responseData.success) { - alert(responseData.data.message); + printStatus(responseData.data.message); $btn.prop('disabled', false); } else { - window.location.reload(); + window.location.href += (window.location.href.indexOf('?') > -1 ? '&' : '?') + "feature-refreshed=1#"; } }); - })(PayPalCommerceGatewaySettings.ajax.refresh_feature_status); + })( + jQuery, + PayPalCommerceGatewaySettings.ajax.refresh_feature_status, + '#field-credentials_feature_onboarding_heading' + ); } ); diff --git a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php index 5fae9c0c9..e8aa59a7d 100644 --- a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php @@ -244,6 +244,10 @@ class SettingsPageAssets { 'endpoint' => \WC_AJAX::get_endpoint( RefreshFeatureStatusEndpoint::ENDPOINT ), 'nonce' => wp_create_nonce( RefreshFeatureStatusEndpoint::nonce() ), 'button' => '.ppcp-refresh-feature-status', + 'messages' => array( + 'waiting' => __( 'Checking features...', 'woocommerce-paypal-payments' ), + 'success' => __( 'Feature status refreshed.', 'woocommerce-paypal-payments' ), + ), ), ), ) diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php index d9513f9c6..b97f59a8f 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php @@ -393,7 +393,7 @@ return function ( ContainerInterface $container, array $fields ): array { 'refresh_feature_status' => array( 'title' => __( 'Refresh feature availability status', 'woocommerce-paypal-payments' ), 'type' => 'ppcp-text', - 'text' => '', + 'text' => '
', 'screens' => array( State::STATE_ONBOARDED, ), From 5e6d7f97d25e0d4360d12df773b7b859bb37650a Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Thu, 4 Jan 2024 17:30:50 +0000 Subject: [PATCH 2/2] Clear PUI and DCC product status on woocommerce_paypal_payments_clear_apm_product_status event --- .../src/Helper/DCCProductStatus.php | 26 +++++++++++++++++++ .../Helper/PayUponInvoiceProductStatus.php | 26 +++++++++++++++++++ .../ppcp-wc-gateway/src/WCGatewayModule.php | 16 ++++++++++++ 3 files changed, 68 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php index 772a76e3a..3ca5b9998 100644 --- a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php @@ -186,4 +186,30 @@ class DCCProductStatus { return $this->has_request_failure; } + /** + * Clears the persisted result to force a recheck. + * + * @param Settings|null $settings The settings object. + * We accept a Settings object to don't override other sequential settings that are being updated elsewhere. + * @return void + */ + public function clear( Settings $settings = null ): void { + if ( null === $settings ) { + $settings = $this->settings; + } + + // Unset check stored in memory. + $this->current_status_cache = null; + + // Unset settings flag. + $settings_key = 'products_dcc_enabled'; + if ( $settings->has( $settings_key ) ) { + $settings->set( $settings_key, false ); + $settings->persist(); + } + + // Delete cached value. + $this->cache->delete( self::DCC_STATUS_CACHE_KEY ); + } + } diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php index f1a3221eb..dfbcb0380 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php @@ -173,4 +173,30 @@ class PayUponInvoiceProductStatus { return $this->has_request_failure; } + /** + * Clears the persisted result to force a recheck. + * + * @param Settings|null $settings The settings object. + * We accept a Settings object to don't override other sequential settings that are being updated elsewhere. + * @return void + */ + public function clear( Settings $settings = null ): void { + if ( null === $settings ) { + $settings = $this->settings; + } + + // Unset check stored in memory. + $this->current_status_cache = null; + + // Unset settings flag. + $settings_key = 'products_pui_enabled'; + if ( $settings->has( $settings_key ) ) { + $settings->set( $settings_key, false ); + $settings->persist(); + } + + // Delete cached value. + $this->cache->delete( self::PUI_STATUS_CACHE_KEY ); + } + } diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 6af49c778..79a6dc127 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -430,6 +430,22 @@ class WCGatewayModule implements ModuleInterface { $c->get( 'wcgateway.cli.settings.command' ) ); } + + // Clears product status when appropriate. + add_action( + 'woocommerce_paypal_payments_clear_apm_product_status', + function( Settings $settings = null ) use ( $c ): void { + $dcc_product_status = $c->get( 'wcgateway.helper.dcc-product-status' ); + if ( $dcc_product_status instanceof DCCProductStatus ) { + $dcc_product_status->clear( $settings ); + } + + $pui_product_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' ); + if ( $pui_product_status instanceof PayUponInvoiceProductStatus ) { + $pui_product_status->clear( $settings ); + } + } + ); } /**