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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex f0529a5660..4a86f0027c 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -39,14 +39,15 @@ module Spreeafter_initialize :set_amount, if: -> { new_record? && order.present? && !amount_changed? }+ #+ # Callbacksafter_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 paymentsafter_create :invalidate_old_paymentsafter_create :create_eligible_credit_event+ after_destroy :update_orderattr_accessor :source_attributes, :request_env, :capture_on_dispatchdiff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex 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 doit { expect(Spree::Payment::INVALID_STATES).to eq(%w(failed invalid void)) }end- describe 'scopes' do+ describe 'Scopes' dodescribe '.valid' dosubject { Spree::Payment.valid }@@ -150,7 +150,7 @@ describe Spree::Payment, type: :model doendend- context 'validations' do+ describe 'Validations' docontext 'when payment source is not required' doit 'do not validate source presence' doallow_any_instance_of(Spree::PaymentMethod).to receive(:source_required?).and_return(false)@@ -205,6 +205,24 @@ describe Spree::Payment, type: :model doendend+ 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/2224context 'failure' doit 'transitions to failed from pending state' do