From bea17db5881dbc80c058fe7eca812f0d82ff4cdc Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 09:25:28 +0200 Subject: [PATCH 01/15] Use client credentials --- modules/ppcp-api-client/services.php | 14 +++++-- .../src/Authentication/ClientCredentials.php | 41 +++++++++++++++++++ .../src/Authentication/SdkClientToken.php | 26 ++++++------ .../src/Authentication/UserIdToken.php | 26 ++++++------ 4 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 modules/ppcp-api-client/src/Authentication/ClientCredentials.php diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index 241240ce2..7cb890c18 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -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\PaymentMethodTokensEndpoint; @@ -1656,18 +1657,23 @@ return array( return new PurchaseUnitSanitizer( $behavior, $line_name ); } ), + 'api.client-credentials' => static function(ContainerInterface $container): ClientCredentials { + return new ClientCredentials( + $container->get( 'wcgateway.settings' ) + ); + }, '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' ) ); }, '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' ) ); }, ); diff --git a/modules/ppcp-api-client/src/Authentication/ClientCredentials.php b/modules/ppcp-api-client/src/Authentication/ClientCredentials.php new file mode 100644 index 000000000..7ec4628a5 --- /dev/null +++ b/modules/ppcp-api-client/src/Authentication/ClientCredentials.php @@ -0,0 +1,41 @@ +settings = $settings; + } + + 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' ) : ''; + + return 'Basic ' . base64_encode($client_id . ':' . $client_secret); + } +} diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index ba0f3a373..c55847d30 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -27,13 +27,6 @@ class SdkClientToken { */ private $host; - /** - * The bearer. - * - * @var Bearer - */ - private $bearer; - /** * The logger. * @@ -41,21 +34,28 @@ class SdkClientToken { */ private $logger; + /** + * The client credentials. + * + * @var ClientCredentials + */ + private $client_credentials; + /** * SdkClientToken constructor. * * @param string $host The host. - * @param Bearer $bearer The bearer. * @param LoggerInterface $logger The logger. + * @param ClientCredentials $client_credentials The client credentials. */ public function __construct( string $host, - Bearer $bearer, - LoggerInterface $logger + LoggerInterface $logger, + ClientCredentials $client_credentials ) { $this->host = $host; - $this->bearer = $bearer; $this->logger = $logger; + $this->client_credentials = $client_credentials; } /** @@ -69,8 +69,6 @@ class SdkClientToken { * @throws RuntimeException If something unexpected happens. */ public function sdk_client_token( string $target_customer_id = '' ): string { - $bearer = $this->bearer->bearer(); - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $domain = wp_unslash( $_SERVER['HTTP_HOST'] ?? '' ); $domain = preg_replace( '/^www\./', '', $domain ); @@ -89,7 +87,7 @@ class SdkClientToken { $args = array( 'method' => 'POST', 'headers' => array( - 'Authorization' => 'Bearer ' . $bearer->token(), + 'Authorization' => $this->client_credentials->credentials(), 'Content-Type' => 'application/x-www-form-urlencoded', ), ); diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index cae8cb58a..05dc62282 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -27,13 +27,6 @@ class UserIdToken { */ private $host; - /** - * The bearer. - * - * @var Bearer - */ - private $bearer; - /** * The logger. * @@ -41,21 +34,28 @@ class UserIdToken { */ private $logger; + /** + * The client credentials. + * + * @var ClientCredentials + */ + private $client_credentials; + /** * UserIdToken constructor. * * @param string $host The host. - * @param Bearer $bearer The bearer. * @param LoggerInterface $logger The logger. + * @param ClientCredentials $client_credentials The client credentials. */ public function __construct( string $host, - Bearer $bearer, - LoggerInterface $logger + LoggerInterface $logger, + ClientCredentials $client_credentials ) { $this->host = $host; - $this->bearer = $bearer; $this->logger = $logger; + $this->client_credentials = $client_credentials; } /** @@ -69,8 +69,6 @@ class UserIdToken { * @throws RuntimeException If something unexpected happens. */ public function id_token( string $target_customer_id = '' ): string { - $bearer = $this->bearer->bearer(); - $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; if ( $target_customer_id ) { $url = add_query_arg( @@ -84,7 +82,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', ), ); From 54657ed649352f3d5b8172c245bc4e9499bd9104 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 10:44:54 +0200 Subject: [PATCH 02/15] Add tokens cache --- modules/ppcp-api-client/services.php | 8 +++-- .../src/Authentication/SdkClientToken.php | 32 +++++++++++++---- .../src/Authentication/UserIdToken.php | 32 +++++++++++++---- modules/ppcp-onboarding/services.php | 6 ++-- .../src/Endpoint/LoginSellerEndpoint.php | 34 ++++++++++++++----- modules/ppcp-wc-gateway/services.php | 3 +- .../src/Settings/SettingsListener.php | 20 ++++++++++- 7 files changed, 107 insertions(+), 28 deletions(-) diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index 7cb890c18..dd2c786a1 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -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' ) ); }, ); diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index c55847d30..2e1f3a7ca 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -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 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->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; } } diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 05dc62282..2d18d0507 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -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 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->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; } } diff --git a/modules/ppcp-onboarding/services.php b/modules/ppcp-onboarding/services.php index b76519f62..e7f493999 100644 --- a/modules/ppcp-onboarding/services.php +++ b/modules/ppcp-onboarding/services.php @@ -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 { diff --git a/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php b/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php index 20ab7b95a..2a30ad6b1 100644 --- a/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php +++ b/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php @@ -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,16 +104,18 @@ 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; - $this->login_seller_production = $login_seller_production; - $this->login_seller_sandbox = $login_seller_sandbox; - $this->partner_referrals_data = $partner_referrals_data; - $this->settings = $settings; - $this->cache = $cache; - $this->logger = $logger; + $this->request_data = $request_data; + $this->login_seller_production = $login_seller_production; + $this->login_seller_sandbox = $login_seller_sandbox; + $this->partner_referrals_data = $partner_referrals_data; + $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, diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index ce41619ca..52a41576e 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -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 { diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php index 9986d5edc..e7e39a9e6 100644 --- a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -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 ); From bc58fce8a0853178fa04bf9398f3d89320e4af1b Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Tue, 6 Aug 2024 10:56:37 +0200 Subject: [PATCH 03/15] Apple Pay: Fix the shipping callback --- .../ppcp-applepay/resources/js/Context/BaseHandler.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/modules/ppcp-applepay/resources/js/Context/BaseHandler.js b/modules/ppcp-applepay/resources/js/Context/BaseHandler.js index f763ac5d9..00d7bd463 100644 --- a/modules/ppcp-applepay/resources/js/Context/BaseHandler.js +++ b/modules/ppcp-applepay/resources/js/Context/BaseHandler.js @@ -1,6 +1,5 @@ import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler'; import CartActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler'; -import { isPayPalSubscription } from '../../../../ppcp-blocks/resources/js/Helper/Subscription'; class BaseHandler { constructor( buttonConfig, ppcpConfig ) { @@ -24,7 +23,7 @@ class BaseHandler { } shippingAllowed() { - return this.buttonConfig.product.needsShipping; + return this.buttonConfig.product.needShipping; } transactionInfo() { @@ -76,13 +75,6 @@ class BaseHandler { document.querySelector( '.woocommerce-notices-wrapper' ) ); } - - errorHandler() { - return new ErrorHandler( - this.ppcpConfig.labels.error.generic, - document.querySelector( '.woocommerce-notices-wrapper' ) - ); - } } export default BaseHandler; From 9c380917c55ac75c4d394264da9c9dd2f705c8b0 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 11:14:46 +0200 Subject: [PATCH 04/15] Fix phpunit --- tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php index c34146f3e..c747b19b8 100644 --- a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php +++ b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php @@ -43,6 +43,7 @@ class SettingsListenerTest extends ModularTestCase $billing_agreement_endpoint = Mockery::mock(BillingAgreementsEndpoint::class); $subscription_helper = Mockery::mock(SubscriptionHelper::class); $logger = Mockery::mock(LoggerInterface::class); + $client_credentials_cache = Mockery::mock(Cache::class); $testee = new SettingsListener( $settings, @@ -60,7 +61,8 @@ class SettingsListenerTest extends ModularTestCase '', '', $billing_agreement_endpoint, - $logger + $logger, + $client_credentials_cache ); $_GET['section'] = PayPalGateway::ID; From 86860d4fd6c285d2497cc0b6f5458a89516b24c3 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 11:23:50 +0200 Subject: [PATCH 05/15] Fix phpunit --- tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php index c747b19b8..efa1b7de6 100644 --- a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php +++ b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php @@ -96,6 +96,9 @@ class SettingsListenerTest extends ModularTestCase ->andReturn(false); $dcc_status_cache->shouldReceive('has') ->andReturn(false); + $client_credentials_cache->shouldReceive('has')->andReturn(true); + $client_credentials_cache->shouldReceive('delete'); + $testee->listen(); } From c68db5c1a11f961ad7890fb9695e58e33edc594c Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 11:35:28 +0200 Subject: [PATCH 06/15] Fix phpcs --- .../src/Authentication/ClientCredentials.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/ClientCredentials.php b/modules/ppcp-api-client/src/Authentication/ClientCredentials.php index 7ec4628a5..08d37bead 100644 --- a/modules/ppcp-api-client/src/Authentication/ClientCredentials.php +++ b/modules/ppcp-api-client/src/Authentication/ClientCredentials.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Authentication; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; +use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; /** * Class ClientCredentials @@ -28,14 +29,21 @@ class ClientCredentials { * * @param Settings $settings The settings. */ - public function __construct(Settings $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_id = $this->settings->has( 'client_id' ) ? $this->settings->get( 'client_id' ) : ''; $client_secret = $this->settings->has( 'client_secret' ) ? $this->settings->get( 'client_secret' ) : ''; - return 'Basic ' . base64_encode($client_id . ':' . $client_secret); + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode + return 'Basic ' . base64_encode( $client_id . ':' . $client_secret ); } } From 22c2b5afd4703ae02f602dcf99d18b72cacf61fb Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 12:04:58 +0200 Subject: [PATCH 07/15] Delete transients stored while user is logged in --- modules/ppcp-api-client/services.php | 7 +++++-- modules/ppcp-api-client/src/ApiModule.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index dd2c786a1..7f62da5f6 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -1662,12 +1662,15 @@ return array( $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( 'woocommerce.logger.woocommerce' ), $container->get( 'api.client-credentials' ), - new Cache( 'ppcp-client-credentials-cache' ) + $container->get( 'api.client-credentials-cache' ) ); }, 'api.sdk-client-token' => static function( ContainerInterface $container ): SdkClientToken { @@ -1675,7 +1678,7 @@ return array( $container->get( 'api.host' ), $container->get( 'woocommerce.logger.woocommerce' ), $container->get( 'api.client-credentials' ), - new Cache( 'ppcp-client-credentials-cache' ) + $container->get( 'api.client-credentials-cache' ) ); }, ); diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php index 42bc1f117..5c3ec56bd 100644 --- a/modules/ppcp-api-client/src/ApiModule.php +++ b/modules/ppcp-api-client/src/ApiModule.php @@ -10,6 +10,9 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient; use WC_Order; +use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken; +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 +97,21 @@ class ApiModule implements ModuleInterface { 10, 2 ); + + add_action( + 'wp_logout', + function() 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 ) ) { + $client_credentials_cache->delete( UserIdToken::CACHE_KEY ); + } + if ( $client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) { + $client_credentials_cache->delete( SdkClientToken::CACHE_KEY ); + } + } + ); } /** From a1413782e98dd78ccd2cecb66d056d7f3917a311 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 12:32:40 +0200 Subject: [PATCH 08/15] Invalidate cache when switching user --- .../src/Authentication/SdkClientToken.php | 15 +++++++++++++-- .../src/Authentication/UserIdToken.php | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index 2e1f3a7ca..85a447176 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -83,7 +83,12 @@ class SdkClientToken { */ public function sdk_client_token( string $target_customer_id = '' ): string { if ( $this->cache->has( self::CACHE_KEY ) ) { - return $this->cache->get( self::CACHE_KEY ); + $user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0; + $access_token = $this->cache->get( self::CACHE_KEY )['access_token'] ?? ''; + + if ( $user_id === get_current_user_id() && $access_token ) { + return $access_token; + } } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized @@ -121,7 +126,13 @@ class SdkClientToken { } $access_token = $json->access_token; - $this->cache->set( self::CACHE_KEY, $access_token ); + + $data = array( + 'access_token' => $access_token, + 'user_id' => get_current_user_id(), + ); + + $this->cache->set( self::CACHE_KEY, $data ); return $access_token; } diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 2d18d0507..c60801dd5 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -83,7 +83,12 @@ class UserIdToken { */ public function id_token( string $target_customer_id = '' ): string { if ( $this->cache->has( self::CACHE_KEY ) ) { - return $this->cache->get( self::CACHE_KEY ); + $user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0; + $id_token = $this->cache->get( self::CACHE_KEY )['id_token'] ?? ''; + + if ( $user_id === get_current_user_id() && $id_token ) { + return $id_token; + } } $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; @@ -116,7 +121,13 @@ class UserIdToken { } $id_token = $json->id_token; - $this->cache->set( self::CACHE_KEY, $id_token ); + + $data = array( + 'id_token' => $id_token, + 'user_id' => get_current_user_id(), + ); + + $this->cache->set( self::CACHE_KEY, $data ); return $id_token; } From b42e9436ae88ed159b645b65f07cfdd4cccc8320 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Tue, 6 Aug 2024 15:30:50 +0200 Subject: [PATCH 09/15] Add id token cache key for each user --- modules/ppcp-api-client/src/ApiModule.php | 10 ++----- .../src/Authentication/SdkClientToken.php | 30 +++---------------- .../src/Authentication/UserIdToken.php | 16 ++-------- modules/ppcp-axo/src/AxoModule.php | 10 +------ .../js/modules/Helper/ScriptLoading.js | 5 +++- .../src/Endpoint/LoginSellerEndpoint.php | 3 -- .../src/Settings/SettingsListener.php | 3 -- 7 files changed, 15 insertions(+), 62 deletions(-) diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php index 5c3ec56bd..2d63bd80f 100644 --- a/modules/ppcp-api-client/src/ApiModule.php +++ b/modules/ppcp-api-client/src/ApiModule.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient; use WC_Order; -use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken; use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry; @@ -100,15 +99,12 @@ class ApiModule implements ModuleInterface { add_action( 'wp_logout', - function() use ( $c ) { + 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 ) ) { - $client_credentials_cache->delete( UserIdToken::CACHE_KEY ); - } - if ( $client_credentials_cache->has( SdkClientToken::CACHE_KEY ) ) { - $client_credentials_cache->delete( SdkClientToken::CACHE_KEY ); + 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/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index 85a447176..6236869e2 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -72,23 +72,16 @@ class SdkClientToken { } /** - * 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 { + public function sdk_client_token(): string { if ( $this->cache->has( self::CACHE_KEY ) ) { - $user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0; - $access_token = $this->cache->get( self::CACHE_KEY )['access_token'] ?? ''; - - if ( $user_id === get_current_user_id() && $access_token ) { - return $access_token; - } + $this->cache->get( self::CACHE_KEY ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized @@ -97,15 +90,6 @@ 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( @@ -126,13 +110,7 @@ class SdkClientToken { } $access_token = $json->access_token; - - $data = array( - 'access_token' => $access_token, - 'user_id' => get_current_user_id(), - ); - - $this->cache->set( self::CACHE_KEY, $data ); + $this->cache->set( self::CACHE_KEY, $access_token ); return $access_token; } diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index c60801dd5..3c215a11f 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -82,13 +82,8 @@ class UserIdToken { * @throws RuntimeException If something unexpected happens. */ public function id_token( string $target_customer_id = '' ): string { - if ( $this->cache->has( self::CACHE_KEY ) ) { - $user_id = $this->cache->get( self::CACHE_KEY )['user_id'] ?? 0; - $id_token = $this->cache->get( self::CACHE_KEY )['id_token'] ?? ''; - - if ( $user_id === get_current_user_id() && $id_token ) { - return $id_token; - } + 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'; @@ -122,12 +117,7 @@ class UserIdToken { $id_token = $json->id_token; - $data = array( - 'id_token' => $id_token, - 'user_id' => get_current_user_id(), - ); - - $this->cache->set( self::CACHE_KEY, $data ); + $this->cache->set( self::CACHE_KEY . '-' . (string) get_current_user_id(), $id_token ); return $id_token; } diff --git a/modules/ppcp-axo/src/AxoModule.php b/modules/ppcp-axo/src/AxoModule.php index 3505ff555..c0fc34b24 100644 --- a/modules/ppcp-axo/src/AxoModule.php +++ b/modules/ppcp-axo/src/AxoModule.php @@ -280,15 +280,7 @@ class AxoModule implements ModuleInterface { array $localized_script_data ): array { try { - $target_customer_id = ''; - if ( is_user_logged_in() ) { - $target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true ); - if ( ! $target_customer_id ) { - $target_customer_id = get_user_meta( get_current_user_id(), 'ppcp_customer_id', true ); - } - } - - $sdk_client_token = $api->sdk_client_token( $target_customer_id ); + $sdk_client_token = $api->sdk_client_token(); $localized_script_data['axo'] = array( 'sdk_client_token' => $sdk_client_token, ); diff --git a/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js b/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js index 0aa70f793..588e14cd6 100644 --- a/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js +++ b/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js @@ -71,7 +71,10 @@ export const loadPaypalScript = ( config, onLoaded, onError = null ) => { } // Load PayPal script for special case with data-client-token - if ( config.data_client_id?.set_attribute ) { + if ( + config.data_client_id?.set_attribute && + config.vault_v3_enabled !== '1' + ) { dataClientIdAttributeHandler( scriptOptions, config.data_client_id, diff --git a/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php b/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php index 2a30ad6b1..e1d23234f 100644 --- a/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php +++ b/modules/ppcp-onboarding/src/Endpoint/LoginSellerEndpoint.php @@ -187,9 +187,6 @@ 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 ); } diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php index e7e39a9e6..6c01830d6 100644 --- a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -502,9 +502,6 @@ 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 ); } From 8411ef2106890a0ad6c282d0513244509e08dae7 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 7 Aug 2024 09:51:12 +0200 Subject: [PATCH 10/15] Fix not returning the cached token --- modules/ppcp-api-client/src/Authentication/SdkClientToken.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index 6236869e2..10b1eb0eb 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -81,7 +81,7 @@ class SdkClientToken { */ public function sdk_client_token(): string { if ( $this->cache->has( self::CACHE_KEY ) ) { - $this->cache->get( self::CACHE_KEY ); + return $this->cache->get( self::CACHE_KEY ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized From 4f47b382c571ead74c5f5cf768e37ecae79389ea Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 7 Aug 2024 10:36:05 +0200 Subject: [PATCH 11/15] Add token expiration --- .../ppcp-api-client/src/Authentication/SdkClientToken.php | 4 +++- modules/ppcp-api-client/src/Authentication/UserIdToken.php | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index 10b1eb0eb..5031f1291 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -110,7 +110,9 @@ class SdkClientToken { } $access_token = $json->access_token; - $this->cache->set( self::CACHE_KEY, $access_token ); + $expires_in = (int) $json->expires_in; + + $this->cache->set( self::CACHE_KEY, $access_token, $expires_in ); return $access_token; } diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php index 3c215a11f..9487a7238 100644 --- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php +++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php @@ -115,9 +115,10 @@ class UserIdToken { throw new PayPalApiException( $json, $status_code ); } - $id_token = $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 ); + $this->cache->set( self::CACHE_KEY . '-' . (string) get_current_user_id(), $id_token, $expires_in ); return $id_token; } From 7778cfafed613b6286d08e38ed9eb9b2ec1f48c0 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 7 Aug 2024 12:45:44 +0200 Subject: [PATCH 12/15] Revert #2283 --- modules/ppcp-api-client/src/Authentication/SdkClientToken.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index 5031f1291..b85490acf 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -80,10 +80,6 @@ class SdkClientToken { * @throws RuntimeException If something unexpected happens. */ 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'] ?? '' ); $domain = preg_replace( '/^www\./', '', $domain ); From 939991c42f794ff26813c6d811e412fe355793b4 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 7 Aug 2024 12:47:11 +0200 Subject: [PATCH 13/15] Revert #2283 --- modules/ppcp-api-client/src/Authentication/SdkClientToken.php | 4 ++++ .../src/SavePaymentMethodsModule.php | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php index b85490acf..5031f1291 100644 --- a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php +++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php @@ -80,6 +80,10 @@ class SdkClientToken { * @throws RuntimeException If something unexpected happens. */ 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'] ?? '' ); $domain = preg_replace( '/^www\./', '', $domain ); diff --git a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php index db7335cf5..b667fb10b 100644 --- a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php +++ b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php @@ -84,10 +84,6 @@ class SavePaymentMethodsModule implements ModuleInterface { add_filter( 'woocommerce_paypal_payments_localized_script_data', function( array $localized_script_data ) use ( $c ) { - if ( ! is_user_logged_in() ) { - return $localized_script_data; - } - $api = $c->get( 'api.user-id-token' ); assert( $api instanceof UserIdToken ); From c0bd7a5c46be17e589f90a2cc73a272c9feb091e Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 7 Aug 2024 14:24:44 +0200 Subject: [PATCH 14/15] Release 2.8.3-rc1 --- changelog.txt | 17 +++++++++++++++++ package.json | 2 +- readme.txt | 19 ++++++++++++++++++- woocommerce-paypal-payments.php | 4 ++-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/changelog.txt b/changelog.txt index aaf167948..e28d7a258 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,22 @@ *** Changelog *** += 2.8.3 - xxxx-xx-xx = +* Fix - Google Pay: Prevent field validation from being triggered on checkout page load #2474 +* Fix - Do not add tax info into order meta during order creation #2471 +* Fix - PayPal declares subscription support when for Subscription mode is set Disable PayPal for subscription #2425 +* Fix - PayPal js files loaded on non PayPal pages #2411 +* Fix - Google Pay: Fix the incorrect popup triggering #2414 +* Fix - Add tax configurator when programmatically creating WC orders #2431 +* Fix - Shipping callback compatibility with WC Name Your Price plugin #2402 +* Fix - Uncaught Error: Cannot use object of type ...\Settings as array in .../AbstractPaymentMethodType.php (3253) #2334 +* Fix - Prevent displaying smart button multiple times on variable product page #2420 +* Fix - Prevent enabling Standard Card Button when ACDC is enabled #2404 +* Fix - Use client credentials for user tokens #2491 +* Fix - Apple Pay: Fix the shipping callback #2492 +* Enhancement - Separate Google Pay button for Classic Checkout #2430 +* Enhancement - Add Apple Pay and Google Pay support for China, simplify country-currency matrix #2468 +* Enhancement - Add AMEX support for Advanced Card Processing in China #2469 + = 2.8.2 - 2024-07-22 = * Fix - Sold individually checkbox automatically disabled after adding product to the cart more than once #2415 * Fix - All products "Sold individually" when PayPal Subscriptions selected as Subscriptions Mode #2400 diff --git a/package.json b/package.json index 61c625ca9..72aad99db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "woocommerce-paypal-payments", - "version": "2.8.2", + "version": "2.8.3", "description": "WooCommerce PayPal Payments", "repository": "https://github.com/woocommerce/woocommerce-paypal-payments", "license": "GPL-2.0", diff --git a/readme.txt b/readme.txt index e4404042e..fc1976e15 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: woocommerce, paypal, payments, ecommerce, checkout, cart, pay later, apple Requires at least: 5.3 Tested up to: 6.6 Requires PHP: 7.2 -Stable tag: 2.8.2 +Stable tag: 2.8.3 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -179,6 +179,23 @@ If you encounter issues with the PayPal buttons not appearing after an update, p == Changelog == += 2.8.3 - xxxx-xx-xx = +* Fix - Google Pay: Prevent field validation from being triggered on checkout page load #2474 +* Fix - Do not add tax info into order meta during order creation #2471 +* Fix - PayPal declares subscription support when for Subscription mode is set Disable PayPal for subscription #2425 +* Fix - PayPal js files loaded on non PayPal pages #2411 +* Fix - Google Pay: Fix the incorrect popup triggering #2414 +* Fix - Add tax configurator when programmatically creating WC orders #2431 +* Fix - Shipping callback compatibility with WC Name Your Price plugin #2402 +* Fix - Uncaught Error: Cannot use object of type ...\Settings as array in .../AbstractPaymentMethodType.php (3253) #2334 +* Fix - Prevent displaying smart button multiple times on variable product page #2420 +* Fix - Prevent enabling Standard Card Button when ACDC is enabled #2404 +* Fix - Use client credentials for user tokens #2491 +* Fix - Apple Pay: Fix the shipping callback #2492 +* Enhancement - Separate Google Pay button for Classic Checkout #2430 +* Enhancement - Add Apple Pay and Google Pay support for China, simplify country-currency matrix #2468 +* Enhancement - Add AMEX support for Advanced Card Processing in China #2469 + = 2.8.2 - 2024-07-22 = * Fix - Sold individually checkbox automatically disabled after adding product to the cart more than once #2415 * Fix - All products "Sold individually" when PayPal Subscriptions selected as Subscriptions Mode #2400 diff --git a/woocommerce-paypal-payments.php b/woocommerce-paypal-payments.php index 2cdf4f8ed..fcf66ae12 100644 --- a/woocommerce-paypal-payments.php +++ b/woocommerce-paypal-payments.php @@ -3,7 +3,7 @@ * Plugin Name: WooCommerce PayPal Payments * Plugin URI: https://woocommerce.com/products/woocommerce-paypal-payments/ * Description: PayPal's latest complete payments processing solution. Accept PayPal, Pay Later, credit/debit cards, alternative digital wallets local payment types and bank accounts. Turn on only PayPal options or process a full suite of payment methods. Enable global transaction with extensive currency and country coverage. - * Version: 2.8.2 + * Version: 2.8.3 * Author: WooCommerce * Author URI: https://woocommerce.com/ * License: GPL-2.0 @@ -26,7 +26,7 @@ define( 'PAYPAL_API_URL', 'https://api-m.paypal.com' ); define( 'PAYPAL_URL', 'https://www.paypal.com' ); define( 'PAYPAL_SANDBOX_API_URL', 'https://api-m.sandbox.paypal.com' ); define( 'PAYPAL_SANDBOX_URL', 'https://www.sandbox.paypal.com' ); -define( 'PAYPAL_INTEGRATION_DATE', '2024-07-17' ); +define( 'PAYPAL_INTEGRATION_DATE', '2024-08-07' ); define( 'PPCP_PAYPAL_BN_CODE', 'Woo_PPCP' ); ! defined( 'CONNECT_WOO_CLIENT_ID' ) && define( 'CONNECT_WOO_CLIENT_ID', 'AcCAsWta_JTL__OfpjspNyH7c1GGHH332fLwonA5CwX4Y10mhybRZmHLA0GdRbwKwjQIhpDQy0pluX_P' ); From e41470b159c0879586d521cdbdf8c021c8d274b9 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Wed, 7 Aug 2024 15:01:27 +0200 Subject: [PATCH 15/15] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Google=20Pay=20butto?= =?UTF-8?q?n=20on=20non-checkout=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/GooglepayButton.js | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index 4363c67be..ac477404b 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -292,24 +292,33 @@ class GooglepayButton { onButtonClick() { this.log( 'onButtonClick', this.context ); - this.contextHandler.validateForm().then( - () => { - window.ppcpFundingSource = 'googlepay'; + const initiatePaymentRequest = () => { + window.ppcpFundingSource = 'googlepay'; - const paymentDataRequest = this.paymentDataRequest(); + const paymentDataRequest = this.paymentDataRequest(); - this.log( - 'onButtonClick: paymentDataRequest', - paymentDataRequest, - this.context - ); + this.log( + 'onButtonClick: paymentDataRequest', + paymentDataRequest, + this.context + ); - this.paymentsClient.loadPaymentData( paymentDataRequest ); - }, - () => { - console.error( '[GooglePayButton] Form validation failed.' ); - } - ); + this.paymentsClient.loadPaymentData( paymentDataRequest ); + }; + + if ( 'function' === typeof this.contextHandler.validateForm ) { + // During regular checkout, validate the checkout form before initiating the payment. + this.contextHandler + .validateForm() + .then( initiatePaymentRequest, () => { + console.error( + '[GooglePayButton] Form validation failed.' + ); + } ); + } else { + // This is the flow on product page, cart, and other non-checkout pages. + initiatePaymentRequest(); + } } paymentDataRequest() {