diff --git a/config/locales/en.yml b/config/locales/en.yml index e09d9481b..ce52530c6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/enterprise/app/services/enterprise/billing/currencies.rb b/enterprise/app/services/enterprise/billing/currencies.rb index 783ba7547..e24d261da 100644 --- a/enterprise/app/services/enterprise/billing/currencies.rb +++ b/enterprise/app/services/enterprise/billing/currencies.rb @@ -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 diff --git a/enterprise/app/services/enterprise/billing/switch_currency_service.rb b/enterprise/app/services/enterprise/billing/switch_currency_service.rb index 36bdf1525..791a34e71 100644 --- a/enterprise/app/services/enterprise/billing/switch_currency_service.rb +++ b/enterprise/app/services/enterprise/billing/switch_currency_service.rb @@ -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