Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Follow countries and states off the database

Damian Legawiec02c4f5c1aeb5
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
index 2ee3182321..033808a87c 100644
--- a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
@@ -270,12 +270,13 @@ module SpreeStripe
return if owner.bill_address.present? && owner.bill_address.valid?
- country_iso = address.country
- country = (country_iso.present? && Spree::Country.by_iso(country_iso)) ||
+ country = (address.country.presence && Spree::Country.by_iso(address.country)) ||
owner.store.default_market&.default_country ||
Spree::Country.by_iso('US')
- owner.bill_address ||= Spree::Address.new(country: country, customer: owner.customer)
+ # Countries are reference data now, not records — addresses name one by
+ # ISO code rather than holding a foreign key.
+ owner.bill_address ||= Spree::Address.new(country_iso: country.iso, customer: owner.customer)
owner.bill_address.quick_checkout = true
# Google Pay sometimes omits the name entirely.
@@ -290,11 +291,13 @@ module SpreeStripe
owner.bill_address.city ||= address.city
owner.bill_address.zipcode ||= address.postal_code
+ # Stripe sends whatever the wallet had — a code or a full name — so it
+ # is resolved to a current subdivision code. The free-text state_name
+ # is kept as the fallback for countries without subdivisions, and as a
+ # record of what the wallet actually said.
state_name = address.state
if country.states_required?
- owner.bill_address.state = country.states.find_all_by_name_or_abbr(state_name)&.first
- else
- owner.bill_address.state_name = state_name
+ owner.bill_address.state_code ||= Spree::IsoData.subdivision_code(country.iso, state_name)
end
owner.bill_address.state_name ||= state_name
diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
index 1fab54c5bf..2895a83d26 100644
--- a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
@@ -237,7 +237,7 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions do
context 'when the charge billing address has a known country ISO' do
let!(:california) do
usa = Spree::Country.by_iso('US')
- usa.states.find_by(abbr: 'CA') || create(:state, name: 'California', abbr: 'CA', country: usa)
+ usa.states.find { |state| state.abbr == 'CA' }
end
it 'builds the bill_address from the charge billing details' do
@@ -274,12 +274,16 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions do
let(:stripe_charge) { build_charge({}, country: nil) }
context 'and the store has a default_market with a default_country' do
- let(:default_country) { create(:country, iso: 'CA', name: 'Canada') }
-
+ # Great Britain: the charge's address stays valid there (no
+ # subdivision required), so the fallback country actually survives
+ # the address validation instead of degrading to the ship address.
+ let(:default_country) { Spree::Country.by_iso('GB') }
+
+ # The store's real default market, repointed. Stubbing cannot work
+ # here: the flow reloads the order, so it sees the database rather
+ # than any in-memory double.
before do
- allow_any_instance_of(Spree::Store).to receive(:default_market).and_return(
- instance_double(Spree::Market, default_country: default_country)
- )
+ order.store.default_market.update!(countries: [default_country])
end
it 'falls back to the store default_market default_country' do
@@ -289,7 +293,7 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions do
order.reload
expect(order.bill_address).to be_present
- expect(order.bill_address.country).to eq(default_country)
+ expect(order.bill_address.country_iso).to eq(default_country.iso)
end
end
diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb
index e2f6b9dc21..0a10e67a09 100644
--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb
+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb
@@ -352,8 +352,8 @@ RSpec.describe SpreeStripe::Gateway do
)
end
- let(:usa_country) { Spree::Country.by_iso('US') || create(:country_us) }
- let(:california_state) { usa_country.states.find_by(abbr: 'CA') || create(:state, name: 'California', abbr: 'CA', country: usa_country) }
+ let(:usa_country) { Spree::Country.by_iso('US') }
+ let(:california_state) { usa_country.states.find { |state| state.abbr == 'CA' } }
before do
order.update!(shipping_address: address)
@@ -715,9 +715,9 @@ RSpec.describe SpreeStripe::Gateway do
)
end
- let(:usa_country) { Spree::Country.by_iso('US') || create(:country_us) }
- let(:california_state) { usa_country.states.find_by(abbr: 'CA') || create(:state, name: 'California', abbr: 'CA', country: usa_country) }
- let(:new_york_state) { usa_country.states.find_by(abbr: 'NY') || create(:state, name: 'New York', abbr: 'NY', country: usa_country) }
+ let(:usa_country) { Spree::Country.by_iso('US') }
+ let(:california_state) { usa_country.states.find { |state| state.abbr == 'CA' } }
+ let(:new_york_state) { usa_country.states.find { |state| state.abbr == 'NY' } }
let(:gateway_customer) { Spree::GatewayCustomer.for_provider(SpreeStripe::Gateway).last }
@@ -812,9 +812,9 @@ RSpec.describe SpreeStripe::Gateway do
)
end
- let(:usa_country) { Spree::Country.by_iso('US') || create(:country_us) }
- let(:california_state) { usa_country.states.find_by(abbr: 'CA') || create(:state, name: 'California', abbr: 'CA', country: usa_country) }
- let(:new_york_state) { usa_country.states.find_by(abbr: 'NY') || create(:state, name: 'New York', abbr: 'NY', country: usa_country) }
+ let(:usa_country) { Spree::Country.by_iso('US') }
+ let(:california_state) { usa_country.states.find { |state| state.abbr == 'CA' } }
+ let(:new_york_state) { usa_country.states.find { |state| state.abbr == 'NY' } }
let!(:gateway_customer) { create(:gateway_customer, customer: customer, profile_id: customer_id, payment_method: gateway) }
let(:customer_id) { 'cus_SeIsxI1TG3dGJv' }