Merge branch 'trunk' into PCP-3317-implement-ap-ms-via-orders-api

This commit is contained in:
Emili Castells Guasch 2024-08-08 11:58:33 +02:00
commit c70fb24bdb
19 changed files with 274 additions and 109 deletions

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\ClientCredentials;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
@ -1663,18 +1664,28 @@ return array(
return new PurchaseUnitSanitizer( $behavior, $line_name );
}
),
'api.client-credentials' => static function( ContainerInterface $container ): ClientCredentials {
return new ClientCredentials(
$container->get( 'wcgateway.settings' )
);
},
'api.client-credentials-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-client-credentials-cache' );
},
'api.user-id-token' => static function( ContainerInterface $container ): UserIdToken {
return new UserIdToken(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ),
$container->get( 'api.client-credentials-cache' )
);
},
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {
return new SdkClientToken(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ),
$container->get( 'api.client-credentials-cache' )
);
},
);

View file

@ -10,6 +10,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
@ -94,6 +96,18 @@ class ApiModule implements ModuleInterface {
10,
2
);
add_action(
'wp_logout',
function( int $user_id ) use ( $c ) {
$client_credentials_cache = $c->get( 'api.client-credentials-cache' );
assert( $client_credentials_cache instanceof Cache );
if ( $client_credentials_cache->has( UserIdToken::CACHE_KEY . '-' . (string) $user_id ) ) {
$client_credentials_cache->delete( UserIdToken::CACHE_KEY . '-' . (string) $user_id );
}
}
);
}
/**

View file

@ -0,0 +1,49 @@
<?php
/**
* The client credentials.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class ClientCredentials
*/
class ClientCredentials {
/**
* The settings.
*
* @var Settings
*/
protected $settings;
/**
* ClientCredentials constructor.
*
* @param Settings $settings The settings.
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
}
/**
* Returns encoded client credentials.
*
* @return string
* @throws NotFoundException If setting does not found.
*/
public function credentials(): string {
$client_id = $this->settings->has( 'client_id' ) ? $this->settings->get( 'client_id' ) : '';
$client_secret = $this->settings->has( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : '';
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
return 'Basic ' . base64_encode( $client_id . ':' . $client_secret );
}
}

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.
*
@ -27,13 +30,6 @@ class SdkClientToken {
*/
private $host;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* The logger.
*
@ -41,35 +37,52 @@ class SdkClientToken {
*/
private $logger;
/**
* The client credentials.
*
* @var ClientCredentials
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* SdkClientToken constructor.
*
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
* @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,
Bearer $bearer,
LoggerInterface $logger
LoggerInterface $logger,
ClientCredentials $client_credentials,
Cache $cache
) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
* Returns `sdk_client_token` which uniquely identifies the payer.
*
* @param string $target_customer_id Vaulted customer id.
* Returns the client token for SDK `data-sdk-client-token`.
*
* @return string
*
* @throws PayPalApiException If the request fails.
* @throws RuntimeException If something unexpected happens.
*/
public function sdk_client_token( string $target_customer_id = '' ): string {
$bearer = $this->bearer->bearer();
public function sdk_client_token(): 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'] ?? '' );
@ -77,19 +90,10 @@ class SdkClientToken {
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=client_token&intent=sdk_init&domains[]=' . $domain;
if ( $target_customer_id ) {
$url = add_query_arg(
array(
'target_customer_id' => $target_customer_id,
),
$url
);
}
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Authorization' => $this->client_credentials->credentials(),
'Content-Type' => 'application/x-www-form-urlencoded',
),
);
@ -105,6 +109,11 @@ class SdkClientToken {
throw new PayPalApiException( $json, $status_code );
}
return $json->access_token;
$access_token = $json->access_token;
$expires_in = (int) $json->expires_in;
$this->cache->set( self::CACHE_KEY, $access_token, $expires_in );
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.
*
@ -27,13 +30,6 @@ class UserIdToken {
*/
private $host;
/**
* The bearer.
*
* @var Bearer
*/
private $bearer;
/**
* The logger.
*
@ -41,21 +37,38 @@ class UserIdToken {
*/
private $logger;
/**
* The client credentials.
*
* @var ClientCredentials
*/
private $client_credentials;
/**
* The cache.
*
* @var Cache
*/
private $cache;
/**
* UserIdToken constructor.
*
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
* @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,
Bearer $bearer,
LoggerInterface $logger
LoggerInterface $logger,
ClientCredentials $client_credentials,
Cache $cache
) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
@ -69,7 +82,9 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
$bearer = $this->bearer->bearer();
if ( $this->cache->has( self::CACHE_KEY . '-' . (string) get_current_user_id() ) ) {
return $this->cache->get( self::CACHE_KEY . '-' . (string) get_current_user_id() );
}
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) {
@ -84,7 +99,7 @@ class UserIdToken {
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Authorization' => $this->client_credentials->credentials(),
'Content-Type' => 'application/x-www-form-urlencoded',
),
);
@ -100,6 +115,11 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code );
}
return $json->id_token;
$id_token = $json->id_token;
$expires_in = (int) $json->expires_in;
$this->cache->set( self::CACHE_KEY . '-' . (string) get_current_user_id(), $id_token, $expires_in );
return $id_token;
}
}