Skip to content

Commit

Permalink
Ensures billing address is present if it is required
Browse files Browse the repository at this point in the history
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
  • Loading branch information
softr8 committed Jun 9, 2020
1 parent 05ef512 commit 60524b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/order/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60524b6

Please sign in to comment.