diff --git a/modules/ppcp-googlepay/services.php b/modules/ppcp-googlepay/services.php index aa9d3f677..fafc41de4 100644 --- a/modules/ppcp-googlepay/services.php +++ b/modules/ppcp-googlepay/services.php @@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Googlepay\Assets\BlocksPaymentMethod; use WooCommerce\PayPalCommerce\Googlepay\Assets\Button; use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmApplies; use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus; +use WooCommerce\PayPalCommerce\Googlepay\Helper\AvailabilityNotice; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; return array( @@ -49,6 +50,12 @@ return array( return true; }, + 'googlepay.availability_notice' => static function ( ContainerInterface $container ): AvailabilityNotice { + return new AvailabilityNotice( + $container->get( 'googlepay.helpers.apm-product-status' ) + ); + }, + 'googlepay.helpers.apm-product-status' => SingletonDecorator::make( static function( ContainerInterface $container ): ApmProductStatus { return new ApmProductStatus( diff --git a/modules/ppcp-googlepay/src/GooglepayModule.php b/modules/ppcp-googlepay/src/GooglepayModule.php index 5855710ca..eab601704 100644 --- a/modules/ppcp-googlepay/src/GooglepayModule.php +++ b/modules/ppcp-googlepay/src/GooglepayModule.php @@ -14,6 +14,7 @@ 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\Googlepay\Helper\AvailabilityNotice; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; @@ -39,78 +40,25 @@ class GooglepayModule implements ModuleInterface { */ public function run( ContainerInterface $c ): void { + // Check if the module is applicable, correct country, currency, ... etc. if ( ! $c->get( 'googlepay.eligible' ) ) { return; } + // Load the button handler. $button = $c->get( 'googlepay.button' ); assert( $button instanceof ButtonInterface ); $button->initialize(); + // Check if this merchant can activate / use the buttons. 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; - } - ); - - } - + $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; + } + ); + } + +}