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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 SpreeStripereturn 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 SpreeStripeowner.bill_address.city ||= address.cityowner.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.stateif 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)endowner.bill_address.state_name ||= state_namediff --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.rbindex 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 docontext 'when the charge billing address has a known country ISO' dolet!(:california) dousa = 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' }endit 'builds the bill_address from the charge billing details' do@@ -274,12 +274,16 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions dolet(: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])endit 'falls back to the store default_market default_country' do@@ -289,7 +293,7 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions doorder.reloadexpect(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)endenddiff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex 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 doorder.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' }