mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
🔀 Merge branch 'trunk'
This commit is contained in:
commit
ac98924f52
2 changed files with 31 additions and 3 deletions
|
@ -834,11 +834,15 @@ return array(
|
||||||
'api.client-credentials-cache' => static function( ContainerInterface $container ): Cache {
|
'api.client-credentials-cache' => static function( ContainerInterface $container ): Cache {
|
||||||
return new Cache( 'ppcp-client-credentials-cache' );
|
return new Cache( 'ppcp-client-credentials-cache' );
|
||||||
},
|
},
|
||||||
|
'api.user-id-token-cache' => static function( ContainerInterface $container ): Cache {
|
||||||
|
return new Cache( 'ppcp-id-token-cache' );
|
||||||
|
},
|
||||||
'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken {
|
'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken {
|
||||||
return new UserIdToken(
|
return new UserIdToken(
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||||
$container->get( 'api.client-credentials' )
|
$container->get( 'api.client-credentials' ),
|
||||||
|
$container->get( 'api.user-id-token-cache' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
|
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||||
use WP_Error;
|
use WP_Error;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,6 +21,8 @@ class UserIdToken {
|
||||||
|
|
||||||
use RequestTrait;
|
use RequestTrait;
|
||||||
|
|
||||||
|
const CACHE_KEY = 'id-token-key';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The host.
|
* The host.
|
||||||
*
|
*
|
||||||
|
@ -41,21 +44,31 @@ class UserIdToken {
|
||||||
*/
|
*/
|
||||||
private $client_credentials;
|
private $client_credentials;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The cache.
|
||||||
|
*
|
||||||
|
* @var Cache
|
||||||
|
*/
|
||||||
|
private $cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UserIdToken constructor.
|
* UserIdToken constructor.
|
||||||
*
|
*
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
* @param ClientCredentials $client_credentials The client credentials.
|
* @param ClientCredentials $client_credentials The client credentials.
|
||||||
|
* @param Cache $cache The cache.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $host,
|
string $host,
|
||||||
LoggerInterface $logger,
|
LoggerInterface $logger,
|
||||||
ClientCredentials $client_credentials
|
ClientCredentials $client_credentials,
|
||||||
|
Cache $cache
|
||||||
) {
|
) {
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->client_credentials = $client_credentials;
|
$this->client_credentials = $client_credentials;
|
||||||
|
$this->cache = $cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,6 +82,11 @@ class UserIdToken {
|
||||||
* @throws RuntimeException If something unexpected happens.
|
* @throws RuntimeException If something unexpected happens.
|
||||||
*/
|
*/
|
||||||
public function id_token( string $target_customer_id = '' ): string {
|
public function id_token( string $target_customer_id = '' ): string {
|
||||||
|
$session_customer_id = WC()->session->get_customer_id() ?: '';
|
||||||
|
if ( $session_customer_id && $this->cache->has( self::CACHE_KEY . (string) $session_customer_id ) ) {
|
||||||
|
return $this->cache->get( self::CACHE_KEY . (string) $session_customer_id );
|
||||||
|
}
|
||||||
|
|
||||||
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
|
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
|
||||||
if ( $target_customer_id ) {
|
if ( $target_customer_id ) {
|
||||||
$url = add_query_arg(
|
$url = add_query_arg(
|
||||||
|
@ -98,6 +116,12 @@ class UserIdToken {
|
||||||
throw new PayPalApiException( $json, $status_code );
|
throw new PayPalApiException( $json, $status_code );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $json->id_token;
|
$id_token = $json->id_token;
|
||||||
|
|
||||||
|
if ( $session_customer_id ) {
|
||||||
|
$this->cache->set( self::CACHE_KEY . (string) $session_customer_id, $id_token, 5 );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $id_token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue