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
Fix: capped store credit at what the order can still give back
Damian Legawiec0d56cf6515ca
diff --git a/spree/core/app/workflows/spree/refunds/order_payments.rb b/spree/core/app/workflows/spree/refunds/order_payments.rbindex 2da7831306..deb5daa02c 100644--- a/spree/core/app/workflows/spree/refunds/order_payments.rb+++ b/spree/core/app/workflows/spree/refunds/order_payments.rb@@ -39,6 +39,8 @@ module Spree# @return [Array<Spree::StoreCredit>] the one credit written, shaped like# {#refund_order_payments} so both branches answer the same waydef issue_refund_store_credit(order:, amount:, record:, memo:, refunder: nil)+ failure(record, :refund_exceeds_paid) if amount.to_d > store_creditable_amount(order)+[Spree::StoreCredit.create!(store: record.store,@@ -53,6 +55,23 @@ module Spree]end+ # What the order can still give back as store credit: what its payments+ # can still refund, less credit already issued against it. Credit writes+ # no refund row, so without the second half every return, claim and+ # exchange on one order could each hand back the full amount paid.+ #+ # The order row is locked first, so two settlements racing on one order+ # cannot both read the same headroom.+ #+ # @param order [Spree::Order]+ # @return [BigDecimal]+ def store_creditable_amount(order)+ Spree::Order.lock.find(order.id)++ refundable_shares(order).values.sum(0.to_d) -+ Spree::StoreCredit.where(refunded_order: order).sum(:amount).to_d+ end+# Puts `amount` back on `order`, oldest payment first, until it is# covered.#diff --git a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rbindex b33a308dd2..444cbd2978 100644--- a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb@@ -142,6 +142,18 @@ RSpec.describe 'Spree::Claims workflows' doexpect(result.error.value).to eq(:refund_exceeds_paid)end+ # Store credit writes no refund row, so without a ceiling on the order+ # every settlement could each give back the full amount paid.+ it 'refuses store credit beyond what the order can still give back' do+ create(:store_credit, store: store, customer: order.customer, refunded_order: order, amount: order.total)++ result = Spree::Claims::Resolve.call(claim: claim, resolution: 'refund')++ expect(result).to be_failure+ expect(result.error.value).to eq(:refund_exceeds_paid)+ expect(Spree::StoreCredit.find_by(originator: claim)).to be_nil+ end+it 'ships a replacement' doclaim = create(:approved_claim, store: store, order: order, send_replacement: true)claim.claim_line_items.each { |line| line.variant.stock_levels.first&.set_count_on_hand(10) }diff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rbindex 93dfcc3f37..eddc455158 100644--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb@@ -303,6 +303,9 @@ RSpec.describe 'Spree::Returns workflows' dobefore doreturn_record.return_line_items.first.update!(quantity: 3, received_quantity: 2, pre_tax_amount: 269.97)+ # Store credit is capped at what the order paid, so the order has to+ # have paid for the three units this return now claims.+ return_record.order.payments.completed.first.update_column(:amount, 500)endit 'credits only what arrived when the caller names no amount' do