diff --git a/modules/ppcp-googlepay/extensions.php b/modules/ppcp-googlepay/extensions.php index 05a57284a..07e29d97b 100644 --- a/modules/ppcp-googlepay/extensions.php +++ b/modules/ppcp-googlepay/extensions.php @@ -24,6 +24,8 @@ return array( return $fields; } + $is_available = $container->get( 'googlepay.available' ); + $insert_after = function( array $array, string $key, array $new ): array { $keys = array_keys( $array ); $index = array_search( $key, $keys, true ); @@ -66,6 +68,15 @@ return array( ->action_visible( 'googlepay_button_language' ) ->action_visible( 'googlepay_button_shipping_enabled' ) ->to_array(), + $display_manager + ->rule() + ->condition_is_true( $is_available ) + ->action_enable( 'googlepay_button_enabled' ) + ->action_enable( 'googlepay_button_type' ) + ->action_enable( 'googlepay_button_color' ) + ->action_enable( 'googlepay_button_language' ) + ->action_enable( 'googlepay_button_shipping_enabled' ) + ->to_array(), ) ), ), diff --git a/modules/ppcp-googlepay/services.php b/modules/ppcp-googlepay/services.php index a7944a952..aa9d3f677 100644 --- a/modules/ppcp-googlepay/services.php +++ b/modules/ppcp-googlepay/services.php @@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Googlepay; use Automattic\WooCommerce\Blocks\Payments\PaymentMethodTypeInterface; use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface; +use WooCommerce\PayPalCommerce\Common\Pattern\SingletonDecorator; use WooCommerce\PayPalCommerce\Googlepay\Assets\BlocksPaymentMethod; use WooCommerce\PayPalCommerce\Googlepay\Assets\Button; use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmApplies; @@ -37,7 +38,7 @@ return array( // If GooglePay is configured. 'googlepay.available' => static function ( ContainerInterface $container ): bool { - if ( apply_filters( 'woocommerce_paypal_payments_googlepay_validate_product_status', false ) ) { + if ( apply_filters( 'woocommerce_paypal_payments_googlepay_validate_product_status', true ) ) { $status = $container->get( 'googlepay.helpers.apm-product-status' ); assert( $status instanceof ApmProductStatus ); /** @@ -48,13 +49,15 @@ return array( return true; }, - 'googlepay.helpers.apm-product-status' => static function( ContainerInterface $container ): ApmProductStatus { - return new ApmProductStatus( - $container->get( 'wcgateway.settings' ), - $container->get( 'api.endpoint.partners' ), - $container->get( 'onboarding.state' ) - ); - }, + 'googlepay.helpers.apm-product-status' => SingletonDecorator::make( + static function( ContainerInterface $container ): ApmProductStatus { + return new ApmProductStatus( + $container->get( 'wcgateway.settings' ), + $container->get( 'api.endpoint.partners' ), + $container->get( 'onboarding.state' ) + ); + } + ), /** * The matrix which countries and currency combinations can be used for GooglePay. diff --git a/modules/ppcp-googlepay/src/GooglepayModule.php b/modules/ppcp-googlepay/src/GooglepayModule.php index b93d58552..5855710ca 100644 --- a/modules/ppcp-googlepay/src/GooglepayModule.php +++ b/modules/ppcp-googlepay/src/GooglepayModule.php @@ -10,6 +10,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Googlepay; use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; +use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; +use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository; use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface; use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; @@ -46,6 +48,66 @@ class GooglepayModule implements ModuleInterface { $button->initialize(); if ( ! $c->get( 'googlepay.available' ) ) { + + $apm_status = $c->get( 'googlepay.helpers.apm-product-status' ); + assert( $apm_status instanceof ApmProductStatus ); + + // TODO: refactor the notices. + if ( $apm_status->has_request_failure() ) { + + add_filter( + Repository::NOTICES_FILTER, + /** + * Adds seller status notice. + * + * @param array $notices The notices. + * @return array + * + * @psalm-suppress MissingClosureParamType + */ + static function ( $notices ) use ( $c ): 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. + $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. *