Skip to content

Commit

Permalink
Fix payment method dettachment in live mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrosa committed Nov 4, 2024
1 parent 4596314 commit bb5d579
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions includes/class-wc-stripe-payment-tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class WC_Stripe_Payment_Tokens {
* The values are the related gateway ID we use for them in the extension.
*/
const UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD = [
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID,
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID,
WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID,
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID,
WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_Cash_App_Pay::STRIPE_ID => WC_Stripe_UPE_Payment_Gateway::ID . '_' . WC_Stripe_UPE_Payment_Method_Cash_App_Pay::STRIPE_ID,
];

Expand Down Expand Up @@ -412,9 +412,28 @@ public function woocommerce_payment_token_deleted( $token_id, $token ) {
$stripe_customer = new WC_Stripe_Customer( $token->get_user_id() );
try {
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
if ( in_array( $token->get_gateway_id(), self::UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
$stripe_customer->detach_payment_method( $token->get_token() );
// If it's not reusable payment method, we don't need to perform any additional checks.
if ( ! in_array( $token->get_gateway_id(), self::UPE_REUSABLE_GATEWAYS_BY_PAYMENT_METHOD, true ) ) {
return;
}

/**
* 1. Check if it's live mode.
* 2. Check if it's admin.
* 3. 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.
*/
$settings = WC_Stripe_Helper::get_stripe_settings();
if (
'no' === $settings['testmode'] &&
is_admin() &&
'production' !== wp_get_environment_type()
) {
return;
}

$stripe_customer->detach_payment_method( $token->get_token() );
} else {
if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
$stripe_customer->delete_source( $token->get_token() );
Expand Down

0 comments on commit bb5d579

Please sign in to comment.