fix(billing): gate currency switch by locale and sync Stripe location after success
This commit is contained in:
@@ -168,6 +168,7 @@ en:
|
||||
no_payment_method: No payment methods found. Please add a payment method before making a purchase.
|
||||
billing:
|
||||
currency_required: Currency is required
|
||||
currency_switch_unavailable: Currency switching is not available for this account
|
||||
unsupported_currency: This currency is not supported
|
||||
same_currency: This account is already billed in the selected currency
|
||||
stripe_customer_not_configured: Stripe customer not configured
|
||||
|
||||
@@ -38,6 +38,11 @@ module Enterprise::Billing::Currencies
|
||||
LOCALE_DEFAULTS.fetch(locale.to_s, DEFAULT)
|
||||
end
|
||||
|
||||
# Currency switching is rolled out only to locales with a non-default currency (e.g. pt_BR).
|
||||
def rollout_enabled?(locale)
|
||||
LOCALE_DEFAULTS.key?(locale.to_s)
|
||||
end
|
||||
|
||||
def country_for(code)
|
||||
COUNTRY_BY_CURRENCY[coerce(code)]
|
||||
end
|
||||
|
||||
@@ -28,6 +28,7 @@ class Enterprise::Billing::SwitchCurrencyService
|
||||
end
|
||||
|
||||
def validate!
|
||||
raise Error, I18n.t('errors.billing.currency_switch_unavailable') unless Enterprise::Billing::Currencies.rollout_enabled?(account.locale)
|
||||
raise Error, I18n.t('errors.billing.unsupported_currency') unless Enterprise::Billing::Currencies.supported?(currency)
|
||||
raise Error, I18n.t('errors.billing.same_currency') if target_currency == account.billing_currency
|
||||
raise Error, I18n.t('errors.billing.stripe_customer_not_configured') if stripe_customer_id.blank?
|
||||
@@ -35,8 +36,8 @@ class Enterprise::Billing::SwitchCurrencyService
|
||||
|
||||
# Free plan: no subscription churn — record the preference and sync the Stripe customer.
|
||||
def switch_free_plan
|
||||
sync_stripe_customer_location
|
||||
persist_currency(account.custom_attributes.merge('billing_currency' => target_currency))
|
||||
sync_stripe_customer_location
|
||||
end
|
||||
|
||||
# Replace all live subs with one paid sub in the target currency, preserving seats and paid-through.
|
||||
@@ -54,10 +55,10 @@ class Enterprise::Billing::SwitchCurrencyService
|
||||
key: paid_subscription.id
|
||||
}
|
||||
|
||||
sync_stripe_customer_location
|
||||
new_subscription = replace_subscriptions(subscriptions, change)
|
||||
|
||||
persist_currency(build_custom_attributes(new_subscription, plan))
|
||||
sync_stripe_customer_location
|
||||
Enterprise::Billing::ReconcilePlanFeaturesService.new(account: account).perform
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user