Skip to content

Commit

Permalink
Rename method that recalculates payment state
Browse files Browse the repository at this point in the history
Update implies that we are persisting the change in Rails, which this
method does not do.

Co-authored-by: Adam Mueller <[email protected]>
Co-authored-by: Andrew Stewart <[email protected]>
Co-authored-by: Benjamin Willems <[email protected]>
Co-authored-by: Senem Soy <[email protected]>
Co-authored-by: Sofia Besenski <[email protected]>
Co-authored-by: Kendra Riga <[email protected]>
  • Loading branch information
7 people committed Nov 8, 2024
1 parent 8040224 commit 7ce5874
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
8 changes: 5 additions & 3 deletions core/app/models/spree/in_memory_order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def recalculate(persist: true)
update_shipment_amounts(persist:)
update_totals(persist:)
if order.completed?
update_payment_state
recalculate_payment_state
update_shipments
recalculate_shipment_state
end
Expand Down Expand Up @@ -54,7 +54,7 @@ def recalculate_shipment_state
alias_method :update_shipment_state, :recalculate_shipment_state
deprecate update_shipment_state: :recalculate_shipment_state, deprecator: Spree.deprecator

# Updates the +payment_state+ attribute according to the following logic:
# Recalculates the +payment_state+ attribute according to the following logic:
#
# paid when +payment_total+ is equal to +total+
# balance_due when +payment_total+ is less than +total+
Expand All @@ -63,13 +63,15 @@ def recalculate_shipment_state
# void when the order has been canceled and the payment total is 0
#
# The +payment_state+ value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention.
def update_payment_state
def recalculate_payment_state
log_state_change('payment') do
order.payment_state = determine_payment_state
end

order.payment_state
end
alias_method :update_payment_state, :recalculate_shipment_state
deprecate update_payment_state: :recalculate_shipment_state, deprecator: Spree.deprecator

private

Expand Down
21 changes: 10 additions & 11 deletions core/spec/models/spree/in_memory_order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ module Spree

context "updating payment state" do
let(:order) { build(:order) }
let(:updater) { order.recalculator }
before { allow(order).to receive(:refund_total).and_return(0) }

context 'no valid payments with non-zero order total' do
Expand All @@ -264,7 +263,7 @@ module Spree
order.total = 1
order.payment_total = 0

updater.update_payment_state
updater.recalculate_payment_state
expect(order.payment_state).to eq('failed')
end
end
Expand All @@ -276,7 +275,7 @@ module Spree
order.payment_total = 0

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'paid'
end
end
Expand All @@ -287,7 +286,7 @@ module Spree
order.total = 1

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'credit_owed'
end
end
Expand All @@ -298,7 +297,7 @@ module Spree
order.total = 2

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'balance_due'
end
end
Expand All @@ -309,7 +308,7 @@ module Spree
order.total = 30

expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'paid'
end
end
Expand All @@ -324,7 +323,7 @@ module Spree
order.payment_total = 0
order.total = 30
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'void'
end
end
Expand All @@ -335,7 +334,7 @@ module Spree
order.total = 30
create(:payment, order: order, state: 'completed', amount: 30)
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'credit_owed'
end
end
Expand All @@ -345,7 +344,7 @@ module Spree
order.payment_total = 0
order.total = 30
expect {
updater.update_payment_state
updater.recalculate_payment_state
}.to change { order.payment_state }.to 'void'
end
end
Expand All @@ -356,7 +355,7 @@ module Spree
before { allow(order).to receive_messages completed?: true }

it "updates payment state" do
expect(updater).to receive(:update_payment_state)
expect(updater).to receive(:recalculate_payment_state)
updater.recalculate
end

Expand Down Expand Up @@ -385,7 +384,7 @@ module Spree
before { allow(order).to receive_messages completed?: false }

it "doesnt update payment state" do
expect(updater).not_to receive(:update_payment_state)
expect(updater).not_to receive(:recalculate_payment_state)
updater.recalculate
end

Expand Down

0 comments on commit 7ce5874

Please sign in to comment.