fix(billing): only set Stripe billing country for currencies that require an override

This commit is contained in:
Tanmay Deep Sharma
2026-06-11 18:13:26 +05:30
parent 15f920c9af
commit 56d9b232d1
4 changed files with 35 additions and 15 deletions
@@ -23,20 +23,22 @@ class Enterprise::Billing::CreateStripeCustomerService
def prepare_customer_id
customer_id = account.custom_attributes['stripe_customer_id']
if customer_id.blank?
customer = Stripe::Customer.create(
{
name: account.name,
email: billing_email,
address: { country: Enterprise::Billing::Currencies.country_for(account.billing_currency) },
preferred_locales: [Enterprise::Billing::Currencies.preferred_locale_for(account.billing_currency)]
}
)
customer_id = customer.id
end
customer_id = Stripe::Customer.create(customer_params).id if customer_id.blank?
customer_id
end
# Only currencies that need a country override (e.g. BRL/PIX) set address/locale; usd keeps Stripe defaults.
def customer_params
params = { name: account.name, email: billing_email }
country = Enterprise::Billing::Currencies.country_for(account.billing_currency)
return params if country.blank?
params.merge(
address: { country: country },
preferred_locales: [Enterprise::Billing::Currencies.preferred_locale_for(account.billing_currency)]
)
end
def default_quantity
default_plan['default_quantity'] || DEFAULT_QUANTITY
end
@@ -9,13 +9,13 @@ module Enterprise::Billing::Currencies
'pt_BR' => 'brl'
}.freeze
# Billing country override per currency; absent currencies (e.g. usd) keep Stripe's default.
COUNTRY_BY_CURRENCY = {
'usd' => 'US',
'brl' => 'BR'
}.freeze
# Preferred Stripe/checkout locale per currency; absent currencies keep Stripe's default.
PREFERRED_LOCALE_BY_CURRENCY = {
'usd' => 'en',
'brl' => 'pt-BR'
}.freeze
@@ -97,7 +97,7 @@ describe Enterprise::Billing::CreateStripeCustomerService do
create_stripe_customer_service.new(account: account).perform
expect(Stripe::Customer).to have_received(:create).with(
{ name: account.name, email: admin1.email, address: { country: 'US' }, preferred_locales: ['en'] }
{ name: account.name, email: admin1.email }
)
expect(Stripe::Subscription)
.to have_received(:create)
@@ -116,6 +116,20 @@ describe Enterprise::Billing::CreateStripeCustomerService do
}.with_indifferent_access
)
end
it 'sets the billing country override when the account currency requires it' do
account.update!(custom_attributes: { billing_currency: 'brl' })
customer = double
allow(Stripe::Customer).to receive(:create).and_return(customer)
allow(customer).to receive(:id).and_return('cus_random_number')
allow(Stripe::Subscription).to receive(:create).and_return(created_subscription)
create_stripe_customer_service.new(account: account).perform
expect(Stripe::Customer).to have_received(:create).with(
{ name: account.name, email: admin1.email, address: { country: 'BR' }, preferred_locales: ['pt-BR'] }
)
end
end
describe 'when checking for existing subscriptions' do
@@ -26,7 +26,11 @@ describe Enterprise::Billing::Currencies do
it 'falls back to the usd default for unsupported input' do
expect(described_class.to_supported('eur')).to eq('usd')
expect(described_class.country_for('eur')).to eq('US')
end
it 'does not set a country override for usd customers' do
expect(described_class.country_for('usd')).to be_nil
expect(described_class.preferred_locale_for('usd')).to be_nil
end
end
end