Merge pull request #2820 from woocommerce/PCP-3860-only-last-payment-token-is-available-because-a-new-customer-is-returned-each-time

Only last payment token is available because a new customer is returned each time (PCP-3860)
This commit is contained in:
Emili Castells 2024-11-21 17:07:28 +01:00 committed by GitHub
commit 44b885455f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 4 deletions

View file

@ -61,19 +61,24 @@ class PaymentMethodTokensEndpoint {
* Creates a setup token. * Creates a setup token.
* *
* @param PaymentSource $payment_source The payment source. * @param PaymentSource $payment_source The payment source.
* @param string $customer_id PayPal customer ID.
* *
* @return stdClass * @return stdClass
* *
* @throws RuntimeException When something when wrong with the request. * @throws RuntimeException When something when wrong with the request.
* @throws PayPalApiException When something when wrong setting up the token. * @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( $data = array(
'payment_source' => array( 'payment_source' => array(
$payment_source->name() => $payment_source->properties(), $payment_source->name() => $payment_source->properties(),
), ),
); );
if ( $customer_id ) {
$data['customer']['id'] = $customer_id;
}
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens'; $url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens';
@ -109,19 +114,24 @@ class PaymentMethodTokensEndpoint {
* Creates a payment token for the given payment source. * Creates a payment token for the given payment source.
* *
* @param PaymentSource $payment_source The payment source. * @param PaymentSource $payment_source The payment source.
* @param string $customer_id PayPal customer ID.
* *
* @return stdClass * @return stdClass
* *
* @throws RuntimeException When something when wrong with the request. * @throws RuntimeException When something when wrong with the request.
* @throws PayPalApiException When something when wrong setting up the token. * @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( $data = array(
'payment_source' => array( 'payment_source' => array(
$payment_source->name() => $payment_source->properties(), $payment_source->name() => $payment_source->properties(),
), ),
); );
if ( $customer_id ) {
$data['customer']['id'] = $customer_id;
}
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens'; $url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens';

View file

@ -94,7 +94,9 @@ class CreatePaymentToken implements EndpointInterface {
) )
); );
$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 ) ) { if ( is_user_logged_in() && isset( $result->customer->id ) ) {
$current_user_id = get_current_user_id(); $current_user_id = get_current_user_id();

View file

@ -103,7 +103,9 @@ class CreateSetupToken implements EndpointInterface {
); );
} }
$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 ); wp_send_json_success( $result );
return true; return true;

View file

@ -166,6 +166,10 @@ class VaultingModule implements ServiceModule, ExtendingModule, ExecutableModule
add_action( add_action(
'wp', 'wp',
function() use ( $container ) { function() use ( $container ) {
if ( $container->get( 'vaulting.vault-v3-enabled' ) ) {
return;
}
global $wp; global $wp;
if ( isset( $wp->query_vars['delete-payment-method'] ) ) { if ( isset( $wp->query_vars['delete-payment-method'] ) ) {