Add SellerStatus API caching.

Add GooglePay capability notice.
This commit is contained in:
Pedro Silva 2023-09-25 10:12:27 +01:00
parent 1581f34a44
commit 561d43baf6
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
7 changed files with 140 additions and 26 deletions

View file

@ -136,15 +136,9 @@ class PartnersEndpoint {
'response' => $response,
)
);
// Register the failure on api failure registry.
$this->failure_registry->add_failure( FailureRegistry::SELLER_STATUS_KEY );
throw $error;
}
$this->failure_registry->clear_failures( FailureRegistry::SELLER_STATUS_KEY );
$json = json_decode( wp_remote_retrieve_body( $response ) );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
@ -157,9 +151,15 @@ class PartnersEndpoint {
'response' => $response,
)
);
// Register the failure on api failure registry.
$this->failure_registry->add_failure( FailureRegistry::SELLER_STATUS_KEY );
throw $error;
}
$this->failure_registry->clear_failures( FailureRegistry::SELLER_STATUS_KEY );
$status = $this->seller_status_factory->from_paypal_reponse( $json );
return $status;
}

View file

@ -41,7 +41,8 @@ return array(
$container->get( 'wcgateway.settings' ),
$container->get( 'api.endpoint.partners' ),
$container->get( 'applepay.status-cache' ),
$container->get( 'onboarding.state' )
$container->get( 'onboarding.state' ),
$container->get( 'api.helper.failure-registry' )
);
},
'applepay.enabled' => static function ( ContainerInterface $container ): bool {

View file

@ -13,6 +13,7 @@ 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\FailureRegistry;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -36,6 +37,14 @@ class AppleProductStatus {
* @var bool|null
*/
private $current_status_cache;
/**
* If there was a request failure.
*
* @var bool
*/
private $has_request_failure = false;
/**
* The settings.
*
@ -57,6 +66,13 @@ class AppleProductStatus {
*/
private $onboarding_state;
/**
* The API failure registry
*
* @var FailureRegistry
*/
private $api_failure_registry;
/**
* PayUponInvoiceProductStatus constructor.
*
@ -64,17 +80,20 @@ class AppleProductStatus {
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param Cache $cache The cache.
* @param State $onboarding_state The onboarding state.
* @param FailureRegistry $api_failure_registry The API failure registry.
*/
public function __construct(
Settings $settings,
PartnersEndpoint $partners_endpoint,
Cache $cache,
State $onboarding_state
State $onboarding_state,
FailureRegistry $api_failure_registry
) {
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
$this->onboarding_state = $onboarding_state;
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
$this->onboarding_state = $onboarding_state;
$this->api_failure_registry = $api_failure_registry;
}
/**
@ -99,9 +118,17 @@ class AppleProductStatus {
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;
}
@ -124,4 +151,14 @@ class AppleProductStatus {
$this->current_status_cache = false;
return false;
}
/**
* Returns if there was a request failure.
*
* @return bool
*/
public function has_request_failure(): bool {
return $this->has_request_failure;
}
}

View file

@ -70,7 +70,7 @@ class AvailabilityNotice {
$message = sprintf(
__(
'<p>There was an error getting your PayPal seller status. Some features may be disabled.</p><p>Certify that you connected to your account via our onboarding process.</p>',
'<p>There was an error getting your PayPal seller status. Some features may be disabled.</p><p>Certify that you connected to your PayPal business account via our onboarding process.</p>',
'woocommerce-paypal-payments'
)
);

View file

@ -1017,7 +1017,8 @@ return array(
$partner_endpoint,
$container->get( 'dcc.status-cache' ),
$container->get( 'api.helpers.dccapplies' ),
$container->get( 'onboarding.state' )
$container->get( 'onboarding.state' ),
$container->get( 'api.helper.failure-registry' )
);
},
@ -1097,7 +1098,8 @@ return array(
$container->get( 'wcgateway.settings' ),
$container->get( 'api.endpoint.partners' ),
$container->get( 'pui.status-cache' ),
$container->get( 'onboarding.state' )
$container->get( 'onboarding.state' ),
$container->get( 'api.helper.failure-registry' )
);
},
'wcgateway.pay-upon-invoice' => static function ( ContainerInterface $container ): PayUponInvoice {

View file

@ -14,6 +14,7 @@ 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;
@ -37,6 +38,14 @@ class DCCProductStatus {
* @var bool|null
*/
private $current_status_cache;
/**
* If there was a request failure.
*
* @var bool
*/
private $has_request_failure = false;
/**
* The settings.
*
@ -65,6 +74,13 @@ class DCCProductStatus {
*/
private $onboarding_state;
/**
* The API failure registry
*
* @var FailureRegistry
*/
private $api_failure_registry;
/**
* DccProductStatus constructor.
*
@ -73,19 +89,22 @@ class DCCProductStatus {
* @param Cache $cache The cache.
* @param DccApplies $dcc_applies The dcc applies helper.
* @param State $onboarding_state The onboarding state.
* @param FailureRegistry $api_failure_registry The API failure registry.
*/
public function __construct(
Settings $settings,
PartnersEndpoint $partners_endpoint,
Cache $cache,
DccApplies $dcc_applies,
State $onboarding_state
State $onboarding_state,
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->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;
}
/**
@ -111,9 +130,17 @@ class DCCProductStatus {
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;
}
@ -149,4 +176,14 @@ class DCCProductStatus {
$this->current_status_cache = false;
return false;
}
/**
* Returns if there was a request failure.
*
* @return bool
*/
public function has_request_failure(): bool {
return $this->has_request_failure;
}
}

View file

@ -13,6 +13,7 @@ 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\FailureRegistry;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -36,6 +37,14 @@ class PayUponInvoiceProductStatus {
* @var bool|null
*/
private $current_status_cache;
/**
* If there was a request failure.
*
* @var bool
*/
private $has_request_failure = false;
/**
* The settings.
*
@ -57,6 +66,13 @@ class PayUponInvoiceProductStatus {
*/
private $onboarding_state;
/**
* The API failure registry
*
* @var FailureRegistry
*/
private $api_failure_registry;
/**
* PayUponInvoiceProductStatus constructor.
*
@ -64,17 +80,20 @@ class PayUponInvoiceProductStatus {
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param Cache $cache The cache.
* @param State $onboarding_state The onboarding state.
* @param FailureRegistry $api_failure_registry The API failure registry.
*/
public function __construct(
Settings $settings,
PartnersEndpoint $partners_endpoint,
Cache $cache,
State $onboarding_state
State $onboarding_state,
FailureRegistry $api_failure_registry
) {
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
$this->onboarding_state = $onboarding_state;
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->cache = $cache;
$this->onboarding_state = $onboarding_state;
$this->api_failure_registry = $api_failure_registry;
}
/**
@ -99,9 +118,17 @@ class PayUponInvoiceProductStatus {
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;
}
@ -136,4 +163,14 @@ class PayUponInvoiceProductStatus {
$this->current_status_cache = false;
return false;
}
/**
* Returns if there was a request failure.
*
* @return bool
*/
public function has_request_failure(): bool {
return $this->has_request_failure;
}
}