Refactor GooglePay onboarding

This commit is contained in:
Pedro Silva 2023-09-08 18:43:33 +01:00
parent 62e10387cb
commit bad21380b3
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
12 changed files with 289 additions and 24 deletions

View file

@ -2,7 +2,7 @@
/**
* Registers and configures the necessary Javascript for the button, credit messaging and DCC fields.
*
* @package WooCommerce\PayPalCommerce\Button\Assets
* @package WooCommerce\PayPalCommerce\Googlepay\Assets
*/
declare(strict_types=1);
@ -141,8 +141,8 @@ class Button implements ButtonInterface {
public function add_onboarding_options( $options ): string {
$checked = '';
try {
$onboard_with_apple = $this->settings->get( 'ppcp-onboarding-apple' );
if ( $onboard_with_apple === '1' ) {
$onboard_with_google = $this->settings->get( 'ppcp-onboarding-google' );
if ( $onboard_with_google === '1' ) {
$checked = 'checked';
}
} catch ( NotFoundException $exception ) {
@ -204,7 +204,6 @@ class Button implements ButtonInterface {
return $data;
}
/**
* Returns if Google Pay button is enabled
*

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Googlepay;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -34,13 +35,13 @@ class GooglepayModule implements ModuleInterface {
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
$button = $c->get( 'googlepay.button' );
assert( $button instanceof ButtonInterface );
if ( ! $c->get( 'googlepay.eligible' ) ) {
return;
}
$button = $c->get( 'googlepay.button' );
assert( $button instanceof ButtonInterface );
$button->initialize();
if ( ! $c->get( 'googlepay.available' ) ) {
@ -72,6 +73,17 @@ class GooglepayModule implements ModuleInterface {
}
}
);
// Clear product status handling.
add_action(
'woocommerce_paypal_payments_clear_apm_product_status',
function() use ( $c ): void {
$apm_status = $c->get( 'googlepay.helpers.apm-product-status' );
assert( $apm_status instanceof ApmProductStatus );
$apm_status->clear();
}
);
}
/**

View file

@ -0,0 +1,67 @@
<?php
/**
* Properties of the GooglePay module.
*
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
/**
* Class ApmApplies
*/
class ApmApplies {
/**
* The matrix which countries and currency combinations can be used for DCC.
*
* @var array
*/
private $allowed_country_currency_matrix;
/**
* 3-letter currency code of the shop.
*
* @var string
*/
private $currency;
/**
* 2-letter country code of the shop.
*
* @var string
*/
private $country;
/**
* DccApplies constructor.
*
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for DCC.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
*/
public function __construct(
array $allowed_country_currency_matrix,
string $currency,
string $country
) {
$this->allowed_country_currency_matrix = $allowed_country_currency_matrix;
$this->currency = $currency;
$this->country = $country;
}
/**
* Returns whether GooglePay can be used in the current country and the current currency used.
*
* @return bool
*/
public function for_country_currency(): bool {
if ( ! in_array( $this->country, array_keys( $this->allowed_country_currency_matrix ), true ) ) {
return false;
}
return in_array( $this->currency, $this->allowed_country_currency_matrix[ $this->country ], true );
}
}

View file

@ -0,0 +1,133 @@
<?php
/**
* Status of the GooglePay merchant connection.
*
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class ApmProductStatus
*/
class ApmProductStatus {
const CAPABILITY_NAME = 'GOOGLE_PAY';
const SETTINGS_KEY = 'products_googlepay_enabled';
const SETTINGS_VALUE_ENABLED = 'yes';
const SETTINGS_VALUE_DISABLED = 'no';
const SETTINGS_VALUE_UNDEFINED = '';
/**
* The current status stored in memory.
*
* @var bool|null
*/
private $current_status = null;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The partners endpoint.
*
* @var PartnersEndpoint
*/
private $partners_endpoint;
/**
* The onboarding status
*
* @var State
*/
private $onboarding_state;
/**
* ApmProductStatus constructor.
*
* @param Settings $settings The Settings.
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param State $onboarding_state The onboarding state.
*/
public function __construct(
Settings $settings,
PartnersEndpoint $partners_endpoint,
State $onboarding_state
) {
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->onboarding_state = $onboarding_state;
}
/**
* Whether the active/subscribed products support Googlepay.
*
* @return bool
*/
public function is_active() : bool {
if ( $this->onboarding_state->current_state() < State::STATE_ONBOARDED ) {
return false;
}
if ( null !== $this->current_status ) {
return $this->current_status;
}
if ( $this->settings->has( self::SETTINGS_KEY ) && ( $this->settings->get( self::SETTINGS_KEY ) ) ) {
$this->current_status = wc_string_to_bool( $this->settings->get( self::SETTINGS_KEY ) );
return $this->current_status;
}
try {
$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;
return $this->current_status;
}
foreach ( $seller_status->products() as $product ) {
if ( $product->name() !== 'PAYMENT_METHODS' ) {
continue;
}
if ( in_array( self::CAPABILITY_NAME, $product->capabilities(), true ) ) {
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
$this->settings->persist();
$this->current_status = true;
return $this->current_status;
}
}
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_DISABLED );
$this->settings->persist();
$this->current_status = false;
return $this->current_status;
}
/**
* Clears the persisted result to force a recheck.
*
* @return void
*/
public function clear(): void {
$this->current_status = null;
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_UNDEFINED );
$this->settings->persist();
}
}

View file

@ -2,12 +2,12 @@
/**
* Properties of the GooglePay module.
*
* @package WooCommerce\PayPalCommerce\Button\Assets
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Assets;
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
/**
* Class Button