mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
♻️ Use new base class for GooglePay status check
This commit is contained in:
parent
3bd118de1b
commit
00edc11392
2 changed files with 34 additions and 129 deletions
|
@ -75,7 +75,7 @@ return array(
|
||||||
return new ApmProductStatus(
|
return new ApmProductStatus(
|
||||||
$container->get( 'wcgateway.settings' ),
|
$container->get( 'wcgateway.settings' ),
|
||||||
$container->get( 'api.endpoint.partners' ),
|
$container->get( 'api.endpoint.partners' ),
|
||||||
$container->get( 'onboarding.state' ),
|
$container->get( 'settings.flag.is-connected' ),
|
||||||
$container->get( 'api.helper.failure-registry' )
|
$container->get( 'api.helper.failure-registry' )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,130 +9,67 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
|
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
|
||||||
|
|
||||||
use Throwable;
|
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusCapability;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusCapability;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\ProductStatus;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ApmProductStatus
|
* Class ApmProductStatus
|
||||||
*/
|
*/
|
||||||
class ApmProductStatus {
|
class ApmProductStatus extends ProductStatus {
|
||||||
const CAPABILITY_NAME = 'GOOGLE_PAY';
|
public const CAPABILITY_NAME = 'GOOGLE_PAY';
|
||||||
const SETTINGS_KEY = 'products_googlepay_enabled';
|
public const SETTINGS_KEY = 'products_googlepay_enabled';
|
||||||
|
|
||||||
const SETTINGS_VALUE_ENABLED = 'yes';
|
public const SETTINGS_VALUE_ENABLED = 'yes';
|
||||||
const SETTINGS_VALUE_DISABLED = 'no';
|
public const SETTINGS_VALUE_DISABLED = 'no';
|
||||||
const SETTINGS_VALUE_UNDEFINED = '';
|
public const SETTINGS_VALUE_UNDEFINED = '';
|
||||||
|
|
||||||
/**
|
|
||||||
* The current status stored in memory.
|
|
||||||
*
|
|
||||||
* @var bool|null
|
|
||||||
*/
|
|
||||||
private $current_status = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If there was a request failure.
|
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private $has_request_failure = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The settings.
|
* The settings.
|
||||||
*
|
*
|
||||||
* @var Settings
|
* @var Settings
|
||||||
*/
|
*/
|
||||||
private $settings;
|
private Settings $settings;
|
||||||
|
|
||||||
/**
|
|
||||||
* The partners endpoint.
|
|
||||||
*
|
|
||||||
* @var PartnersEndpoint
|
|
||||||
*/
|
|
||||||
private $partners_endpoint;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The onboarding status
|
|
||||||
*
|
|
||||||
* @var State
|
|
||||||
*/
|
|
||||||
private $onboarding_state;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The API failure registry
|
|
||||||
*
|
|
||||||
* @var FailureRegistry
|
|
||||||
*/
|
|
||||||
private $api_failure_registry;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ApmProductStatus constructor.
|
* ApmProductStatus constructor.
|
||||||
*
|
*
|
||||||
* @param Settings $settings The Settings.
|
* @param Settings $settings The Settings.
|
||||||
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
|
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
|
||||||
* @param State $onboarding_state The onboarding state.
|
* @param bool $is_connected The onboarding state.
|
||||||
* @param FailureRegistry $api_failure_registry The API failure registry.
|
* @param FailureRegistry $api_failure_registry The API failure registry.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Settings $settings,
|
Settings $settings,
|
||||||
PartnersEndpoint $partners_endpoint,
|
PartnersEndpoint $partners_endpoint,
|
||||||
State $onboarding_state,
|
bool $is_connected,
|
||||||
FailureRegistry $api_failure_registry
|
FailureRegistry $api_failure_registry
|
||||||
) {
|
) {
|
||||||
$this->settings = $settings;
|
parent::__construct( $is_connected, $partners_endpoint, $api_failure_registry );
|
||||||
$this->partners_endpoint = $partners_endpoint;
|
|
||||||
$this->onboarding_state = $onboarding_state;
|
$this->settings = $settings;
|
||||||
$this->api_failure_registry = $api_failure_registry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** {@inheritDoc} */
|
||||||
* Whether the active/subscribed products support Googlepay.
|
protected function check_local_state() : ?bool {
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function is_active() : bool {
|
|
||||||
|
|
||||||
// If not onboarded then makes no sense to check status.
|
|
||||||
if ( ! $this->is_onboarded() ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$status_override = apply_filters( 'woocommerce_paypal_payments_google_pay_product_status', null );
|
$status_override = apply_filters( 'woocommerce_paypal_payments_google_pay_product_status', null );
|
||||||
if ( null !== $status_override ) {
|
if ( null !== $status_override ) {
|
||||||
return $status_override;
|
return $status_override;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If status was already checked on this request return the same result.
|
|
||||||
if ( null !== $this->current_status ) {
|
|
||||||
return $this->current_status;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if status was checked on previous requests.
|
// Check if status was checked on previous requests.
|
||||||
if ( $this->settings->has( self::SETTINGS_KEY ) && ( $this->settings->get( self::SETTINGS_KEY ) ) ) {
|
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 wc_string_to_bool( $this->settings->get( self::SETTINGS_KEY ) );
|
||||||
return $this->current_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check API failure registry to prevent multiple failed API requests.
|
return null;
|
||||||
if ( $this->api_failure_registry->has_failure_in_timeframe( FailureRegistry::SELLER_STATUS_KEY, HOUR_IN_SECONDS ) ) {
|
}
|
||||||
$this->has_request_failure = true;
|
|
||||||
$this->current_status = false;
|
|
||||||
return $this->current_status;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request seller status via PayPal API.
|
|
||||||
try {
|
|
||||||
$seller_status = $this->partners_endpoint->seller_status();
|
|
||||||
} catch ( Throwable $error ) {
|
|
||||||
$this->has_request_failure = true;
|
|
||||||
$this->current_status = false;
|
|
||||||
return $this->current_status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
protected function check_active_state( SellerStatus $seller_status ) : bool {
|
||||||
// Check the seller status for the intended capability.
|
// Check the seller status for the intended capability.
|
||||||
$has_capability = false;
|
$has_capability = false;
|
||||||
foreach ( $seller_status->products() as $product ) {
|
foreach ( $seller_status->products() as $product ) {
|
||||||
|
@ -145,65 +82,33 @@ class ApmProductStatus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $seller_status->capabilities() as $capability ) {
|
if ( ! $has_capability ) {
|
||||||
if ( $capability->name() === self::CAPABILITY_NAME && $capability->status() === SellerStatusCapability::STATUS_ACTIVE ) {
|
foreach ( $seller_status->capabilities() as $capability ) {
|
||||||
$has_capability = true;
|
if ( $capability->name() === self::CAPABILITY_NAME && $capability->status() === SellerStatusCapability::STATUS_ACTIVE ) {
|
||||||
|
$has_capability = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $has_capability ) {
|
if ( $has_capability ) {
|
||||||
// Capability found, persist status and return true.
|
|
||||||
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
|
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
|
||||||
$this->settings->persist();
|
} else {
|
||||||
|
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_DISABLED );
|
||||||
$this->current_status = true;
|
|
||||||
return $this->current_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capability not found, persist status and return false.
|
|
||||||
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_DISABLED );
|
|
||||||
$this->settings->persist();
|
$this->settings->persist();
|
||||||
|
|
||||||
$this->current_status = false;
|
return $has_capability;
|
||||||
return $this->current_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** {@inheritDoc} */
|
||||||
* Returns if the seller is onboarded.
|
protected function clear_state( Settings $settings = null ) : void {
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function is_onboarded(): bool {
|
|
||||||
return $this->onboarding_state->current_state() >= State::STATE_ONBOARDED;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @param Settings|null $settings The settings object.
|
|
||||||
* We accept a Settings object to don't override other sequential settings that are being updated elsewhere.
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function clear( Settings $settings = null ): void {
|
|
||||||
if ( null === $settings ) {
|
if ( null === $settings ) {
|
||||||
$settings = $this->settings;
|
$settings = $this->settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->current_status = null;
|
|
||||||
|
|
||||||
if ( $settings->has( self::SETTINGS_KEY ) ) {
|
if ( $settings->has( self::SETTINGS_KEY ) ) {
|
||||||
$settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_UNDEFINED );
|
$settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_UNDEFINED );
|
||||||
$settings->persist();
|
$settings->persist();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue