From 67dc0d7090d27afe6dd376e90fee6ed7012bb5c2 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Tue, 9 Jun 2026 14:07:59 +0530 Subject: [PATCH] fix(billing): clear switch metadata when subscription cancel fails --- .../enterprise/billing/switch_currency_service.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/enterprise/app/services/enterprise/billing/switch_currency_service.rb b/enterprise/app/services/enterprise/billing/switch_currency_service.rb index 70ea5eec8..dc8d4690e 100644 --- a/enterprise/app/services/enterprise/billing/switch_currency_service.rb +++ b/enterprise/app/services/enterprise/billing/switch_currency_service.rb @@ -94,7 +94,13 @@ class Enterprise::Billing::SwitchCurrencyService def cancel_subscriptions(subscriptions) subscriptions.each do |subscription| Stripe::Subscription.update(subscription.id, metadata: { SWITCH_METADATA_KEY => 'true' }) - Stripe::Subscription.cancel(subscription.id, { prorate: false }) + begin + Stripe::Subscription.cancel(subscription.id, { prorate: false }) + rescue Stripe::StripeError + # Clear the flag so a still-live sub isn't permanently skipped by the webhook guard. + Stripe::Subscription.update(subscription.id, metadata: { SWITCH_METADATA_KEY => '' }) + raise + end end end