From d2abeb5dbfdf8371eb1fedd7e0756a97fa3f4cd2 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 12 Dec 2023 17:30:55 +0000 Subject: [PATCH] Add mechanism to refresh feature status. --- .../ppcp-applepay/src/Helper/ApmApplies.php | 5 +- .../ppcp-googlepay/src/Helper/ApmApplies.php | 7 +- .../resources/js/gateway-settings.js | 37 +++++++ modules/ppcp-wc-gateway/services.php | 8 ++ .../src/Assets/SettingsPageAssets.php | 9 ++ .../Endpoint/RefreshFeatureStatusEndpoint.php | 96 +++++++++++++++++++ .../Settings/Fields/connection-tab-fields.php | 10 ++ .../ppcp-wc-gateway/src/WCGatewayModule.php | 11 +++ 8 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 modules/ppcp-wc-gateway/src/Endpoint/RefreshFeatureStatusEndpoint.php diff --git a/modules/ppcp-applepay/src/Helper/ApmApplies.php b/modules/ppcp-applepay/src/Helper/ApmApplies.php index dec9b1a08..0fa12420b 100644 --- a/modules/ppcp-applepay/src/Helper/ApmApplies.php +++ b/modules/ppcp-applepay/src/Helper/ApmApplies.php @@ -1,6 +1,7 @@ { + const props = PayPalCommerceGatewaySettings.ajax.refresh_feature_status; + const $btn = jQuery(props.button); + + $btn.click(async () => { + $btn.prop('disabled', true); + + const response = await fetch( + props.endpoint, + { + method: 'POST', + credentials: 'same-origin', + headers: { + 'content-type': 'application/json' + }, + body: JSON.stringify( + { + nonce: props.nonce, + } + ) + } + ); + + const responseData = await response.json(); + + if (!responseData.success) { + alert(responseData.data.message); + $btn.prop('disabled', false); + } else { + window.location.reload(); + } + }); + + })(); + } ); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 62a84a733..0edfeb699 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer; use WooCommerce\PayPalCommerce\Onboarding\State; +use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer; @@ -1003,6 +1004,13 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice { $container->get( 'woocommerce.logger.woocommerce' ) ); }, + 'wcgateway.endpoint.refresh-feature-status' => static function ( ContainerInterface $container ) : RefreshFeatureStatusEndpoint { + return new RefreshFeatureStatusEndpoint( + $container->get( 'wcgateway.settings' ), + new Cache( 'ppcp-timeout' ), + $container->get( 'woocommerce.logger.woocommerce' ) + ); + }, 'wcgateway.transaction-url-sandbox' => static function ( ContainerInterface $container ): string { return 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s'; diff --git a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php index 7c5cc7506..5fae9c0c9 100644 --- a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php @@ -10,9 +10,11 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Assets; use WooCommerce\PayPalCommerce\Onboarding\Environment; +use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint; /** * Class SettingsPageAssets @@ -237,6 +239,13 @@ private function register_paypal_admin_assets(): void { 'disabled_sources' => $this->disabled_sources, 'all_funding_sources' => $this->all_funding_sources, 'components' => array( 'buttons', 'funding-eligibility', 'messages' ), + 'ajax' => array( + 'refresh_feature_status' => array( + 'endpoint' => \WC_AJAX::get_endpoint( RefreshFeatureStatusEndpoint::ENDPOINT ), + 'nonce' => wp_create_nonce( RefreshFeatureStatusEndpoint::nonce() ), + 'button' => '.ppcp-refresh-feature-status', + ), + ), ) ) ); diff --git a/modules/ppcp-wc-gateway/src/Endpoint/RefreshFeatureStatusEndpoint.php b/modules/ppcp-wc-gateway/src/Endpoint/RefreshFeatureStatusEndpoint.php new file mode 100644 index 000000000..8aa49946c --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Endpoint/RefreshFeatureStatusEndpoint.php @@ -0,0 +1,96 @@ +settings = $settings; + $this->cache = $cache; + $this->logger = $logger; + } + + /** + * Returns the nonce. + * + * @return string + */ + public static function nonce(): string { + return self::ENDPOINT; + } + + /** + * Handles the incoming request. + */ + public function handle_request(): void { + $now = time(); + $last_request_time = $this->cache->get( self::CACHE_KEY ) ?: 0; + $seconds_missing = $last_request_time + self::TIMEOUT - $now; + + if ( $seconds_missing > 0 ) { + $response = array( + 'message' => sprintf( + // translators: %1$s is the number of seconds remaining. + __( 'Wait %1$s seconds before trying again.', 'woocommerce-paypal-payments' ), + $seconds_missing + ), + ); + wp_send_json_error( $response ); + } + + $this->cache->set( self::CACHE_KEY, $now, self::TIMEOUT ); + do_action( 'woocommerce_paypal_payments_clear_apm_product_status', $this->settings ); + wp_send_json_success(); + } +} 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 4a7e3ac6a..d9513f9c6 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 @@ -390,6 +390,16 @@ '' ), ), + 'refresh_feature_status' => array( + 'title' => __( 'Refresh feature availability status', 'woocommerce-paypal-payments' ), + 'type' => 'ppcp-text', + 'text' => '', + 'screens' => array( + State::STATE_ONBOARDED, + ), + 'requirements' => array(), + 'gateway' => Settings::CONNECTION_TAB_ID, + ), 'ppcp_dcc_status' => array( 'title' => __( 'Advanced Credit and Debit Card Payments', 'woocommerce-paypal-payments' ), 'type' => 'ppcp-text', diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index a30be4d5a..6af49c778 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -13,6 +13,7 @@ use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; +use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; @@ -255,6 +256,16 @@ static function () use ( $c ) { } ); + add_action( + 'wc_ajax_' . RefreshFeatureStatusEndpoint::ENDPOINT, + static function () use ( $c ) { + $endpoint = $c->get( 'wcgateway.endpoint.refresh-feature-status' ); + assert( $endpoint instanceof RefreshFeatureStatusEndpoint ); + + $endpoint->handle_request(); + } + ); + add_action( 'woocommerce_paypal_payments_gateway_migrate', static function () use ( $c ) {