♻️ Use new base class for DCC status check

This commit is contained in:
Philipp Stracker 2025-01-30 17:43:15 +01:00
parent 4006605964
commit e6a6cbb7b1
No known key found for this signature in database
6 changed files with 51 additions and 126 deletions

View file

@ -1431,7 +1431,7 @@ return array(
$partner_endpoint,
$container->get( 'dcc.status-cache' ),
$container->get( 'api.helpers.dccapplies' ),
$container->get( 'onboarding.state' ),
$container->get( 'settings.flag.is-connected' ),
$container->get( 'api.helper.failure-registry' )
);
},
@ -1728,7 +1728,7 @@ return array(
$environment = $container->get( 'onboarding.environment' );
assert( $environment instanceof Environment );
$dcc_enabled = $dcc_product_status->dcc_is_active();
$dcc_enabled = $dcc_product_status->is_active();
$enabled_status_text = esc_html__( 'Status: Available', 'woocommerce-paypal-payments' );
$disabled_status_text = esc_html__( 'Status: Not yet enabled', 'woocommerce-paypal-payments' );

View file

@ -9,86 +9,55 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ProductStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
/**
* Class DccProductStatus
*/
class DCCProductStatus {
class DCCProductStatus extends ProductStatus {
public const SETTINGS_KEY = 'products_dcc_enabled';
public const DCC_STATUS_CACHE_KEY = 'dcc_status_cache';
const DCC_STATUS_CACHE_KEY = 'dcc_status_cache';
public const SETTINGS_VALUE_ENABLED = 'yes';
public const SETTINGS_VALUE_DISABLED = 'no';
public const SETTINGS_VALUE_UNDEFINED = '';
/**
* The Cache.
*
* @var Cache
*/
protected $cache;
/**
* Caches the status for the current load.
*
* @var bool|null
*/
private $current_status_cache;
/**
* If there was a request failure.
*
* @var bool
*/
private $has_request_failure = false;
protected Cache $cache;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The partners endpoint.
*
* @var PartnersEndpoint
*/
private $partners_endpoint;
private Settings $settings;
/**
* The dcc applies helper.
*
* @var DccApplies
*/
protected $dcc_applies;
/**
* The onboarding state.
*
* @var State
*/
private $onboarding_state;
/**
* The API failure registry
*
* @var FailureRegistry
*/
private $api_failure_registry;
protected DccApplies $dcc_applies;
/**
* DccProductStatus constructor.
*
* @param Settings $settings The Settings.
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param Cache $cache The cache.
* @param DccApplies $dcc_applies The dcc applies helper.
* @param State $onboarding_state The onboarding state.
* @param Settings $settings The Settings.
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param Cache $cache The cache.
* @param DccApplies $dcc_applies The dcc applies helper.
* @param bool $is_connected The onboarding state.
* @param FailureRegistry $api_failure_registry The API failure registry.
*/
public function __construct(
@ -96,55 +65,31 @@ class DCCProductStatus {
PartnersEndpoint $partners_endpoint,
Cache $cache,
DccApplies $dcc_applies,
State $onboarding_state,
bool $is_connected,
FailureRegistry $api_failure_registry
) {
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
$this->dcc_applies = $dcc_applies;
$this->onboarding_state = $onboarding_state;
$this->api_failure_registry = $api_failure_registry;
parent::__construct( $is_connected, $partners_endpoint, $api_failure_registry );
$this->settings = $settings;
$this->cache = $cache;
$this->dcc_applies = $dcc_applies;
}
/**
* Whether the active/subscribed products support DCC.
*
* @return bool
*/
public function dcc_is_active() : bool {
if ( $this->onboarding_state->current_state() < State::STATE_ONBOARDED ) {
return false;
}
/** {@inheritDoc} */
protected function check_local_state() : ?bool {
if ( $this->cache->has( self::DCC_STATUS_CACHE_KEY ) ) {
return $this->cache->get( self::DCC_STATUS_CACHE_KEY ) === 'true';
return wc_string_to_bool( $this->cache->get( self::DCC_STATUS_CACHE_KEY ) );
}
if ( $this->current_status_cache === true ) {
return $this->current_status_cache;
if ( $this->settings->has( self::SETTINGS_KEY ) && ( $this->settings->get( self::SETTINGS_KEY ) ) ) {
return wc_string_to_bool( $this->settings->get( self::SETTINGS_KEY ) );
}
if ( $this->settings->has( 'products_dcc_enabled' ) && $this->settings->get( 'products_dcc_enabled' ) === true ) {
$this->current_status_cache = true;
return true;
}
// Check API failure registry to prevent multiple failed API requests.
if ( $this->api_failure_registry->has_failure_in_timeframe( FailureRegistry::SELLER_STATUS_KEY, HOUR_IN_SECONDS ) ) {
$this->has_request_failure = true;
$this->current_status_cache = false;
return $this->current_status_cache;
}
try {
$seller_status = $this->partners_endpoint->seller_status();
} catch ( Throwable $error ) {
$this->has_request_failure = true;
$this->current_status_cache = false;
return false;
}
return null;
}
/** {@inheritDoc} */
protected function check_active_state( SellerStatus $seller_status ) : bool {
foreach ( $seller_status->products() as $product ) {
if ( ! in_array(
$product->vetting_status(),
@ -159,57 +104,37 @@ class DCCProductStatus {
}
if ( in_array( 'CUSTOM_CARD_PROCESSING', $product->capabilities(), true ) ) {
$this->settings->set( 'products_dcc_enabled', true );
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
$this->settings->persist();
$this->current_status_cache = true;
$this->cache->set( self::DCC_STATUS_CACHE_KEY, 'true', MONTH_IN_SECONDS );
$this->cache->set( self::DCC_STATUS_CACHE_KEY, self::SETTINGS_VALUE_ENABLED, MONTH_IN_SECONDS );
return true;
}
}
$expiration = MONTH_IN_SECONDS;
if ( $this->dcc_applies->for_country_currency() ) {
$expiration = 3 * HOUR_IN_SECONDS;
} else {
$expiration = MONTH_IN_SECONDS;
}
$this->cache->set( self::DCC_STATUS_CACHE_KEY, 'false', $expiration );
$this->current_status_cache = false;
$this->cache->set( self::DCC_STATUS_CACHE_KEY, self::SETTINGS_VALUE_DISABLED, $expiration );
return false;
}
/**
* 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 {
/** {@inheritDoc} */
protected function clear_state( Settings $settings = null ): void {
if ( null === $settings ) {
$settings = $this->settings;
}
// Unset check stored in memory.
$this->current_status_cache = null;
// Unset settings flag.
$settings_key = 'products_dcc_enabled';
if ( $settings->has( $settings_key ) ) {
$settings->set( $settings_key, false );
if ( $settings->has( self::SETTINGS_KEY ) ) {
$settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_UNDEFINED );
$settings->persist();
}
// Delete cached value.
$this->cache->delete( self::DCC_STATUS_CACHE_KEY );
}
}

View file

@ -168,7 +168,7 @@ class SectionsRenderer {
}
}
if ( ! $this->dcc_product_status->dcc_is_active() || ! $this->dcc_applies->for_country_currency() ) {
if ( ! $this->dcc_product_status->is_active() || ! $this->dcc_applies->for_country_currency() ) {
unset( $sections['ppcp-credit-card-gateway'] );
}

View file

@ -465,7 +465,7 @@ $data_rows_html
if ( $this->dcc_applies->for_country_currency() ) {
if ( State::STATE_ONBOARDED > $this->state->current_state() ) {
$this->render_dcc_onboarding_info();
} elseif ( ! $this->dcc_product_status->dcc_is_active() ) {
} elseif ( ! $this->dcc_product_status->is_active() ) {
$this->render_dcc_not_active_yet();
}
} else {

View file

@ -341,7 +341,7 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
// Update caches.
$dcc_status = $c->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_status instanceof DCCProductStatus );
$dcc_status->dcc_is_active();
$dcc_status->is_active();
$pui_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' );
assert( $pui_status instanceof PayUponInvoiceProductStatus );
@ -668,7 +668,7 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
// Performing the full DCCProductStatus check only when on the gateway list page
// to avoid sending the API requests all the time.
( $is_our_page ||
( $is_gateways_list_page && $dcc_product_status->dcc_is_active() ) ||
( $is_gateways_list_page && $dcc_product_status->is_active() ) ||
( $settings->has( 'products_dcc_enabled' ) && $settings->get( 'products_dcc_enabled' ) )
)
) {