Add tokens cache

This commit is contained in:
Emili Castells Guasch 2024-08-06 10:44:54 +02:00
parent bea17db588
commit 54657ed649
7 changed files with 107 additions and 28 deletions

View file

@ -1657,7 +1657,7 @@ return array(
return new PurchaseUnitSanitizer( $behavior, $line_name );
}
),
'api.client-credentials' => static function(ContainerInterface $container): ClientCredentials {
'api.client-credentials' => static function( ContainerInterface $container ): ClientCredentials {
return new ClientCredentials(
$container->get( 'wcgateway.settings' )
);
@ -1666,14 +1666,16 @@ return array(
return new UserIdToken(
$container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' )
$container->get( 'api.client-credentials' ),
new Cache( 'ppcp-client-credentials-cache' )
);
},
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
return new SdkClientToken(
$container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' )
$container->get( 'api.client-credentials' ),
new Cache( 'ppcp-client-credentials-cache' )
);
},
);

View file

@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error;
/**
@ -20,6 +21,8 @@ class SdkClientToken {
use RequestTrait;
const CACHE_KEY = 'sdk-client-token-key';
/**
* The host.
*
@ -41,21 +44,31 @@ class SdkClientToken {
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* SdkClientToken constructor.
*
* @param string $host The host.
* @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/
public function __construct(
string $host,
LoggerInterface $logger,
ClientCredentials $client_credentials
ClientCredentials $client_credentials,
Cache $cache
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
@ -69,6 +82,10 @@ class SdkClientToken {
* @throws RuntimeException If something unexpected happens.
*/
public function sdk_client_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
return $this->cache->get( self::CACHE_KEY );
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' );
$domain = preg_replace( '/^www\./', '', $domain );
@ -103,6 +120,9 @@ class SdkClientToken {
throw new PayPalApiException( $json, $status_code );
}
return $json->access_token;
$access_token = $json->access_token;
$this->cache->set( self::CACHE_KEY, $access_token );
return $access_token;
}
}

View file

@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WP_Error;
/**
@ -20,6 +21,8 @@ class UserIdToken {
use RequestTrait;
const CACHE_KEY = 'user-id-token-key';
/**
* The host.
*
@ -41,21 +44,31 @@ class UserIdToken {
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* UserIdToken constructor.
*
* @param string $host The host.
* @param LoggerInterface $logger The logger.
* @param ClientCredentials $client_credentials The client credentials.
* @param Cache $cache The cache.
*/
public function __construct(
string $host,
LoggerInterface $logger,
ClientCredentials $client_credentials
ClientCredentials $client_credentials,
Cache $cache
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
@ -69,6 +82,10 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
if ( $this->cache->has( self::CACHE_KEY ) ) {
return $this->cache->get( self::CACHE_KEY );
}
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) {
$url = add_query_arg(
@ -98,6 +115,9 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code );
}
return $json->id_token;
$id_token = $json->id_token;
$this->cache->set( self::CACHE_KEY, $id_token );
return $id_token;
}
}

View file

@ -189,9 +189,8 @@ return array(
$login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
$settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$cache = new Cache( 'ppcp-paypal-bearer' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new LoginSellerEndpoint(
$request_data,
$login_seller_production,
@ -199,7 +198,8 @@ return array(
$partner_referrals_data,
$settings,
$cache,
$logger
$logger,
new Cache( 'ppcp-client-credentials-cache' )
);
},
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint {

View file

@ -12,6 +12,8 @@ namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
@ -76,6 +78,13 @@ class LoginSellerEndpoint implements EndpointInterface {
*/
protected $logger;
/**
* The client credentials cache.
*
* @var Cache
*/
private $client_credentials_cache;
/**
* LoginSellerEndpoint constructor.
*
@ -86,6 +95,7 @@ class LoginSellerEndpoint implements EndpointInterface {
* @param Settings $settings The Settings.
* @param Cache $cache The Cache.
* @param LoggerInterface $logger The logger.
* @param Cache $client_credentials_cache The client credentials cache.
*/
public function __construct(
RequestData $request_data,
@ -94,7 +104,8 @@ class LoginSellerEndpoint implements EndpointInterface {
PartnerReferralsData $partner_referrals_data,
Settings $settings,
Cache $cache,
LoggerInterface $logger
LoggerInterface $logger,
Cache $client_credentials_cache
) {
$this->request_data = $request_data;
@ -104,6 +115,7 @@ class LoginSellerEndpoint implements EndpointInterface {
$this->settings = $settings;
$this->cache = $cache;
$this->logger = $logger;
$this->client_credentials_cache = $client_credentials_cache;
}
/**
@ -175,6 +187,12 @@ class LoginSellerEndpoint implements EndpointInterface {
if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) {
$this->cache->delete( PayPalBearer::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( UserIdToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( UserIdToken::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( SdkClientToken::CACHE_KEY );
}
wp_schedule_single_event(
time() + 5,

View file

@ -366,7 +366,8 @@ return array(
$container->get( 'api.partner_merchant_id-production' ),
$container->get( 'api.partner_merchant_id-sandbox' ),
$container->get( 'api.endpoint.billing-agreements' ),
$logger
$logger,
new Cache( 'ppcp-client-credentials-cache' )
);
},
'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor {

View file

@ -12,6 +12,8 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
@ -164,6 +166,13 @@ class SettingsListener {
*/
private $logger;
/**
* The client credentials cache.
*
* @var Cache
*/
private $client_credentials_cache;
/**
* SettingsListener constructor.
*
@ -183,6 +192,7 @@ class SettingsListener {
* @param string $partner_merchant_id_sandbox Partner merchant ID sandbox.
* @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint.
* @param ?LoggerInterface $logger The logger.
* @param Cache $client_credentials_cache The client credentials cache.
*/
public function __construct(
Settings $settings,
@ -200,7 +210,8 @@ class SettingsListener {
string $partner_merchant_id_production,
string $partner_merchant_id_sandbox,
BillingAgreementsEndpoint $billing_agreements_endpoint,
LoggerInterface $logger = null
LoggerInterface $logger = null,
Cache $client_credentials_cache
) {
$this->settings = $settings;
@ -219,6 +230,7 @@ class SettingsListener {
$this->partner_merchant_id_sandbox = $partner_merchant_id_sandbox;
$this->billing_agreements_endpoint = $billing_agreements_endpoint;
$this->logger = $logger ?: new NullLogger();
$this->client_credentials_cache = $client_credentials_cache;
}
/**
@ -490,6 +502,12 @@ class SettingsListener {
if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) {
$this->cache->delete( PayPalBearer::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( UserIdToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( UserIdToken::CACHE_KEY );
}
if ( $this->client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) {
$this->client_credentials_cache->delete( SdkClientToken::CACHE_KEY );
}
if ( $this->pui_status_cache->has( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ) ) {
$this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY );