Move handling of unused payments to Order::Updater

An order can be set to paid in various cases that are unrelated to the order being finalized, so this bit of logic needs to be called at the point the order actually gets paid.
This commit is contained in:
Matt-Yorkley
2021-11-15 15:31:12 +00:00
parent 0b3d78b2a5
commit 3e02023bf8
4 changed files with 23 additions and 22 deletions

View File

@@ -344,7 +344,6 @@ module Spree
# update payment and shipment(s) states, and save
updater.update_payment_state
cancel_payments_requiring_auth
shipments.each do |shipment|
shipment.update!(self)
shipment.finalize!
@@ -612,12 +611,6 @@ module Spree
private
def cancel_payments_requiring_auth
return unless payment_state == "paid"
payments.to_a.select(&:requires_authorization?).each(&:void_transaction!)
end
def fee_handler
@fee_handler ||= OrderFeesHandler.new(self)
end

View File

@@ -122,6 +122,7 @@ module OrderManagement
last_payment_state = order.payment_state
order.payment_state = infer_payment_state
cancel_payments_requiring_auth unless last_payment_state == "paid"
track_payment_state_change(last_payment_state)
order.payment_state
@@ -162,6 +163,12 @@ module OrderManagement
private
def cancel_payments_requiring_auth
return unless order.payment_state == "paid"
payments.to_a.select(&:requires_authorization?).each(&:void_transaction!)
end
def round_money(value)
(value * 100).round / 100.0
end

View File

@@ -360,6 +360,22 @@ module OrderManagement
end
end
end
context "when unused payments records exist which require authorization, but the order is fully paid" do
let!(:cash_payment) { build(:payment, state: "completed", amount: order.new_outstanding_balance) }
let!(:stripe_payment) { build(:payment, state: "requires_authorization") }
before do
order.payments << cash_payment
order.payments << stripe_payment
end
it "cancels unused payments requiring authorization" do
expect(stripe_payment).to receive(:void_transaction!)
expect(cash_payment).to_not receive(:void_transaction!)
order.updater.update_payment_state
end
end
end
end
end

View File

@@ -195,21 +195,6 @@ describe Spree::Order do
expect(order.updater).to receive(:before_save_hook)
order.finalize!
end
context "extra payments exist that require authorization" do
let!(:cash_payment) { build(:payment, state: "completed", amount: order.new_outstanding_balance) }
let!(:stripe_payment) { build(:payment, state: "requires_authorization") }
before do
order.payments << cash_payment
order.payments << stripe_payment
end
it "cancels payments requiring authorization" do
expect(stripe_payment).to receive(:void_transaction!)
expect(cash_payment).to_not receive(:void_transaction!)
order.finalize!
end
end
end
context "#process_payments!" do