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
Phase 9: let each market choose its tax engine, and tell it the outcome
Łukasz Adamczykcc0ee3559cf7
diff --git a/packages/admin-sdk/src/types/generated/Market.ts b/packages/admin-sdk/src/types/generated/Market.tsindex 4e2f7f1de6..69a12b83f5 100644--- a/packages/admin-sdk/src/types/generated/Market.ts+++ b/packages/admin-sdk/src/types/generated/Market.ts@@ -1,4 +1,4 @@-// Typelizer digest 92b5c97c745dfd6f7f456ff4b135826a+// Typelizer digest 3a86f2db1c82f77d3a9ef3ca5079fbe4//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {Country} from '@/types'@@ -13,6 +13,7 @@ interface Market {country_isos: Array<string>;supported_locales: Array<string>;countries?: Array<Country>;+ tax_provider: string | null;created_at: string;updated_at: string;}diff --git a/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rbindex f981968782..13c6f726b8 100644--- a/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb@@ -30,7 +30,7 @@ module Spreedef permitted_paramsnormalize_params(params.permit(- :name, :currency, :default_locale, :tax_inclusive,+ :name, :currency, :default_locale, :tax_inclusive, :tax_provider,:default, :position, supported_locales: [], country_isos: []))diff --git a/spree/api/app/controllers/spree/api/v3/admin/tax_providers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tax_providers_controller.rbnew file mode 100644index 0000000000..c8aa9d688c--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/admin/tax_providers_controller.rb@@ -0,0 +1,35 @@+module Spree+ module Api+ module V3+ module Admin+ # Which tax engines this installation can point a market at, and what+ # each of them cannot do. Discovery rather than CRUD: providers are+ # classes registered in code, not rows.+ #+ # The capability list is the point — a merchant selling into US states+ # needs to be told at configuration time that the built-in engine has no+ # local-tax data, rather than discovering it from a tax bill.+ class TaxProvidersController < BaseController+ scoped_resource :settings++ # GET /api/v3/admin/tax_providers+ def index+ render json: { data: Spree.tax_providers.map { |provider| serialize_provider(provider) } }+ end++ private++ def serialize_provider(provider)+ {+ id: provider.to_s,+ name: provider.to_s.demodulize.titleize,+ available: provider.available_for_store?(current_store),+ unsupported_capabilities: provider.unsupported_capabilities.map(&:to_s),+ default: provider.to_s == Rails.application.config.spree.tax_provider.to_s+ }+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/admin/market_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/market_serializer.rbindex b0d0a78cc9..675b222b40 100644--- a/spree/api/app/serializers/spree/api/v3/admin/market_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/market_serializer.rb@@ -3,7 +3,11 @@ module Spreemodule V3module Adminclass MarketSerializer < V3::MarketSerializer- attributes created_at: :iso8601, updated_at: :iso8601+ typelize tax_provider: [:string, nullable: true]++ # Which tax engine computes for this market. Nil means the store-wide+ # default; the selectable values come from /admin/tax_providers.+ attributes :tax_provider, created_at: :iso8601, updated_at: :iso8601many :countries,resource: proc { Spree.api.admin_country_serializer },diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex 28736f9329..94daf9d843 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -309,6 +309,9 @@ Spree::Core::Engine.add_routes do# Tax Rates — the internal tax provider's configurationresources :tax_rates+ # Selectable tax engines and their declared limits (discovery only)+ resources :tax_providers, only: [:index]+# Return / claim / refund reasons (dropdowns + settings management)resources :return_reasonsresources :claim_reasonsdiff --git a/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rbindex cb2fe7a75d..5df4311ac7 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb@@ -35,5 +35,39 @@ RSpec.describe Spree::Api::V3::Admin::MarketsController, type: :controller doexpect(response).to have_http_status(:not_found)end++ it 'reports which tax engine computes for the market' do+ get :show, params: { id: market.prefixed_id }, as: :json++ # Nil means the store-wide default.+ expect(json_response).to have_key('tax_provider')+ expect(json_response['tax_provider']).to be_nil+ end+ end++ describe 'PATCH #update' do+ it 'points the market at an installed tax engine' do+ patch :update, params: { id: market.prefixed_id, tax_provider: 'Spree::TaxProvider::Internal' }, as: :json++ expect(response).to have_http_status(:ok)+ expect(market.reload.tax_provider).to eq('Spree::TaxProvider::Internal')+ expect(market.tax_provider_instance).to be_a(Spree::TaxProvider::Internal)+ end++ it 'refuses an engine this installation does not have' do+ patch :update, params: { id: market.prefixed_id, tax_provider: 'MyApp::ImaginaryProvider' }, as: :json++ expect(response).to have_http_status(:unprocessable_content)+ expect(market.reload.tax_provider).to be_nil+ end++ it 'clears the selection back to the store default' do+ market.update_columns(tax_provider: 'Spree::TaxProvider::Internal')++ patch :update, params: { id: market.prefixed_id, tax_provider: '' }, as: :json++ expect(response).to have_http_status(:ok)+ expect(market.reload.tax_provider).to be_blank+ endendenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/tax_providers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/tax_providers_controller_spec.rbnew file mode 100644index 0000000000..215f78cf77--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/admin/tax_providers_controller_spec.rb@@ -0,0 +1,43 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Admin::TaxProvidersController, type: :controller do+ render_views++ include_context 'API v3 Admin authenticated'++ before { request.headers.merge!(headers) }++ describe 'GET #index' do+ it 'lists the built-in engine with the domains it cannot handle' do+ get :index, as: :json++ expect(response).to have_http_status(:ok)+ internal = json_response['data'].find { |provider| provider['id'] == 'Spree::TaxProvider::Internal' }+ expect(internal['name']).to eq('Internal')+ expect(internal['available']).to be(true)+ expect(internal['default']).to be(true)+ expect(internal['unsupported_capabilities']).to contain_exactly('us_local_tax', 'reverse_charge', 'oss_thresholds')+ end++ it 'reports an engine whose credentials are missing as unavailable' do+ stub_const('SpecUnconnectedProvider', Class.new(Spree::TaxProvider::Base) do+ def self.available_for_store?(_store)+ false+ end++ def self.unsupported_capabilities+ []+ end+ end)+ Spree.tax_providers << SpecUnconnectedProvider++ get :index, as: :json++ row = json_response['data'].find { |provider| provider['id'] == 'SpecUnconnectedProvider' }+ expect(row['available']).to be(false)+ expect(row['default']).to be(false)+ ensure+ Spree.tax_providers.delete(SpecUnconnectedProvider)+ end+ end+enddiff --git a/spree/core/app/models/concerns/spree/purchase/taxation.rb b/spree/core/app/models/concerns/spree/purchase/taxation.rbindex 117df1c69e..35111d28e3 100644--- a/spree/core/app/models/concerns/spree/purchase/taxation.rb+++ b/spree/core/app/models/concerns/spree/purchase/taxation.rb@@ -15,6 +15,15 @@ module Spree@tax_zone ||= Spree::Zone.match(tax_address) || Spree::Zone.default_taxend+ # The tax engine for this sale, chosen by its market. Falls back to the+ # store-wide default when the market names none, or when there is no+ # market yet (an empty cart before currency resolution).+ #+ # @return [Spree::TaxProvider::Base]+ def tax_provider+ market&.tax_provider_instance || Spree.tax_provider+ end+# The buyer's tax registration to compute against: a checkout-time# override first, then the customer's own. Nil means treat the sale as a# consumer sale, which is the legally safe default — charging normal taxdiff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rbindex d652d48b32..97f1c8b8ef 100644--- a/spree/core/app/models/spree/line_item.rb+++ b/spree/core/app/models/spree/line_item.rb@@ -389,7 +389,7 @@ module Spreedef update_tax_chargereturn unless owner- Spree.tax_provider.estimate(owner, [self], tax_date: Time.current)+ owner.tax_provider.estimate(owner, [self], tax_date: Time.current)enddef ensure_proper_currencydiff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rbindex 9690fc679e..bcb89f211b 100644--- a/spree/core/app/models/spree/market.rb+++ b/spree/core/app/models/spree/market.rb@@ -36,6 +36,12 @@ module Spreevalidates :preferred_return_window_days,numericality: { only_integer: true, greater_than: 0, allow_nil: true }validates :name, presence: true, uniqueness: { scope: spree_base_uniqueness_scope + [:store_id] }+ # An unregistered class name would only fail at checkout, when a customer is+ # waiting on a total. The list is read at validation time so a provider gem+ # loaded after boot still counts.+ validates :tax_provider,+ inclusion: { in: ->(_market) { Spree.tax_providers.map(&:to_s) } },+ allow_blank: truevalidates :currency, presence: truevalidates :default_locale, presence: truevalidates :countries, presence: true@@ -95,6 +101,16 @@ module Spree@tax_zone ||= Spree::Zone.match(default_country)end+ # The tax engine that computes for this market. A fresh instance per call:+ # providers are stateless and argless, so the market selects which class+ # rather than constructing state — everything request-specific arrives as an+ # argument to the provider's own methods.+ #+ # @return [Spree::TaxProvider::Base]+ def tax_provider_instance+ (tax_provider.presence || Rails.application.config.spree.tax_provider.to_s).constantize.new+ end+# Returns supported locales as an array, always including default_locale## @return [Array<String>]diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex afe0143320..904f5b7bc2 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -519,11 +519,11 @@ module Spree# Re-estimates tax through the configured provider (writes TaxLine rows# with replace-all semantics).def create_tax_charge!- Spree.tax_provider.estimate(self, tax_date: Time.current)+ tax_provider.estimate(self, tax_date: Time.current)enddef create_shipment_tax_charge!- Spree.tax_provider.estimate(self, fulfillments.to_a, tax_date: Time.current) if fulfillments.any?+ tax_provider.estimate(self, fulfillments.to_a, tax_date: Time.current) if fulfillments.any?enddef update_line_item_prices!diff --git a/spree/core/app/workflows/spree/carts/add_item.rb b/spree/core/app/workflows/spree/carts/add_item.rbindex 9185354e76..8909937dd2 100644--- a/spree/core/app/workflows/spree/carts/add_item.rb+++ b/spree/core/app/workflows/spree/carts/add_item.rb@@ -72,7 +72,7 @@ module Spree@line_item.reload.recalculate_price- ::Spree.tax_provider.estimate(@line_item.owner, [@line_item], tax_date: Time.current) if @line_item_created+ @line_item.owner.tax_provider.estimate(@line_item.owner, [@line_item], tax_date: Time.current) if @line_item_createdenddef handle_stock_reservationsdiff --git a/spree/core/app/workflows/spree/carts/complete.rb b/spree/core/app/workflows/spree/carts/complete.rbindex 52c5ef58b1..c40d6de4fd 100644--- a/spree/core/app/workflows/spree/carts/complete.rb+++ b/spree/core/app/workflows/spree/carts/complete.rb@@ -312,6 +312,15 @@ module Spreestep :complete_order, with: -> { Spree.order_complete_workflow }step :complete_cartstep :mark_coupon_codes_used+ external_step :commit_tax+ end++ # Tells the tax engine the sale is final. A no-op for the built-in+ # provider, whose rows are already the record; external engines file the+ # document that a tax return is later built from. Outside the+ # transaction — a remote call must not hold the completion open.+ def commit_tax+ order.tax_provider.commit(order)enddef complete_cartdiff --git a/spree/core/app/workflows/spree/carts/recalculate_totals.rb b/spree/core/app/workflows/spree/carts/recalculate_totals.rbindex 3e3f967de3..37db30fbdb 100644--- a/spree/core/app/workflows/spree/carts/recalculate_totals.rb+++ b/spree/core/app/workflows/spree/carts/recalculate_totals.rb@@ -58,7 +58,7 @@ module SpreeSpree.adjusters.each { |adjuster| adjuster.adjust(cart) }refresh_discount_and_fee_columns@tax_line_context = run_hooks :set_tax_line_context- Spree.tax_provider.estimate(+ cart.tax_provider.estimate(cart,tax_date: Time.current,exemptions: Spree.tax_resolve_exemptions_service.new.call(order: cart).value,diff --git a/spree/core/app/workflows/spree/orders/cancel.rb b/spree/core/app/workflows/spree/orders/cancel.rbindex f7626e928f..29da6fb622 100644--- a/spree/core/app/workflows/spree/orders/cancel.rb+++ b/spree/core/app/workflows/spree/orders/cancel.rb@@ -51,6 +51,7 @@ module Spreeendexternal_step :settle_payments+ external_step :void_taxstep :recompute_totals, with: -> { Spree.order_recalculate_totals_workflow }step :update_statuses, with: -> { Spree.order_update_statuses_service }order.publish_event('order.canceled', order.event_payload.merge(notify_customer: notify_customer))@@ -94,6 +95,12 @@ module Spree# Gateway I/O. Payments fully covered by a gift card are only voided,# never refunded; everything else cancels captured payments (void or# refund at the gateway's discretion) and voids what never completed.+ # Reverses the filed tax document. The canceled sale must stop appearing+ # in the merchant's tax liability.+ def void_tax+ order.tax_provider.void(order)+ end+def settle_paymentsif order.gift_card.present? && order.covered_by_store_credit?order.payments.completed.store_credits.each(&:void!)diff --git a/spree/core/app/workflows/spree/returns/refund.rb b/spree/core/app/workflows/spree/returns/refund.rbindex bc25d56631..452c6b8988 100644--- a/spree/core/app/workflows/spree/returns/refund.rb+++ b/spree/core/app/workflows/spree/returns/refund.rb@@ -38,6 +38,7 @@ module Spreestep :mark_refundedend+ external_step :refund_taxrun_hooks :after_refundreturn_record.publish_event('return.refunded')success(return_record.reload)@@ -45,6 +46,14 @@ module Spreeprivate+ # Credits the returned items against the filed tax document, keyed to the+ # original supply date rather than today's — the rate that applied then is+ # the rate to credit back.+ def refund_tax+ order = return_record.order+ order.tax_provider.refund(order, return_record.return_line_items.to_a, tax_date: order.completed_at)+ end+def internal_refund?refund_method.to_s == 'store_credit'enddiff --git a/spree/core/db/migrate/20260806000004_add_tax_provider_to_spree_markets.rb b/spree/core/db/migrate/20260806000004_add_tax_provider_to_spree_markets.rbnew file mode 100644index 0000000000..df87bc37c2--- /dev/null+++ b/spree/core/db/migrate/20260806000004_add_tax_provider_to_spree_markets.rb@@ -0,0 +1,9 @@+class AddTaxProviderToSpreeMarkets < ActiveRecord::Migration[8.1]+ def change+ # Which tax engine computes for this market, as a class name. A market is a+ # regional commerce operation, and the right engine is a regional question:+ # US sales tax needs a jurisdiction database, EU VAT is rate configuration.+ # Nil falls back to the store-wide default.+ add_column :spree_markets, :tax_provider, :string+ end+enddiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 360de3eecb..eef249694f 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -304,6 +304,19 @@ module SpreeRails.application.config.spree.tax_provider = valueend+ # Tax engines a market can be pointed at. Provider gems append their own, so a+ # merchant picks from what is actually installed rather than typing a class+ # name and finding out at checkout.+ #+ # @return [Array<Class>]+ def self.tax_providers+ Rails.application.config.spree.tax_providers+ end++ def self.tax_providers=(value)+ Rails.application.config.spree.tax_providers = value+ end+# Validator enforcing the password policy on the default auth models# ({Spree::Customer}, {Spree::AdminUser}). Defaults to# {Spree::PasswordLengthValidator}, which reads the configurable length bounds.diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rbindex 234a6695ad..ec76b2edf3 100644--- a/spree/core/lib/spree/core/engine.rb+++ b/spree/core/lib/spree/core/engine.rb@@ -11,6 +11,7 @@ module Spree:payment_methods,:adjusters,:tax_provider,+ :tax_providers,:password_validator,:fulfillment_providers,:fulfillment_types,@@ -175,6 +176,9 @@ module Spree# The sanctioned TaxLine writer (see docs/plans/6.0-tax-provider.md).Rails.application.config.spree.tax_provider = Spree::TaxProvider::Internal+ # Engines a market can select. Provider gems append their own.+ Rails.application.config.spree.tax_providers = [Spree::TaxProvider::Internal]+# Password policy for the default auth models. Swap for corporate rules,# breach-list lookups or entropy scoring.Rails.application.config.spree.password_validator = Spree::PasswordLengthValidatordiff --git a/spree/core/spec/models/spree/market_spec.rb b/spree/core/spec/models/spree/market_spec.rbindex 7d460bb081..62dd1f0e16 100644--- a/spree/core/spec/models/spree/market_spec.rb+++ b/spree/core/spec/models/spree/market_spec.rb@@ -275,4 +275,35 @@ RSpec.describe Spree::Market, type: :model doexpect(market.prefixed_id).to start_with('mkt_')endend++ describe '#tax_provider_instance' do+ let(:market) { create(:market, store: store) }++ it 'falls back to the store-wide default when the market names none' do+ expect(market.tax_provider).to be_nil+ expect(market.tax_provider_instance).to be_a(Spree::TaxProvider::Internal)+ end++ it 'builds the engine the market selected' do+ stub_const('SpecMarketProvider', Class.new(Spree::TaxProvider::Base))+ Spree.tax_providers << SpecMarketProvider++ market.update!(tax_provider: 'SpecMarketProvider')++ expect(market.tax_provider_instance).to be_a(SpecMarketProvider)+ ensure+ Spree.tax_providers.delete(SpecMarketProvider)+ end++ it 'builds a fresh instance per call, providers being stateless' do+ expect(market.tax_provider_instance).not_to equal(market.tax_provider_instance)+ end++ it 'refuses an engine this installation does not have' do+ market.tax_provider = 'MyApp::ImaginaryTaxProvider'++ expect(market).not_to be_valid+ expect(market.errors[:tax_provider]).to be_present+ end+ endenddiff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rbindex 89da96ecc0..16a090b742 100644--- a/spree/core/spec/models/spree/order_spec.rb+++ b/spree/core/spec/models/spree/order_spec.rb@@ -1351,7 +1351,8 @@ describe Spree::Order, type: :model dodescribe '#create_shipment_tax_charge!' dolet(:provider) { instance_double(Spree::TaxProvider::Internal, estimate: nil) }- before { allow(Spree).to receive(:tax_provider).and_return(provider) }+ # The order's own engine — selection is per-market since 6.0.+ before { allow(order).to receive(:tax_provider).and_return(provider) }context 'when order has shipments' dolet!(:shipment) { create(:shipment, order: order) }diff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rbindex 488c1b9aae..1758b3c12a 100644--- a/spree/core/spec/workflows/spree/carts/complete_spec.rb+++ b/spree/core/spec/workflows/spree/carts/complete_spec.rb@@ -85,6 +85,18 @@ module Spreeendend+ describe 'tax lifecycle' do+ it 'tells the tax engine the sale is final' do+ provider = instance_double(Spree::TaxProvider::Internal, estimate: nil, commit: nil)+ allow_any_instance_of(Spree::Order).to receive(:tax_provider).and_return(provider)+ allow_any_instance_of(Spree::Cart).to receive(:tax_provider).and_return(provider)++ order = described_class.call(cart: ready_cart).value++ expect(provider).to have_received(:commit).with(order)+ end+ end+describe 'tax identifier snapshot' dolet(:customer) { create(:user) }diff --git a/spree/core/spec/workflows/spree/carts/recalculate_totals_spec.rb b/spree/core/spec/workflows/spree/carts/recalculate_totals_spec.rbindex 215cf8d3be..17c983b6ec 100644--- a/spree/core/spec/workflows/spree/carts/recalculate_totals_spec.rb+++ b/spree/core/spec/workflows/spree/carts/recalculate_totals_spec.rb@@ -34,7 +34,11 @@ module Spreelet(:order) { create(:completed_order_with_totals, store: store) }it 'never regenerates typed rows but re-sums them (the post-placement resum path)' do- expect(Spree.tax_provider).not_to receive(:estimate)+ # The order's own provider, not the global default — selection is+ # per-market since 6.0, so stubbing the global would assert nothing.+ provider = instance_double(Spree::TaxProvider::Internal)+ allow(order).to receive(:tax_provider).and_return(provider)+ expect(provider).not_to receive(:estimate)line_item = order.line_items.firstorder.discounts.create!(line_item: line_item, label: 'Manual', amount: -3,diff --git a/spree/core/spec/workflows/spree/orders/cancel_spec.rb b/spree/core/spec/workflows/spree/orders/cancel_spec.rbindex 504c76efdb..85baa399bf 100644--- a/spree/core/spec/workflows/spree/orders/cancel_spec.rb+++ b/spree/core/spec/workflows/spree/orders/cancel_spec.rb@@ -48,6 +48,17 @@ module Spreeit_behaves_like 'tries to cancel'end+ describe 'tax lifecycle' do+ it 'reverses the filed tax document so the sale leaves the liability' do+ provider = instance_double(Spree::TaxProvider::Internal, void: nil, estimate: nil)+ allow(order).to receive(:tax_provider).and_return(provider)++ result++ expect(provider).to have_received(:void).with(order)+ end+ end+describe 'OrderCancellation record creation' doit 'creates a cancellation record with default reason' doexpect { result }.to change(order.cancellations, :count).by(1)diff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rbindex 12b318aed6..029fe7ef51 100644--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb@@ -171,6 +171,19 @@ RSpec.describe 'Spree::Returns workflows' doexpect(credit.amount).to eq(return_record.refund_total)end+ it 'credits the returned items against the filed tax document' 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)++ Spree::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+ )+ end+it 'lets a validate handler veto before any credit is issued' doSpree.hooks.register('returns.refund.validate') { |flow| flow.reject!('refunds on hold') }