From 60524b6453a55f07986c510899b38ea67524237a Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Tue, 9 Jun 2020 13:00:54 -0500 Subject: [PATCH] Ensures billing address is present if it is required Can be controlled via Spree::Config.billing_address_required config flag globally or it can be coming from individual payment method, e.g. it can be disabled by default but required if at least one payment method requires billing address, and this payment method is gotten from order available payment methods allowing flexibility to check only where it is really needed --- core/app/models/spree/order.rb | 12 ++++++++++++ core/app/models/spree/order/checkout.rb | 1 + core/config/locales/en.yml | 1 + core/spec/models/spree/order/checkout_spec.rb | 9 +++++++++ 4 files changed, 23 insertions(+) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4f7c88ceec6..7fffa9d7a9b 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -562,6 +562,18 @@ def ensure_shipping_address end end + def ensure_billing_address + return unless billing_address_required? + return if bill_address&.valid? + + errors.add(:base, I18n.t('spree.bill_address_required')) + false + end + + def billing_address_required? + available_payment_methods.detect {|pm| pm.billing_address_required? }.present? + end + def create_proposed_shipments if completed? raise CannotRebuildShipments.new(I18n.t('spree.cannot_rebuild_shipments_order_completed')) diff --git a/core/app/models/spree/order/checkout.rb b/core/app/models/spree/order/checkout.rb index a22648abe64..886d093ab9a 100644 --- a/core/app/models/spree/order/checkout.rb +++ b/core/app/models/spree/order/checkout.rb @@ -82,6 +82,7 @@ def define_state_machine! after_transition to: :complete, do: :add_payment_sources_to_wallet before_transition to: :payment, do: :add_default_payment_from_wallet + before_transition to: :payment, do: :ensure_billing_address before_transition to: :confirm, do: :add_store_credit_payments diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index 038a12a79e4..db71faa8661 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -1984,6 +1984,7 @@ en: ship: ship ship_address: Ship Address ship_address_required: Valid shipping address required + bill_address_required: Valid billing address required ship_total: Ship Total shipment: Shipment shipment_adjustments: Shipment adjustments diff --git a/core/spec/models/spree/order/checkout_spec.rb b/core/spec/models/spree/order/checkout_spec.rb index c17670d69c3..adf822b2df8 100644 --- a/core/spec/models/spree/order/checkout_spec.rb +++ b/core/spec/models/spree/order/checkout_spec.rb @@ -303,6 +303,15 @@ def assert_state_changed(order, from, to) assert_state_changed(order, 'delivery', 'payment') expect(order.state).to eq('payment') end + + it 'fails if billing address is required and missing' do + payment_method = create(:payment_method) + allow(payment_method).to receive(:billing_address_required?).and_return(true) + allow(order).to receive(:available_payment_methods).and_return([payment_method]) + order.bill_address = nil + + expect { order.next! }.to raise_error(StateMachines::InvalidTransition, /#{I18n.t('spree.bill_address_required')}/) + end end context "without payment required" do