From e5818955ff439d9874caa468edd442a8a6637d8a Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 11 Feb 2022 16:22:12 +0000 Subject: [PATCH] Invalidate all incomplete payments when creating a new one, not just those in "checkout" state. Looking at prod data; when a checkout submission fails due to something like a card being out of date, the payment's state seems to be "pending" and not "checkout", which means this mechanism fro invalidating old payments is potentially not working where it should. --- app/models/spree/payment.rb | 2 +- spec/models/spree/payment_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index 1c331fcd28..d1ebf2aaeb 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -206,7 +206,7 @@ module Spree # Makes newly entered payments invalidate previously entered payments so the most recent payment # is used by the gateway. def invalidate_old_payments - order.payments.with_state('checkout').where.not(id: id).each do |payment| + order.payments.incomplete.where.not(id: id).each do |payment| # Using update_column skips validations and so it skips validate_source. As we are just # invalidating past payments here, we don't want to validate all of them at this stage. payment.update_columns( diff --git a/spec/models/spree/payment_spec.rb b/spec/models/spree/payment_spec.rb index 81f5f822f3..420160ae60 100644 --- a/spec/models/spree/payment_spec.rb +++ b/spec/models/spree/payment_spec.rb @@ -49,6 +49,18 @@ describe Spree::Payment do end end + context "creating a new payment alongside other incomplete payments" do + let(:order) { create(:order_with_totals) } + let!(:incomplete_payment) { create(:payment, order: order, state: "pending") } + let(:new_payment) { create(:payment, order: order, state: "checkout") } + + it "invalidates other incomplete payments on the order" do + new_payment + + expect(incomplete_payment.reload.state).to eq "invalid" + end + end + # Regression test for https://github.com/spree/spree/pull/2224 context 'failure' do it 'should transition to failed from pending state' do