diff --git a/changelog.txt b/changelog.txt index f881db8cad..c24953f49e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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 * Fix - Fix fatal error when trying to allow the `display` CSS property using the `safe_style_css` filter * Fix - Remove `redirect_url` parameter from Express Checkout payment flow * Fix - Adjust UI spacing of help text on express checkout theme settings page diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 953550b79a..7849ef2175 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -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. * @@ -164,6 +171,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; + } + $retryable_types = [ 'invalid_request_error', 'idempotency_error', diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index c255745740..e8caee5e03 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -2467,6 +2467,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; } diff --git a/readme.txt b/readme.txt index 049bfbcbdf..e4d1e115ce 100644 --- a/readme.txt +++ b/readme.txt @@ -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 * Fix - Fix fatal error when trying to allow the `display` CSS property using the `safe_style_css` filter * Fix - Remove `redirect_url` parameter from Express Checkout payment flow * Fix - Adjust UI spacing of help text on express checkout theme settings page