From 08d8cded5b8360565c066811f31146047dc47730 Mon Sep 17 00:00:00 2001 From: Pedro Silva
There was an error getting your PayPal seller status. Some features may be disabled.
Certify that you connected to your account via our onboarding process.
', + 'woocommerce-paypal-payments' + ) + ); + + // Name the key so it can be overridden. + $notices['error_product_status'] = new Message( $message, 'error', true, 'ppcp-notice-wrapper' ); + return $notices; + } + ); + + } else { + + add_filter( + Repository::NOTICES_FILTER, + /** + * Adds GooglePay not available notice. + * + * @param array $notices The notices. + * @return array + * + * @psalm-suppress MissingClosureParamType + */ + static function ( $notices ) use ( $c ): array { + + $message = sprintf( + __( + 'Google Pay is not available on your PayPal account.', + 'woocommerce-paypal-payments' + ) + ); + + $notices[] = new Message( $message, 'warning', true, 'ppcp-notice-wrapper' ); + return $notices; + } + ); + + } + return; } diff --git a/modules/ppcp-googlepay/src/Helper/ApmProductStatus.php b/modules/ppcp-googlepay/src/Helper/ApmProductStatus.php index b6046e0e5..307fa8c14 100644 --- a/modules/ppcp-googlepay/src/Helper/ApmProductStatus.php +++ b/modules/ppcp-googlepay/src/Helper/ApmProductStatus.php @@ -32,6 +32,13 @@ class ApmProductStatus { */ private $current_status = null; + /** + * If there was a request failure. + * + * @var bool + */ + private $has_request_failure = false; + /** * The settings. * @@ -93,7 +100,8 @@ class ApmProductStatus { $seller_status = $this->partners_endpoint->seller_status(); } catch ( Throwable $error ) { // It may be a transitory error, don't persist the status. - $this->current_status = false; + $this->has_request_failure = true; + $this->current_status = false; return $this->current_status; } @@ -118,6 +126,15 @@ class ApmProductStatus { return $this->current_status; } + /** + * Returns if there was a request failure. + * + * @return bool + */ + public function has_request_failure(): bool { + return $this->has_request_failure; + } + /** * Clears the persisted result to force a recheck. * From b16e2571b123bd57e80694f2bd319965065307a2 Mon Sep 17 00:00:00 2001 From: Pedro SilvaThere was an error getting your PayPal seller status. Some features may be disabled.
Certify that you connected to your account via our onboarding process.
', - 'woocommerce-paypal-payments' - ) - ); - - // Name the key so it can be overridden. - $notices['error_product_status'] = new Message( $message, 'error', true, 'ppcp-notice-wrapper' ); - return $notices; - } - ); - - } else { - - add_filter( - Repository::NOTICES_FILTER, - /** - * Adds GooglePay not available notice. - * - * @param array $notices The notices. - * @return array - * - * @psalm-suppress MissingClosureParamType - */ - static function ( $notices ) use ( $c ): array { - - $message = sprintf( - __( - 'Google Pay is not available on your PayPal account.', - 'woocommerce-paypal-payments' - ) - ); - - $notices[] = new Message( $message, 'warning', true, 'ppcp-notice-wrapper' ); - return $notices; - } - ); - - } - + $availability_notice = $c->get( 'googlepay.availability_notice' ); + assert( $availability_notice instanceof AvailabilityNotice ); + $availability_notice->execute(); return; } + // Initializes button rendering. add_action( 'wp', static function () use ( $c, $button ) { @@ -121,6 +69,7 @@ class GooglepayModule implements ModuleInterface { } ); + // Enqueue frontend scripts. add_action( 'wp_enqueue_scripts', static function () use ( $c, $button ) { @@ -128,6 +77,7 @@ class GooglepayModule implements ModuleInterface { } ); + // Enqueue backend scripts. add_action( 'admin_enqueue_scripts', static function () use ( $c, $button ) { @@ -143,6 +93,7 @@ class GooglepayModule implements ModuleInterface { } ); + // Registers buttons on blocks pages. add_action( 'woocommerce_blocks_payment_method_type_registration', function( PaymentMethodRegistry $payment_method_registry ) use ( $c, $button ): void { @@ -152,6 +103,7 @@ class GooglepayModule implements ModuleInterface { } ); + // Adds GooglePay component to the backend button preview settings. add_action( 'woocommerce_paypal_payments_admin_gateway_settings', function( array $settings ) use ( $c, $button ): array { @@ -162,7 +114,7 @@ class GooglepayModule implements ModuleInterface { } ); - // Clear product status handling. + // Clears product status when appropriate. add_action( 'woocommerce_paypal_payments_clear_apm_product_status', function( Settings $settings = null ) use ( $c ): void { diff --git a/modules/ppcp-googlepay/src/Helper/AvailabilityNotice.php b/modules/ppcp-googlepay/src/Helper/AvailabilityNotice.php new file mode 100644 index 000000000..84f7c4abc --- /dev/null +++ b/modules/ppcp-googlepay/src/Helper/AvailabilityNotice.php @@ -0,0 +1,112 @@ +product_status = $product_status; + } + + /** + * Adds availability notice if applicable. + * + * @return void + */ + public function execute(): void { + if ( $this->product_status->has_request_failure() ) { + $this->add_seller_status_failure_notice(); + } else { + $this->add_not_available_notice(); + } + } + + /** + * Adds seller status failure notice. + * + * @return void + */ + private function add_seller_status_failure_notice(): void { + add_filter( + Repository::NOTICES_FILTER, + /** + * Adds seller status notice. + * + * @param array $notices The notices. + * @return array + * + * @psalm-suppress MissingClosureParamType + */ + static function ( $notices ): array { + + $message = sprintf( + __( + 'There was an error getting your PayPal seller status. Some features may be disabled.
Certify that you connected to your account via our onboarding process.
', + 'woocommerce-paypal-payments' + ) + ); + + // Name the key so it can be overridden in other modules. + $notices['error_product_status'] = new Message( $message, 'error', true, 'ppcp-notice-wrapper' ); + return $notices; + } + ); + } + + /** + * Adds not available notice. + * + * @return void + */ + private function add_not_available_notice(): void { + add_filter( + Repository::NOTICES_FILTER, + /** + * Adds GooglePay not available notice. + * + * @param array $notices The notices. + * @return array + * + * @psalm-suppress MissingClosureParamType + */ + static function ( $notices ): array { + + $message = sprintf( + __( + 'Google Pay is not available on your PayPal seller account.', + 'woocommerce-paypal-payments' + ) + ); + + $notices[] = new Message( $message, 'warning', true, 'ppcp-notice-wrapper' ); + return $notices; + } + ); + } + +} From 904beafa1f881534ecfd476529432f7bf2854402 Mon Sep 17 00:00:00 2001 From: Pedro SilvaThere was an error getting your PayPal seller status. Some features may be disabled.
Certify that you connected to your account via our onboarding process.
', + 'There was an error getting your PayPal seller status. Some features may be disabled.
Certify that you connected to your PayPal business account via our onboarding process.
', 'woocommerce-paypal-payments' ) ); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 8a825b0b1..3805230a0 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1017,7 +1017,8 @@ return array( $partner_endpoint, $container->get( 'dcc.status-cache' ), $container->get( 'api.helpers.dccapplies' ), - $container->get( 'onboarding.state' ) + $container->get( 'onboarding.state' ), + $container->get( 'api.helper.failure-registry' ) ); }, @@ -1097,7 +1098,8 @@ return array( $container->get( 'wcgateway.settings' ), $container->get( 'api.endpoint.partners' ), $container->get( 'pui.status-cache' ), - $container->get( 'onboarding.state' ) + $container->get( 'onboarding.state' ), + $container->get( 'api.helper.failure-registry' ) ); }, 'wcgateway.pay-upon-invoice' => static function ( ContainerInterface $container ): PayUponInvoice { diff --git a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php index f8d717f08..c501dadac 100644 --- a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php @@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; +use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; @@ -37,6 +38,14 @@ class DCCProductStatus { * @var bool|null */ private $current_status_cache; + + /** + * If there was a request failure. + * + * @var bool + */ + private $has_request_failure = false; + /** * The settings. * @@ -65,6 +74,13 @@ class DCCProductStatus { */ private $onboarding_state; + /** + * The API failure registry + * + * @var FailureRegistry + */ + private $api_failure_registry; + /** * DccProductStatus constructor. * @@ -73,19 +89,22 @@ class DCCProductStatus { * @param Cache $cache The cache. * @param DccApplies $dcc_applies The dcc applies helper. * @param State $onboarding_state The onboarding state. + * @param FailureRegistry $api_failure_registry The API failure registry. */ public function __construct( Settings $settings, PartnersEndpoint $partners_endpoint, Cache $cache, DccApplies $dcc_applies, - State $onboarding_state + State $onboarding_state, + FailureRegistry $api_failure_registry ) { - $this->settings = $settings; - $this->partners_endpoint = $partners_endpoint; - $this->cache = $cache; - $this->dcc_applies = $dcc_applies; - $this->onboarding_state = $onboarding_state; + $this->settings = $settings; + $this->partners_endpoint = $partners_endpoint; + $this->cache = $cache; + $this->dcc_applies = $dcc_applies; + $this->onboarding_state = $onboarding_state; + $this->api_failure_registry = $api_failure_registry; } /** @@ -111,9 +130,17 @@ class DCCProductStatus { return true; } + // Check API failure registry to prevent multiple failed API requests. + if ( $this->api_failure_registry->has_failure_in_timeframe( FailureRegistry::SELLER_STATUS_KEY, HOUR_IN_SECONDS ) ) { + $this->has_request_failure = true; + $this->current_status_cache = false; + return $this->current_status_cache; + } + try { $seller_status = $this->partners_endpoint->seller_status(); } catch ( Throwable $error ) { + $this->has_request_failure = true; $this->current_status_cache = false; return false; } @@ -149,4 +176,14 @@ class DCCProductStatus { $this->current_status_cache = false; return false; } + + /** + * Returns if there was a request failure. + * + * @return bool + */ + public function has_request_failure(): bool { + return $this->has_request_failure; + } + } diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php index df70c0779..7295b7e89 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php @@ -13,6 +13,7 @@ use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; +use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; @@ -36,6 +37,14 @@ class PayUponInvoiceProductStatus { * @var bool|null */ private $current_status_cache; + + /** + * If there was a request failure. + * + * @var bool + */ + private $has_request_failure = false; + /** * The settings. * @@ -57,6 +66,13 @@ class PayUponInvoiceProductStatus { */ private $onboarding_state; + /** + * The API failure registry + * + * @var FailureRegistry + */ + private $api_failure_registry; + /** * PayUponInvoiceProductStatus constructor. * @@ -64,17 +80,20 @@ class PayUponInvoiceProductStatus { * @param PartnersEndpoint $partners_endpoint The Partner Endpoint. * @param Cache $cache The cache. * @param State $onboarding_state The onboarding state. + * @param FailureRegistry $api_failure_registry The API failure registry. */ public function __construct( Settings $settings, PartnersEndpoint $partners_endpoint, Cache $cache, - State $onboarding_state + State $onboarding_state, + FailureRegistry $api_failure_registry ) { - $this->settings = $settings; - $this->partners_endpoint = $partners_endpoint; - $this->cache = $cache; - $this->onboarding_state = $onboarding_state; + $this->settings = $settings; + $this->partners_endpoint = $partners_endpoint; + $this->cache = $cache; + $this->onboarding_state = $onboarding_state; + $this->api_failure_registry = $api_failure_registry; } /** @@ -99,9 +118,17 @@ class PayUponInvoiceProductStatus { return true; } + // Check API failure registry to prevent multiple failed API requests. + if ( $this->api_failure_registry->has_failure_in_timeframe( FailureRegistry::SELLER_STATUS_KEY, HOUR_IN_SECONDS ) ) { + $this->has_request_failure = true; + $this->current_status_cache = false; + return $this->current_status_cache; + } + try { $seller_status = $this->partners_endpoint->seller_status(); } catch ( Throwable $error ) { + $this->has_request_failure = true; $this->current_status_cache = false; return false; } @@ -136,4 +163,14 @@ class PayUponInvoiceProductStatus { $this->current_status_cache = false; return false; } + + /** + * Returns if there was a request failure. + * + * @return bool + */ + public function has_request_failure(): bool { + return $this->has_request_failure; + } + } From 26a2b80b1b1ce7399d2c513e4ea3973bddcd6d32 Mon Sep 17 00:00:00 2001 From: Pedro SilvaDAY_IN_SECONDS
+ DAY_IN_SECONDS
+ realpath( __FILE__ )
From 43136ecf3c9f93afc99f3ee3fa1a89587b1fe5ca Mon Sep 17 00:00:00 2001
From: Pedro Silva