Skip to content
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 10.1.0 - xxxx-xx-xx =
* Fix - Improves the error message shown in checkout when a saved payment method is no longer valid
* Add - Add a new filter allowing third-party plugins to hook captcha solutions when creating and confirming setup intents
* Dev - Add track events when clicking the "Reconnect to Stripe" button (both in the settings page and the admin notice)
* Update - Removes unnecessary legacy checkout gateway instantiations and UPE disablement code
Expand Down
16 changes: 16 additions & 0 deletions includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ abstract class WC_Stripe_Payment_Gateway extends WC_Payment_Gateway_CC {
use WC_Stripe_Subscriptions_Trait;
use WC_Stripe_Pre_Orders_Trait;

/**
* Error string returned by Stripe when a PaymentMethod is detached.
*
* @var string
*/
protected const DETACHED_PAYMENT_METHOD_ERROR_STRING = 'The provided PaymentMethod was previously used with a PaymentIntent without Customer attachment';

/**
* The delay between retries.
*
Expand Down Expand Up @@ -163,6 +170,15 @@ public function is_retryable_error( $error ) {
return false;
}

// Don't retry if the error indicates that a PaymentMethod is detached.
if ( isset( $error->type )
&& 'invalid_request_error' == $error->type
&& isset( $error->message )
&& str_contains( $error->message, self::DETACHED_PAYMENT_METHOD_ERROR_STRING )
) {
return false;
}

return (
'invalid_request_error' === $error->type ||
'idempotency_error' === $error->type ||
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function init() {
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-feature-flags.php';
}

require_once WC_STRIPE_PLUGIN_PATH . '/includes/wc-stripe-functions.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-upe-compatibility.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-co-branded-cc-compatibility.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-exception.php';
Expand Down
14 changes: 14 additions & 0 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,20 @@ private function get_payment_intent_error_message( $payment_intent ) {
);
}

// This error indicates that the saved payment method is no longer valid.
// This can happen if the payment method was removed in Stripe dashboard, or if it expired.
// In this case, we want to show a specific message to the user.
if ( isset( $payment_intent->error->type )
&& 'invalid_request_error' === $payment_intent->error->type
&& isset( $payment_intent->error->message )
&& str_contains( $payment_intent->error->message, self::DETACHED_PAYMENT_METHOD_ERROR_STRING )
) {
return __(
'This saved payment method is no longer valid. It might be expired, removed, or broken. Please choose a different payment method.',
'woocommerce-gateway-stripe'
);
}

return $payment_intent->error->message;
}

Expand Down
22 changes: 22 additions & 0 deletions includes/wc-stripe-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* WooCommerce Stripe Functions
*
* General functions available on both the front-end and admin.
*
* @package WooCommerce_Stripe\Functions
* @version 10.1.0
*/

if ( ! function_exists( 'str_contains' ) ) {
/**
* Polyfill for str_contains function for PHP < 8.0
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for in the haystack.
* @return bool
*/
function str_contains( string $haystack, string $needle ) {
return '' !== $needle && mb_strpos( $haystack, $needle ) !== false;
}
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 10.1.0 - xxxx-xx-xx =
* Fix - Improves the error message shown in checkout when a saved payment method is no longer valid
* Add - Add a new filter allowing third-party plugins to hook captcha solutions when creating and confirming setup intents
* Dev - Add track events when clicking the "Reconnect to Stripe" button (both in the settings page and the admin notice)
* Update - Removes unnecessary legacy checkout gateway instantiations and UPE disablement code
Expand Down
Loading