Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed: update order payment total after destroying a payment (#12559)

Damian Legawiec39ee5a8fa37f
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index f0529a5660..4a86f0027c 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -39,14 +39,15 @@ module Spree
after_initialize :set_amount, if: -> { new_record? && order.present? && !amount_changed? }
+ #
+ # Callbacks
after_save :create_payment_profile, if: :profiles_supported?
-
# update the order totals, etc.
- set_callback :save, :after, :update_order, unless: -> { capture_on_dispatch }
-
+ after_save :update_order, unless: -> { capture_on_dispatch }
# invalidate previously entered payments
after_create :invalidate_old_payments
after_create :create_eligible_credit_event
+ after_destroy :update_order
attr_accessor :source_attributes, :request_env, :capture_on_dispatch
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 6627faa353..6324ad2ab8 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -53,7 +53,7 @@ describe Spree::Payment, type: :model do
it { expect(Spree::Payment::INVALID_STATES).to eq(%w(failed invalid void)) }
end
- describe 'scopes' do
+ describe 'Scopes' do
describe '.valid' do
subject { Spree::Payment.valid }
@@ -150,7 +150,7 @@ describe Spree::Payment, type: :model do
end
end
- context 'validations' do
+ describe 'Validations' do
context 'when payment source is not required' do
it 'do not validate source presence' do
allow_any_instance_of(Spree::PaymentMethod).to receive(:source_required?).and_return(false)
@@ -205,6 +205,24 @@ describe Spree::Payment, type: :model do
end
end
+ describe 'Callbacks' do
+ describe '#update_order' do
+ let(:payment) { create(:payment, order: order, state: 'completed') }
+
+ context 'when destroying completed payment' do
+ it 'updates the order' do
+ expect { payment.destroy }.to change { order.payment_total }.by(-payment.amount)
+ end
+ end
+
+ context 'when voiding a payment' do
+ it 'updates the order' do
+ expect { payment.void! }.to change { order.payment_total }.by(-payment.amount)
+ end
+ end
+ end
+ end
+
# Regression test for https://github.com/spree/spree/pull/2224
context 'failure' do
it 'transitions to failed from pending state' do