From 43b9452d54d981f13aef89722d206195d0049579 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Fri, 12 Apr 2024 12:19:36 +0100
Subject: [PATCH] Fix lint
---
modules/ppcp-api-client/services.php | 8 ++
.../src/Authentication/SdkClientToken.php | 106 +++++++++++++++++
.../src/Authentication/UserIdToken.php | 4 +-
modules/ppcp-axo/src/AxoModule.php | 111 ++++++++++++++----
.../js/modules/Helper/ScriptLoading.js | 5 +
.../src/Settings/SettingsRenderer.php | 2 +-
.../Authentication/PayPalBearerTest.php | 8 +-
7 files changed, 211 insertions(+), 33 deletions(-)
create mode 100644 modules/ppcp-api-client/src/Authentication/SdkClientToken.php
diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php
index 225530ec2..f91e24119 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\SdkClientToken;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokensEndpoint;
@@ -1633,4 +1634,11 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
+ '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' )
+ );
+ },
);
diff --git a/modules/ppcp-api-client/src/Authentication/SdkClientToken.php b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php
new file mode 100644
index 000000000..1f84171ab
--- /dev/null
+++ b/modules/ppcp-api-client/src/Authentication/SdkClientToken.php
@@ -0,0 +1,106 @@
+host = $host;
+ $this->bearer = $bearer;
+ $this->logger = $logger;
+ }
+
+ /**
+ * Returns `sdk_client_token` which uniquely identifies the payer.
+ *
+ * @param string $target_customer_id Vaulted customer id.
+ *
+ * @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();
+
+ $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=client_token&intent=sdk_init';
+ 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(),
+ 'Content-Type' => 'application/x-www-form-urlencoded',
+ ),
+ );
+
+ $response = $this->request( $url, $args );
+ if ( $response instanceof WP_Error ) {
+ throw new RuntimeException( $response->get_error_message() );
+ }
+
+ $json = json_decode( $response['body'] );
+ $status_code = (int) wp_remote_retrieve_response_code( $response );
+ if ( 200 !== $status_code ) {
+ throw new PayPalApiException( $json, $status_code );
+ }
+
+ return $json->access_token;
+ }
+}
diff --git a/modules/ppcp-api-client/src/Authentication/UserIdToken.php b/modules/ppcp-api-client/src/Authentication/UserIdToken.php
index 2ac1256d2..2c17e7787 100644
--- a/modules/ppcp-api-client/src/Authentication/UserIdToken.php
+++ b/modules/ppcp-api-client/src/Authentication/UserIdToken.php
@@ -72,7 +72,7 @@ class UserIdToken {
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=client_token&intent=sdk_init';
+ $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
if ( $target_customer_id ) {
$url = add_query_arg(
array(
@@ -101,6 +101,6 @@ class UserIdToken {
throw new PayPalApiException( $json, $status_code );
}
- return $json->access_token;
+ return $json->id_token;
}
}
diff --git a/modules/ppcp-axo/src/AxoModule.php b/modules/ppcp-axo/src/AxoModule.php
index c59424c6b..1bf49d7e2 100644
--- a/modules/ppcp-axo/src/AxoModule.php
+++ b/modules/ppcp-axo/src/AxoModule.php
@@ -9,6 +9,11 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Axo;
+use Psr\Log\LoggerInterface;
+use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken;
+use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
+use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
+use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Axo\Assets\AxoManager;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
@@ -35,6 +40,8 @@ class AxoModule implements ModuleInterface {
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
+ $module = $this;
+
add_filter(
'woocommerce_payment_gateways',
/**
@@ -71,34 +78,9 @@ class AxoModule implements ModuleInterface {
9
);
- add_filter(
- 'ppcp_onboarding_dcc_table_rows',
- /**
- * Param types removed to avoid third-party issues.
- *
- * @psalm-suppress MissingClosureParamType
- */
- function ( $rows, $renderer ): array {
- if ( ! is_array( $rows ) ) {
- return $rows;
- }
-
- if ( $renderer instanceof OnboardingOptionsRenderer ) {
- $rows[] = $renderer->render_table_row(
- __( 'Fastlane by PayPal', 'woocommerce-paypal-payments' ),
- __( 'Yes', 'woocommerce-paypal-payments' ),
- __( 'Help accelerate guest checkout with PayPal\'s autofill solution.', 'woocommerce-paypal-payments' )
- );
- }
- return $rows;
- },
- 10,
- 2
- );
-
add_action(
'init',
- static function () use ( $c ) {
+ static function () use ( $c, $module ) {
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'axo.eligible' ) ) {
@@ -150,12 +132,89 @@ class AxoModule implements ModuleInterface {
}
);
+ add_filter(
+ 'woocommerce_paypal_payments_localized_script_data',
+ function( array $localized_script_data ) use ( $c, $module ) {
+ $api = $c->get( 'api.sdk-client-token' );
+ assert( $api instanceof SdkClientToken );
+
+ $logger = $c->get( 'woocommerce.logger.woocommerce' );
+ assert( $logger instanceof LoggerInterface );
+
+ return $module->add_sdk_client_token_to_script_data( $api, $logger, $localized_script_data );
+ }
+ );
+
+ add_filter(
+ 'ppcp_onboarding_dcc_table_rows',
+ /**
+ * Param types removed to avoid third-party issues.
+ *
+ * @psalm-suppress MissingClosureParamType
+ */
+ function ( $rows, $renderer ): array {
+ if ( ! is_array( $rows ) ) {
+ return $rows;
+ }
+
+ if ( $renderer instanceof OnboardingOptionsRenderer ) {
+ $rows[] = $renderer->render_table_row(
+ __( 'Fastlane by PayPal', 'woocommerce-paypal-payments' ),
+ __( 'Yes', 'woocommerce-paypal-payments' ),
+ __( 'Help accelerate guest checkout with PayPal\'s autofill solution.', 'woocommerce-paypal-payments' )
+ );
+ }
+ return $rows;
+ },
+ 10,
+ 2
+ );
+
},
1
);
}
+ /**
+ * Adds id token to localized script data.
+ *
+ * @param SdkClientToken $api User id token api.
+ * @param LoggerInterface $logger The logger.
+ * @param array $localized_script_data The localized script data.
+ * @return array
+ */
+ private function add_sdk_client_token_to_script_data(
+ SdkClientToken $api,
+ LoggerInterface $logger,
+ 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 );
+ $localized_script_data['axo'] = array(
+ 'sdk_client_token' => $sdk_client_token,
+ );
+
+ } catch ( RuntimeException $exception ) {
+ $error = $exception->getMessage();
+ if ( is_a( $exception, PayPalApiException::class ) ) {
+ $error = $exception->get_details( $error );
+ }
+
+ $logger->error( $error );
+ }
+
+ return $localized_script_data;
+ }
+
/**
* Returns the key for the module.
*
diff --git a/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js b/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js
index 2c6b6341d..073be837a 100644
--- a/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js
+++ b/modules/ppcp-button/resources/js/modules/Helper/ScriptLoading.js
@@ -72,6 +72,11 @@ export const loadPaypalScript = (config, onLoaded, onError = null) => {
scriptOptions['data-user-id-token'] = userIdToken;
}
+ const sdkClientToken = config?.axo?.sdk_client_token;
+ if(sdkClientToken) {
+ scriptOptions['data-sdk-client-token'] = sdkClientToken;
+ }
+
scriptOptions['data-client-metadata-id'] = 'ppcp-cm-id';
// Load PayPal script
diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsRenderer.php b/modules/ppcp-wc-gateway/src/Settings/SettingsRenderer.php
index ce8ec39ea..12dc70b4b 100644
--- a/modules/ppcp-wc-gateway/src/Settings/SettingsRenderer.php
+++ b/modules/ppcp-wc-gateway/src/Settings/SettingsRenderer.php
@@ -385,7 +385,7 @@ $data_rows_html
?>
-
+ |
|