From d3cc1ed19e77453bb166377504fecdf9ad83e53f Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Thu, 21 Nov 2024 11:56:26 +0100 Subject: [PATCH] Add `customer_id` when saving payment token --- .../src/Endpoint/PaymentMethodTokensEndpoint.php | 14 ++++++++++++-- .../src/Endpoint/CreatePaymentToken.php | 4 +++- .../src/Endpoint/CreateSetupToken.php | 4 +++- modules/ppcp-vaulting/src/VaultingModule.php | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/PaymentMethodTokensEndpoint.php b/modules/ppcp-api-client/src/Endpoint/PaymentMethodTokensEndpoint.php index 538ca224d..e2c2a91e8 100644 --- a/modules/ppcp-api-client/src/Endpoint/PaymentMethodTokensEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PaymentMethodTokensEndpoint.php @@ -61,19 +61,24 @@ public function __construct( string $host, Bearer $bearer, LoggerInterface $logg * Creates a setup token. * * @param PaymentSource $payment_source The payment source. + * @param string $customer_id PayPal customer ID. * * @return stdClass * * @throws RuntimeException When something when wrong with the request. * @throws PayPalApiException When something when wrong setting up the token. */ - public function setup_tokens( PaymentSource $payment_source ): stdClass { + public function setup_tokens( PaymentSource $payment_source, string $customer_id = '' ): stdClass { $data = array( 'payment_source' => array( $payment_source->name() => $payment_source->properties(), ), ); + if ( $customer_id ) { + $data['customer']['id'] = $customer_id; + } + $bearer = $this->bearer->bearer(); $url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens'; @@ -109,19 +114,24 @@ public function setup_tokens( PaymentSource $payment_source ): stdClass { * Creates a payment token for the given payment source. * * @param PaymentSource $payment_source The payment source. + * @param string $customer_id PayPal customer ID. * * @return stdClass * * @throws RuntimeException When something when wrong with the request. * @throws PayPalApiException When something when wrong setting up the token. */ - public function create_payment_token( PaymentSource $payment_source ): stdClass { + public function create_payment_token( PaymentSource $payment_source, string $customer_id = '' ): stdClass { $data = array( 'payment_source' => array( $payment_source->name() => $payment_source->properties(), ), ); + if ( $customer_id ) { + $data['customer']['id'] = $customer_id; + } + $bearer = $this->bearer->bearer(); $url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens'; diff --git a/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php b/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php index ae3d7dfa0..434a08925 100644 --- a/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php +++ b/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php @@ -94,7 +94,9 @@ public function handle_request(): bool { ) ); - $result = $this->payment_method_tokens_endpoint->create_payment_token( $payment_source ); + $customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true ); + + $result = $this->payment_method_tokens_endpoint->create_payment_token( $payment_source, $customer_id ); if ( is_user_logged_in() && isset( $result->customer->id ) ) { $current_user_id = get_current_user_id(); diff --git a/modules/ppcp-save-payment-methods/src/Endpoint/CreateSetupToken.php b/modules/ppcp-save-payment-methods/src/Endpoint/CreateSetupToken.php index 7eb3a5ace..6952feb43 100644 --- a/modules/ppcp-save-payment-methods/src/Endpoint/CreateSetupToken.php +++ b/modules/ppcp-save-payment-methods/src/Endpoint/CreateSetupToken.php @@ -103,7 +103,9 @@ public function handle_request(): bool { ); } - $result = $this->payment_method_tokens_endpoint->setup_tokens( $payment_source ); + $customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true ); + + $result = $this->payment_method_tokens_endpoint->setup_tokens( $payment_source, $customer_id ); wp_send_json_success( $result ); return true; diff --git a/modules/ppcp-vaulting/src/VaultingModule.php b/modules/ppcp-vaulting/src/VaultingModule.php index 041c61c35..2c4c32311 100644 --- a/modules/ppcp-vaulting/src/VaultingModule.php +++ b/modules/ppcp-vaulting/src/VaultingModule.php @@ -166,6 +166,10 @@ function( $item, $payment_token ) { add_action( 'wp', function() use ( $container ) { + if ( $container->get( 'vaulting.vault-v3-enabled' ) ) { + return; + } + global $wp; if ( isset( $wp->query_vars['delete-payment-method'] ) ) {