Skip to content

Commit a0c1387

Browse files
authored
Allow saved credit cards with a source ID (src_) to be used/displayed with the new checkout experience (#3513)
* Introduce new function to determine valid payment method IDs * don't remove valid source token (i.e those with card payment method type * Create new saved tokens for sources, but only if they belong to card method type * add changelog entries * Use strpos instead of a function that requires PHP 8+ * Fix typo in changelog entry
1 parent 88b2020 commit a0c1387

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
3333
* Tweak - Add order lock for redirect payments.
3434
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
35+
* Fix - Allow legacy `src_` payment methods linked to a saved credit card to be displayed on the checkout and My Account pages when the new checkout experience is enabled.
3536

3637
= 8.7.0 - 2024-09-16 =
3738
* Add - Introduces a new promotional surface to encourage merchants with the legacy checkout experience and APMs enabled to use the new checkout experience.

includes/class-wc-stripe-payment-tokens.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ public function woocommerce_get_customer_upe_payment_tokens( $tokens, $user_id,
270270

271271
// Remove the following deprecated tokens:
272272
// - APM tokens from before Split PE was in place.
273-
// - Tokens using the sources API. Payments using these will fail with the PaymentMethods API.
273+
// - Non-credit card tokens using the sources API. Payments using these will fail with the PaymentMethods API.
274274
if (
275275
( 'stripe' === $token->get_gateway_id() && WC_Stripe_Payment_Methods::SEPA === $token->get_type() ) ||
276-
str_starts_with( $token->get_token(), 'src_' )
276+
! $this->is_valid_payment_method_id( $token->get_token(), $this->get_payment_method_type_from_token( $token ) )
277277
) {
278278
$deprecated_tokens[ $token->get_token() ] = $token;
279279
continue;
@@ -316,11 +316,11 @@ public function woocommerce_get_customer_upe_payment_tokens( $tokens, $user_id,
316316

317317
// Create a new token when:
318318
// - The payment method doesn't have an associated token in WooCommerce.
319-
// - The payment method is not a source.
319+
// - The payment method is a valid PaymentMethodID (i.e. only support IDs starting with "src_" when using the card payment method type.
320320
// - The payment method belongs to the gateway ID being retrieved or the gateway ID is empty (meaning we're looking for all payment methods).
321321
if (
322322
! isset( $stored_tokens[ $payment_method->id ] ) &&
323-
! str_starts_with( $payment_method->id, 'src_' ) &&
323+
$this->is_valid_payment_method_id( $payment_method->id, $payment_method_type ) &&
324324
( $this->is_valid_payment_method_type_for_gateway( $payment_method_type, $gateway_id ) || empty( $gateway_id ) )
325325
) {
326326
$token = $this->add_token_to_user( $payment_method, $customer );
@@ -602,6 +602,24 @@ public static function update_token_from_method_details( $user_id, $payment_meth
602602
}
603603
}
604604

605+
/**
606+
* Returns true if the payment method ID is valid for the given payment method type.
607+
*
608+
* Payment method IDs beginning with 'src_' are only valid for card payment methods.
609+
*
610+
* @param string $payment_method_id The payment method ID (e.g. 'pm_123' or 'src_123').
611+
* @param string $payment_method_type The payment method type.
612+
*
613+
* @return bool
614+
*/
615+
public function is_valid_payment_method_id( $payment_method_id, $payment_method_type = '' ) {
616+
if ( 0 === strpos( $payment_method_id, 'pm_' ) ) {
617+
return true;
618+
}
619+
620+
return 0 === strpos( $payment_method_id, 'src_' ) && WC_Stripe_Payment_Methods::CARD === $payment_method_type;
621+
}
622+
605623
/**
606624
* Controls the output for SEPA on the my account page.
607625
*

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
160160
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
161161
* Tweak - Add order lock for redirect payments.
162162
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
163+
* Fix - Allow legacy `src_` payment methods linked to a saved credit card to be displayed on the checkout and My Account pages when the new checkout experience is enabled.
163164

164165
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Class WC_Stripe_Payment_Tokens tests.
4+
*/
5+
class WC_Stripe_Payment_Tokens_Test extends WP_UnitTestCase {
6+
7+
/**
8+
* WC_Stripe_Payment_Tokens instance.
9+
*
10+
* @var WC_Stripe_Payment_Tokens
11+
*/
12+
private $stripe_payment_tokens;
13+
14+
public function set_up() {
15+
parent::set_up();
16+
$this->stripe_payment_tokens = new WC_Stripe_Payment_Tokens();
17+
}
18+
19+
public function test_is_valid_payment_method_id() {
20+
$this->assertTrue( $this->stripe_payment_tokens->is_valid_payment_method_id( 'pm_1234567890' ) );
21+
$this->assertTrue( $this->stripe_payment_tokens->is_valid_payment_method_id( 'pm_1234567890', 'card' ) );
22+
$this->assertTrue( $this->stripe_payment_tokens->is_valid_payment_method_id( 'pm_1234567890', 'sepa' ) );
23+
24+
// Test with source id (only card payment method type is valid).
25+
$this->assertTrue( $this->stripe_payment_tokens->is_valid_payment_method_id( 'src_1234567890', 'card' ) );
26+
$this->assertFalse( $this->stripe_payment_tokens->is_valid_payment_method_id( 'src_1234567890', 'sepa' ) );
27+
$this->assertFalse( $this->stripe_payment_tokens->is_valid_payment_method_id( 'src_1234567890', 'giropay' ) );
28+
}
29+
}

0 commit comments

Comments
 (0)