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
Feature/v 3312 incorrect total applied store credit (#13512)
Szymon Chorobskid2fe31a923b2
diff --git a/core/app/models/spree/order/store_credit.rb b/core/app/models/spree/order/store_credit.rbindex 328160023e..87d8a0137f 100644--- a/core/app/models/spree/order/store_credit.rb+++ b/core/app/models/spree/order/store_credit.rb@@ -66,7 +66,10 @@ module Spree# @return [BigDecimal] The total amount of store credits applied to the order.def total_applied_store_creditif payments.loaded?- payments.find_all(&:store_credit?).find_all(&:valid?).sum(&:amount) || BigDecimal::ZERO+ payments.+ find_all(&:store_credit?).+ reject(&:has_invalid_state?).+ sum(&:amount) || BigDecimal::ZEROelsepayments.store_credits.valid.sum(:amount)enddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex 3c4f80681c..b30358e036 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -278,6 +278,10 @@ module Spreeendend+ def has_invalid_state?+ INVALID_STATES.include?(state)+ end+privatedef set_amount@@ -302,10 +306,6 @@ module Spreepublish_event('payment.voided')end- def has_invalid_state?- INVALID_STATES.include?(state)- end-def validate_sourceif source && !source.valid?source.errors.map { |error| { field: error.attribute, message: error&.message } }.each do |err|diff --git a/core/spec/models/spree/order/store_credit_spec.rb b/core/spec/models/spree/order/store_credit_spec.rbindex 08977dd950..60de4b389c 100644--- a/core/spec/models/spree/order/store_credit_spec.rb+++ b/core/spec/models/spree/order/store_credit_spec.rb@@ -25,6 +25,8 @@ shared_examples 'check total store credit from payments' doenddescribe 'Order' do+ subject(:order) { create(:order, total: 100.00) }+describe '#add_store_credit_payments' dosubject { order.add_store_credit_payments }@@ -407,25 +409,41 @@ describe 'Order' doenddescribe '#total_applied_store_credit' do+ subject(:total_applied_store_credit) { order.total_applied_store_credit }+context 'with valid payments' do- subject { order }+ let(:order_1_valid_payment) { 10.0 }+ let(:order_1_valid_payment_2) { 20.0 }+ let(:order_1_invalid_payment) { 21.0 }+ let(:order_2_valid_payment) { 22.0 }- let(:order) { payment.order }- let!(:payment) { create(:store_credit_payment) }- let!(:second_payment) { create(:store_credit_payment, order: order) }+ before do+ create(:store_credit_payment, order: order, state: 'completed', amount: order_1_valid_payment)+ create(:store_credit_payment, order: order, state: 'completed', amount: order_1_valid_payment_2)+ create(:store_credit_payment, order: order, state: 'invalid', amount: order_1_invalid_payment)+ create(:store_credit_payment, order: create(:order, total: 100.00), state: 'completed', amount: order_2_valid_payment)+ endit 'returns the sum of the payment amounts' do- expect(subject.total_applied_store_credit).to eq (payment.amount + second_payment.amount)+ expect(total_applied_store_credit).to eq (order_1_valid_payment + order_1_valid_payment_2)+ end++ context 'when payments are loaded' do+ before do+ order.payments.load+ end++ it 'returns the sum of the payment amounts' do+ expect(total_applied_store_credit).to eq (order_1_valid_payment + order_1_valid_payment_2)+ endendendcontext 'without valid payments' do- subject { order }-let(:order) { create(:order) }it 'returns 0' do- expect(subject.total_applied_store_credit).to be_zero+ expect(total_applied_store_credit).to be_zeroendendenddiff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex 26429f7a4d..0ac39c817a 100644--- a/core/spec/models/spree/payment_spec.rb+++ b/core/spec/models/spree/payment_spec.rb@@ -1296,6 +1296,23 @@ describe Spree::Payment, type: :model doendend+ describe '#has_invalid_state?' do+ let(:payment) { create(:payment, state: state) }+ subject(:has_invalid_state?) { payment.has_invalid_state? }++ context 'when the state is invalid' do+ let(:state) { Spree::Payment::INVALID_STATES.first }++ it { is_expected.to be_truthy }+ end++ context 'when the state is valid' do+ let(:state) { 'completed' }++ it { is_expected.to be_falsey }+ end+ end+describe '#gateway_processing_error_messages' dobefore dopayment.add_gateway_processing_error('Gateway processing error')