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
Tell the tax provider how much of a return was actually refunded
Łukasz Adamczyk217d8bab1349
diff --git a/spree/core/app/models/spree/tax_provider/base.rb b/spree/core/app/models/spree/tax_provider/base.rbindex ef582f6853..cfc2a6d802 100644--- a/spree/core/app/models/spree/tax_provider/base.rb+++ b/spree/core/app/models/spree/tax_provider/base.rb@@ -104,11 +104,20 @@ module Spree# Reports a partial credit against the committed document, keyed to the# original transaction rather than voiding and re-committing it.#+ # +amount+ is what the customer was actually refunded, which is not always+ # the returned lines' worth: an admin may keep a restocking fee or refund+ # a goodwill figure of their own. A provider must credit no more tax than+ # that refund carries — crediting every returned line when only part of+ # their value went back would reclaim tax the merchant never repaid, and a+ # return is marked refunded once, so nothing later corrects it. Where the+ # amount is short, allocate proportionally.+ ## @param order [Spree::Order]# @param return_items [Array<Spree::ReturnLineItem>] the returned lines+ # @param amount [BigDecimal, nil] the refund issued; nil = the lines' full worth# @param tax_date [Time, nil] the original supply date; nil = order.completed_at# @return [void]- def refund(order, return_items, tax_date: nil); end+ def refund(order, return_items, amount: nil, tax_date: nil); endendendenddiff --git a/spree/core/app/workflows/spree/returns/refund.rb b/spree/core/app/workflows/spree/returns/refund.rbindex f8043e50eb..63b4114273 100644--- a/spree/core/app/workflows/spree/returns/refund.rb+++ b/spree/core/app/workflows/spree/returns/refund.rb@@ -53,12 +53,16 @@ module Spree# Only lines that actually arrived are credited, matching what# +received_total+ refunds: a customer who announced three items and sent# two must not have tax credited on the third.+ #+ # The refunded amount goes with them, because it can be less than those+ # lines are worth — a restocking fee, or an agreed part-refund. Without it+ # a provider would credit the tax on goods whose value the merchant kept.def refund_taxreceived = return_record.return_line_items.select { |line| line.received_quantity.to_i.positive? }return if received.empty?order = return_record.order- order.tax_provider.refund(order, received, tax_date: order.completed_at)+ order.tax_provider.refund(order, received, amount: @amount_to_refund, tax_date: order.completed_at)enddef internal_refund?diff --git a/spree/core/spec/models/spree/tax_provider/base_spec.rb b/spree/core/spec/models/spree/tax_provider/base_spec.rbindex 5604232ce3..9c7c4dd369 100644--- a/spree/core/spec/models/spree/tax_provider/base_spec.rb+++ b/spree/core/spec/models/spree/tax_provider/base_spec.rb@@ -22,11 +22,17 @@ describe Spree::TaxProvider::Base, type: :model doit 'no-ops for a provider without a remote ledger' doexpect(provider.commit(order)).to be_nilexpect(provider.void(order)).to be_nil- expect(provider.refund(order, [], tax_date: order.completed_at)).to be_nil+ expect(provider.refund(order, [], amount: 10, tax_date: order.completed_at)).to be_nilend- it 'takes the refunded items rather than an amount' do- expect(provider.method(:refund).parameters).to eq([[:req, :order], [:req, :return_items], [:key, :tax_date]])+ # The items are what identifies the credit — a bare amount could not say+ # which lines, at which rates, in which jurisdiction. The amount rides+ # alongside because it can be less than those lines are worth, and a+ # provider must not credit tax the merchant never repaid.+ it 'takes the refunded items, and how much actually went back' do+ expect(provider.method(:refund).parameters).to eq(+ [[:req, :order], [:req, :return_items], [:key, :amount], [:key, :tax_date]]+ )endenddiff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rbindex 5caebd64a9..1041f41ce4 100644--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb@@ -207,7 +207,27 @@ RSpec.describe 'Spree::Returns workflows' doSpree::Returns::Refund.call(return_record: return_record, refund_method: 'store_credit')expect(provider).to have_received(:refund).with(- order, return_record.return_line_items.to_a, tax_date: order.completed_at+ order, return_record.return_line_items.to_a,+ amount: return_record.refund_total, tax_date: order.completed_at+ )+ end++ # A partial refund must not hand the provider the returned lines with no+ # word of how little went back: crediting their full tax would reclaim tax+ # the merchant kept, and a return is marked refunded only once.+ it 'tells the provider how much was actually refunded' do+ order = return_record.order+ provider = instance_double(Spree::TaxProvider::Internal, refund: nil, estimate: nil)+ allow(order).to receive(:tax_provider).and_return(provider)+ allow_any_instance_of(Spree::Return).to receive(:order).and_return(order)++ part = (return_record.refund_total / 2).round(2)++ Spree::Returns::Refund.call(return_record: return_record, amount: part,+ refund_method: 'store_credit')++ expect(provider).to have_received(:refund).with(+ order, anything, amount: part, tax_date: order.completed_at)end