From f52117c196de1cba1ef3c3e89c9991d6b1e4b538 Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Thu, 8 Aug 2024 15:38:08 +0200 Subject: [PATCH 1/3] Apple Pay: Fix shipping method switching inside of the Apple Pay modal in Classic Checkout --- .../resources/js/ApplepayButton.js | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/modules/ppcp-applepay/resources/js/ApplepayButton.js b/modules/ppcp-applepay/resources/js/ApplepayButton.js index 2bcac16fc..281a08ae1 100644 --- a/modules/ppcp-applepay/resources/js/ApplepayButton.js +++ b/modules/ppcp-applepay/resources/js/ApplepayButton.js @@ -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; From dbf303b2f6befab55e9e6653eacb2106a27012e4 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 9 Aug 2024 12:08:24 +0200 Subject: [PATCH 2/3] Do not cache access tokens for `data-user-id-token` --- .../src/Authentication/UserIdToken.php | 11 +---------- .../src/SavePaymentMethodsModule.php | 7 +++++++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 9487a7238..2552483ee 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -82,10 +82,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 +111,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; } } diff --git a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php index b667fb10b..f82095bf4 100644 --- a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php +++ b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php @@ -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 ); From 627328df9c58147594f8a0a2739c0904047970cf Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 9 Aug 2024 12:33:05 +0200 Subject: [PATCH 3/3] Remove cache references --- modules/ppcp-api-client/services.php | 3 +-- modules/ppcp-api-client/src/ApiModule.php | 12 ------------ .../src/Authentication/UserIdToken.php | 15 +-------------- 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index 7f62da5f6..70720e0b4 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -1669,8 +1669,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 { diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php index 2d63bd80f..8b2f43911 100644 --- a/modules/ppcp-api-client/src/ApiModule.php +++ b/modules/ppcp-api-client/src/ApiModule.php @@ -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 ); - } - } - ); } /** diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 2552483ee..dde47c223 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -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; } /**