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

This commit is contained in:
Emili Castells Guasch 2024-08-12 16:08:01 +02:00
commit 1628f86d4c
5 changed files with 28 additions and 46 deletions

View file

@ -1677,8 +1677,7 @@ return array(
return new UserIdToken(
$container->get( 'api.host' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.client-credentials' ),
$container->get( 'api.client-credentials-cache' )
$container->get( 'api.client-credentials' )
);
},
'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken {

View file

@ -96,18 +96,6 @@ 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

@ -11,7 +11,6 @@ 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;
/**
@ -21,8 +20,6 @@ class UserIdToken {
use RequestTrait;
const CACHE_KEY = 'user-id-token-key';
/**
* The host.
*
@ -44,31 +41,21 @@ 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,
Cache $cache
ClientCredentials $client_credentials
) {
$this->host = $host;
$this->logger = $logger;
$this->client_credentials = $client_credentials;
$this->cache = $cache;
}
/**
@ -82,10 +69,6 @@ class UserIdToken {
* @throws RuntimeException If something unexpected happens.
*/
public function id_token( string $target_customer_id = '' ): string {
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 ) {
$url = add_query_arg(
@ -115,11 +98,6 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code );
}
$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;
return $json->id_token;
}
}

View file

@ -644,10 +644,12 @@ class ApplepayButton {
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
simplified_contact:
this.updatedContactInfo ||
this.initialPaymentRequest.shippingContact ||
this.initialPaymentRequest.billingContact,
simplified_contact: this.hasValidContactInfo(
this.updatedContactInfo
)
? this.updatedContactInfo
: this.initialPaymentRequest?.shippingContact ??
this.initialPaymentRequest?.billingContact,
product_id,
products: JSON.stringify( this.products ),
caller_page: 'productDetail',
@ -662,10 +664,12 @@ class ApplepayButton {
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
simplified_contact:
this.updatedContactInfo ||
this.initialPaymentRequest.shippingContact ||
this.initialPaymentRequest.billingContact,
simplified_contact: this.hasValidContactInfo(
this.updatedContactInfo
)
? this.updatedContactInfo
: this.initialPaymentRequest?.shippingContact ??
this.initialPaymentRequest?.billingContact,
caller_page: 'cart',
'woocommerce-process-checkout-nonce': this.nonce,
};
@ -948,6 +952,12 @@ class ApplepayButton {
return btoa( utf8Str );
}
hasValidContactInfo( value ) {
return Array.isArray( value )
? value.length > 0
: Object.keys( value || {} ).length > 0;
}
}
export default ApplepayButton;

View file

@ -31,6 +31,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
/**
* Class SavePaymentMethodsModule
@ -84,6 +85,12 @@ class SavePaymentMethodsModule implements ModuleInterface {
add_filter(
'woocommerce_paypal_payments_localized_script_data',
function( array $localized_script_data ) use ( $c ) {
$subscriptions_helper = $c->get( 'wc-subscriptions.helper' );
assert( $subscriptions_helper instanceof SubscriptionHelper );
if ( ! is_user_logged_in() && ! $subscriptions_helper->cart_contains_subscription() ) {
return $localized_script_data;
}
$api = $c->get( 'api.user-id-token' );
assert( $api instanceof UserIdToken );