diff --git a/changelog/fix-5384-prevent-pm-detach-on-prod-when-user-is-deleted-on-localhost b/changelog/fix-5384-prevent-pm-detach-on-prod-when-user-is-deleted-on-localhost new file mode 100644 index 00000000000..a39adc06bb1 --- /dev/null +++ b/changelog/fix-5384-prevent-pm-detach-on-prod-when-user-is-deleted-on-localhost @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Prevented detaching payment methods from live Stripe accounts when working in non-production environments. diff --git a/includes/class-wc-payments-token-service.php b/includes/class-wc-payments-token-service.php index 0eb810bce75..166ef6c9685 100644 --- a/includes/class-wc-payments-token-service.php +++ b/includes/class-wc-payments-token-service.php @@ -280,18 +280,34 @@ private function is_payment_method_enabled( $payment_method ) { * Delete token from Stripe. * * @param string $token_id Token ID. - * @param WC_Payment_Token $token Token object. + * @param WC_Payment_Token $token Token object. + * + * @throws Exception */ public function woocommerce_payment_token_deleted( $token_id, $token ) { - if ( in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) { - try { - $this->payments_api_client->detach_payment_method( $token->get_token() ); - // Clear cached payment methods. - $this->customer_service->clear_cached_payment_methods_for_user( $token->get_user_id() ); - } catch ( Exception $e ) { - Logger::log( 'Error detaching payment method:' . $e->getMessage() ); - } + // If it's not reusable payment method, we don't need to perform any additional checks. + if ( ! in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) { + return; + } + // First check if it's live mode. + // Second check if it's admin. + // Third check if it's not production environment. + // When all conditions are met, we don't want to delete the payment method from Stripe. + // This is to avoid detaching the payment method from the live stripe account on non production environments. + if ( + WC_Payments::mode()->is_live() && + is_admin() && + 'production' !== wp_get_environment_type() + ) { + return; + } + try { + $this->payments_api_client->detach_payment_method( $token->get_token() ); + // Clear cached payment methods. + $this->customer_service->clear_cached_payment_methods_for_user( $token->get_user_id() ); + } catch ( Exception $e ) { + Logger::log( 'Error detaching payment method:' . $e->getMessage() ); } }