mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Refactor GooglePay onboarding
This commit is contained in:
parent
62e10387cb
commit
bad21380b3
12 changed files with 289 additions and 24 deletions
|
@ -81,10 +81,10 @@ class CartScriptParamsEndpoint implements EndpointInterface {
|
|||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'url_params' => $script_data['url_params'],
|
||||
'button' => $script_data['button'],
|
||||
'messages' => $script_data['messages'],
|
||||
'amount' => WC()->cart->get_total( 'raw' ),
|
||||
'url_params' => $script_data['url_params'],
|
||||
'button' => $script_data['button'],
|
||||
'messages' => $script_data['messages'],
|
||||
'amount' => WC()->cart->get_total( 'raw' ),
|
||||
|
||||
'total' => $total,
|
||||
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
||||
|
|
|
@ -9,7 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Googlepay;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Googlepay\Assets\PropertiesDictionary;
|
||||
use WooCommerce\PayPalCommerce\Googlepay\Helper\PropertiesDictionary;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
||||
|
|
|
@ -13,19 +13,69 @@ use Automattic\WooCommerce\Blocks\Payments\PaymentMethodTypeInterface;
|
|||
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
|
||||
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\Vendor\Psr\Container\ContainerInterface;
|
||||
|
||||
return array(
|
||||
'googlepay.eligible' => static function ( ContainerInterface $container ): bool {
|
||||
// TODO : add handlers.
|
||||
return true;
|
||||
},
|
||||
'googlepay.available' => static function ( ContainerInterface $container ): bool {
|
||||
// TODO : add handlers.
|
||||
return true;
|
||||
|
||||
// If GooglePay can be configured.
|
||||
'googlepay.eligible' => static function ( ContainerInterface $container ): bool {
|
||||
$apm_applies = $container->get( 'googlepay.helpers.apm-applies' );
|
||||
assert( $apm_applies instanceof ApmApplies );
|
||||
|
||||
return $apm_applies->for_country_currency();
|
||||
},
|
||||
|
||||
'googlepay.button' => static function ( ContainerInterface $container ): ButtonInterface {
|
||||
'googlepay.helpers.apm-applies' => static function ( ContainerInterface $container ) : ApmApplies {
|
||||
return new ApmApplies(
|
||||
$container->get( 'googlepay.supported-country-currency-matrix' ),
|
||||
$container->get( 'api.shop.currency' ),
|
||||
$container->get( 'api.shop.country' )
|
||||
);
|
||||
},
|
||||
|
||||
// If GooglePay is configured.
|
||||
'googlepay.available' => static function ( ContainerInterface $container ): bool {
|
||||
/**
|
||||
* $status = $container->get( 'googlepay.helpers.apm-product-status' );
|
||||
* assert( $status instanceof ApmProductStatus );
|
||||
* return $status->is_active();
|
||||
*/
|
||||
return true; // TODO: must test further.
|
||||
},
|
||||
|
||||
'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' )
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* The matrix which countries and currency combinations can be used for GooglePay.
|
||||
*/
|
||||
'googlepay.supported-country-currency-matrix' => static function ( ContainerInterface $container ) : array {
|
||||
/**
|
||||
* Returns which countries and currency combinations can be used for GooglePay.
|
||||
*/
|
||||
return apply_filters(
|
||||
'woocommerce_paypal_payments_supported_country_currency_matrix',
|
||||
array(
|
||||
'US' => array(
|
||||
'AUD',
|
||||
'CAD',
|
||||
'EUR',
|
||||
'GBP',
|
||||
'JPY',
|
||||
'USD',
|
||||
),
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
'googlepay.button' => static function ( ContainerInterface $container ): ButtonInterface {
|
||||
return new Button(
|
||||
$container->get( 'googlepay.url' ),
|
||||
$container->get( 'googlepay.sdk_url' ),
|
||||
|
@ -39,7 +89,7 @@ return array(
|
|||
);
|
||||
},
|
||||
|
||||
'googlepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {
|
||||
'googlepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {
|
||||
return new BlocksPaymentMethod(
|
||||
'ppcp-googlepay',
|
||||
$container->get( 'googlepay.url' ),
|
||||
|
@ -49,7 +99,7 @@ return array(
|
|||
);
|
||||
},
|
||||
|
||||
'googlepay.url' => static function ( ContainerInterface $container ): string {
|
||||
'googlepay.url' => static function ( ContainerInterface $container ): string {
|
||||
$path = realpath( __FILE__ );
|
||||
if ( false === $path ) {
|
||||
return '';
|
||||
|
@ -60,7 +110,7 @@ return array(
|
|||
);
|
||||
},
|
||||
|
||||
'googlepay.sdk_url' => static function ( ContainerInterface $container ): string {
|
||||
'googlepay.sdk_url' => static function ( ContainerInterface $container ): string {
|
||||
return 'https://pay.google.com/gp/p/js/pay.js';
|
||||
},
|
||||
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
67
modules/ppcp-googlepay/src/Helper/ApmApplies.php
Normal file
67
modules/ppcp-googlepay/src/Helper/ApmApplies.php
Normal 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 );
|
||||
}
|
||||
|
||||
}
|
133
modules/ppcp-googlepay/src/Helper/ApmProductStatus.php
Normal file
133
modules/ppcp-googlepay/src/Helper/ApmProductStatus.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
|
@ -134,6 +134,7 @@ class LoginSellerEndpoint implements EndpointInterface {
|
|||
$this->settings->set( 'products_dcc_enabled', null );
|
||||
$this->settings->set( 'products_pui_enabled', null );
|
||||
$this->settings->persist();
|
||||
do_action( 'woocommerce_paypal_payments_clear_apm_product_status' );
|
||||
|
||||
$endpoint = $is_sandbox ? $this->login_seller_sandbox : $this->login_seller_production;
|
||||
$credentials = $endpoint->credentials_for(
|
||||
|
|
|
@ -238,6 +238,7 @@ class OnboardingRESTController {
|
|||
|
||||
$settings->set( 'products_dcc_enabled', null );
|
||||
$settings->set( 'products_pui_enabled', null );
|
||||
do_action( 'woocommerce_paypal_payments_clear_apm_product_status' );
|
||||
|
||||
if ( ! $settings->persist() ) {
|
||||
return new \WP_Error(
|
||||
|
|
|
@ -391,6 +391,7 @@ class SettingsListener {
|
|||
if ( self::CREDENTIALS_UNCHANGED !== $credentials_change_status ) {
|
||||
$this->settings->set( 'products_dcc_enabled', null );
|
||||
$this->settings->set( 'products_pui_enabled', null );
|
||||
do_action( 'woocommerce_paypal_payments_clear_apm_product_status' );
|
||||
}
|
||||
|
||||
if ( in_array(
|
||||
|
|
|
@ -286,6 +286,7 @@ class WCGatewayModule implements ModuleInterface {
|
|||
$settings->set( 'products_dcc_enabled', false );
|
||||
$settings->set( 'products_pui_enabled', false );
|
||||
$settings->persist();
|
||||
do_action( 'woocommerce_paypal_payments_clear_apm_product_status' );
|
||||
|
||||
// Update caches.
|
||||
$dcc_status = $c->get( 'wcgateway.helper.dcc-product-status' );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue