Refactor GooglePay availability notices.

This commit is contained in:
Pedro Silva 2023-09-22 08:53:35 +01:00
parent dc77deea79
commit b16e2571b1
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
3 changed files with 132 additions and 61 deletions

View file

@ -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(
__(
'<p>There was an error getting your PayPal seller status. Some features may be disabled.</p><p>Certify that you connected to your account via our onboarding process.</p>',
'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 {

View file

@ -0,0 +1,112 @@
<?php
/**
* Adds availability notice if applicable.
*
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
/**
* Class AvailabilityNotice
*/
class AvailabilityNotice {
/**
* The product status handler.
*
* @var ApmProductStatus
*/
private $product_status;
/**
* Class ApmProductStatus constructor.
* @param ApmProductStatus $product_status The product status handler.
*/
public function __construct( ApmProductStatus $product_status ) {
$this->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(
__(
'<p>There was an error getting your PayPal seller status. Some features may be disabled.</p><p>Certify that you connected to your account via our onboarding process.</p>',
'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;
}
);
}
}