Skip to content

Commit d6c5821

Browse files
authored
Fix payment method detachment in live mode (#3580)
* Fix payment method dettachment in live mode * Revert unnecessary changes * Adding changelog and readme entries
1 parent 5c19485 commit d6c5821

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 8.9.0 - xxxx-xx-xx =
4+
* Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled).
45
* Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element.
56
* Tweak - Makes the new Stripe Express Checkout Element enabled by default.
67
* Dev - Add multiple unit tests for the Stripe Express Checkout Element implementation (for both frontend and backend).

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,28 @@ public function woocommerce_payment_token_deleted( $token_id, $token ) {
412412
$stripe_customer = new WC_Stripe_Customer( $token->get_user_id() );
413413
try {
414414
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
415-
if ( in_array( $token->get_gateway_id(), self::UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
416-
$stripe_customer->detach_payment_method( $token->get_token() );
415+
// If it's not reusable payment method, we don't need to perform any additional checks.
416+
if ( ! in_array( $token->get_gateway_id(), self::UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
417+
return;
418+
}
419+
420+
/**
421+
* 1. Check if it's live mode.
422+
* 2. Check if it's admin.
423+
* 3. Check if it's not production environment.
424+
* When all conditions are met, we don't want to delete the payment method from Stripe.
425+
* This is to avoid detaching the payment method from the live stripe account on non production environments.
426+
*/
427+
$settings = WC_Stripe_Helper::get_stripe_settings();
428+
if (
429+
'no' === $settings['testmode'] &&
430+
is_admin() &&
431+
'production' !== wp_get_environment_type()
432+
) {
433+
return;
417434
}
435+
436+
$stripe_customer->detach_payment_method( $token->get_token() );
418437
} else {
419438
if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
420439
$stripe_customer->delete_source( $token->get_token() );

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 8.9.0 - xxxx-xx-xx =
114+
* Fix - Fix issues when detaching payment methods on staging sites (with the new checkout experience enabled).
114115
* Fix - Display a notice if taxes vary by customer's billing address when checking out using the Stripe Express Checkout Element.
115116
* Tweak - Makes the new Stripe Express Checkout Element enabled by default.
116117
* Dev - Add multiple unit tests for the Stripe Express Checkout Element implementation (for both frontend and backend).

0 commit comments

Comments
 (0)