diff --git a/enterprise/app/services/enterprise/billing/create_stripe_customer_service.rb b/enterprise/app/services/enterprise/billing/create_stripe_customer_service.rb index 972f0bc12..e20791026 100644 --- a/enterprise/app/services/enterprise/billing/create_stripe_customer_service.rb +++ b/enterprise/app/services/enterprise/billing/create_stripe_customer_service.rb @@ -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 diff --git a/enterprise/app/services/enterprise/billing/currencies.rb b/enterprise/app/services/enterprise/billing/currencies.rb index c19892cd2..50d60122e 100644 --- a/enterprise/app/services/enterprise/billing/currencies.rb +++ b/enterprise/app/services/enterprise/billing/currencies.rb @@ -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 diff --git a/spec/enterprise/services/enterprise/billing/create_stripe_customer_service_spec.rb b/spec/enterprise/services/enterprise/billing/create_stripe_customer_service_spec.rb index 825e20819..b25ddbb33 100644 --- a/spec/enterprise/services/enterprise/billing/create_stripe_customer_service_spec.rb +++ b/spec/enterprise/services/enterprise/billing/create_stripe_customer_service_spec.rb @@ -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 diff --git a/spec/enterprise/services/enterprise/billing/currencies_spec.rb b/spec/enterprise/services/enterprise/billing/currencies_spec.rb index f1c048b90..84172137e 100644 --- a/spec/enterprise/services/enterprise/billing/currencies_spec.rb +++ b/spec/enterprise/services/enterprise/billing/currencies_spec.rb @@ -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