Cache the DCC status PayPal response.

This commit is contained in:
Narek Zakarian 2022-09-13 18:26:02 +04:00
parent cbba252be7
commit bd3d886250
4 changed files with 48 additions and 5 deletions

View file

@ -275,6 +275,7 @@ return array(
$signup_link_cache = $container->get( 'onboarding.signup-link-cache' ); $signup_link_cache = $container->get( 'onboarding.signup-link-cache' );
$signup_link_ids = $container->get( 'onboarding.signup-link-ids' ); $signup_link_ids = $container->get( 'onboarding.signup-link-ids' );
$pui_status_cache = $container->get( 'pui.status-cache' ); $pui_status_cache = $container->get( 'pui.status-cache' );
$dcc_status_cache = $container->get( 'dcc.status-cache' );
return new SettingsListener( return new SettingsListener(
$settings, $settings,
$fields, $fields,
@ -285,7 +286,8 @@ return array(
$page_id, $page_id,
$signup_link_cache, $signup_link_cache,
$signup_link_ids, $signup_link_ids,
$pui_status_cache $pui_status_cache,
$dcc_status_cache
); );
}, },
'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor { 'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor {
@ -1914,7 +1916,7 @@ return array(
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$partner_endpoint = $container->get( 'api.endpoint.partners' ); $partner_endpoint = $container->get( 'api.endpoint.partners' );
return new DCCProductStatus( $settings, $partner_endpoint ); return new DCCProductStatus( $settings, $partner_endpoint, $container->get( 'dcc.status-cache' ) );
}, },
'button.helper.messages-disclaimers' => static function ( ContainerInterface $container ): MessagesDisclaimers { 'button.helper.messages-disclaimers' => static function ( ContainerInterface $container ): MessagesDisclaimers {
@ -2235,4 +2237,7 @@ return array(
'pui.status-cache' => static function( ContainerInterface $container ): Cache { 'pui.status-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-pui-status-cache' ); return new Cache( 'ppcp-paypal-pui-status-cache' );
}, },
'dcc.status-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-dcc-status-cache' );
},
); );

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use Throwable; use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/** /**
@ -19,6 +20,15 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/ */
class DCCProductStatus { class DCCProductStatus {
const DCC_STATUS_CACHE_KEY = 'dcc_status_cache';
/**
* The Cache.
*
* @var Cache
*/
protected $cache;
/** /**
* Caches the status for the current load. * Caches the status for the current load.
* *
@ -44,13 +54,16 @@ class DCCProductStatus {
* *
* @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 Cache $cache The cache.
*/ */
public function __construct( public function __construct(
Settings $settings, Settings $settings,
PartnersEndpoint $partners_endpoint PartnersEndpoint $partners_endpoint,
Cache $cache
) { ) {
$this->settings = $settings; $this->settings = $settings;
$this->partners_endpoint = $partners_endpoint; $this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
} }
/** /**
@ -59,6 +72,10 @@ class DCCProductStatus {
* @return bool * @return bool
*/ */
public function dcc_is_active() : bool { public function dcc_is_active() : bool {
if ( $this->cache->has( self::DCC_STATUS_CACHE_KEY ) ) {
return (bool) $this->cache->get( self::DCC_STATUS_CACHE_KEY );
}
if ( is_bool( $this->current_status_cache ) ) { if ( is_bool( $this->current_status_cache ) ) {
return $this->current_status_cache; return $this->current_status_cache;
} }
@ -92,9 +109,11 @@ class DCCProductStatus {
$this->settings->set( 'products_dcc_enabled', true ); $this->settings->set( 'products_dcc_enabled', true );
$this->settings->persist(); $this->settings->persist();
$this->current_status_cache = true; $this->current_status_cache = true;
$this->cache->set( self::DCC_STATUS_CACHE_KEY, true, 3 * MONTH_IN_SECONDS );
return true; return true;
} }
} }
$this->cache->set( self::DCC_STATUS_CACHE_KEY, false, 3 * MONTH_IN_SECONDS );
$this->current_status_cache = false; $this->current_status_cache = false;
return false; return false;

View file

@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus;
use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar; use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
@ -103,6 +104,13 @@ class SettingsListener {
*/ */
protected $pui_status_cache; protected $pui_status_cache;
/**
* The DCC status cache.
*
* @var Cache
*/
protected $dcc_status_cache;
/** /**
* SettingsListener constructor. * SettingsListener constructor.
* *
@ -116,6 +124,7 @@ class SettingsListener {
* @param Cache $signup_link_cache The signup link cache. * @param Cache $signup_link_cache The signup link cache.
* @param array $signup_link_ids Signup link ids. * @param array $signup_link_ids Signup link ids.
* @param Cache $pui_status_cache The PUI status cache. * @param Cache $pui_status_cache The PUI status cache.
* @param Cache $dcc_status_cache The DCC status cache.
*/ */
public function __construct( public function __construct(
Settings $settings, Settings $settings,
@ -127,7 +136,8 @@ class SettingsListener {
string $page_id, string $page_id,
Cache $signup_link_cache, Cache $signup_link_cache,
array $signup_link_ids, array $signup_link_ids,
Cache $pui_status_cache Cache $pui_status_cache,
Cache $dcc_status_cache
) { ) {
$this->settings = $settings; $this->settings = $settings;
@ -140,6 +150,7 @@ class SettingsListener {
$this->signup_link_cache = $signup_link_cache; $this->signup_link_cache = $signup_link_cache;
$this->signup_link_ids = $signup_link_ids; $this->signup_link_ids = $signup_link_ids;
$this->pui_status_cache = $pui_status_cache; $this->pui_status_cache = $pui_status_cache;
$this->dcc_status_cache = $dcc_status_cache;
} }
/** /**
@ -322,6 +333,10 @@ class SettingsListener {
$this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ); $this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY );
} }
if ( $this->dcc_status_cache->has( DCCProductStatus::DCC_STATUS_CACHE_KEY ) ) {
$this->dcc_status_cache->delete( DCCProductStatus::DCC_STATUS_CACHE_KEY );
}
if ( isset( $_GET['ppcp-onboarding-error'] ) ) { if ( isset( $_GET['ppcp-onboarding-error'] ) ) {
$url = remove_query_arg( 'ppcp-onboarding-error' ); $url = remove_query_arg( 'ppcp-onboarding-error' );
wp_safe_redirect( $url, 302 ); wp_safe_redirect( $url, 302 );

View file

@ -37,6 +37,7 @@ class SettingsListenerTest extends ModularTestCase
$signup_link_cache = Mockery::mock(Cache::class); $signup_link_cache = Mockery::mock(Cache::class);
$signup_link_ids = array(); $signup_link_ids = array();
$pui_status_cache = Mockery::mock(Cache::class); $pui_status_cache = Mockery::mock(Cache::class);
$dcc_status_cache = Mockery::mock(Cache::class);
$testee = new SettingsListener( $testee = new SettingsListener(
$settings, $settings,
@ -48,7 +49,8 @@ class SettingsListenerTest extends ModularTestCase
PayPalGateway::ID, PayPalGateway::ID,
$signup_link_cache, $signup_link_cache,
$signup_link_ids, $signup_link_ids,
$pui_status_cache $pui_status_cache,
$dcc_status_cache
); );
$_GET['section'] = PayPalGateway::ID; $_GET['section'] = PayPalGateway::ID;
@ -79,6 +81,8 @@ class SettingsListenerTest extends ModularTestCase
$signup_link_cache->shouldReceive('has') $signup_link_cache->shouldReceive('has')
->andReturn(false); ->andReturn(false);
$pui_status_cache->shouldReceive('has') $pui_status_cache->shouldReceive('has')
->andReturn(false);
$dcc_status_cache->shouldReceive('has')
->andReturn(false); ->andReturn(false);
$testee->listen(); $testee->listen();