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
Settle on one tax provider config, returning a class
Łukasz Adamczykfd5e98789e4a
diff --git a/docs/plans/6.0-core-rewrite-tasks.md b/docs/plans/6.0-core-rewrite-tasks.mdindex 62df3540db..fda1aa977a 100644--- a/docs/plans/6.0-core-rewrite-tasks.md+++ b/docs/plans/6.0-core-rewrite-tasks.md@@ -163,11 +163,11 @@ Runs after Wave 2 (tables born with `fulfillment_id`). Can proceed in parallel w### T3.4 Minimal TaxProvider (the sanctioned TaxLine writer)- [x] `Spree::TaxProvider::Base` (estimate required; commit/void/refund no-op stubs; `exempt?` default) + `Spree::TaxProvider::Internal` computing directly from TaxRate data (rate × discounted base; VAT included-in-price math) — **no `Calculator::DefaultTax` indirection**; zone matching stays as today (`TaxRate.match` over `Spree::Zone`) until the tax plan decouples it.- [x] Write contract: **replace-all set semantics** per estimate (delete target items' TaxLines, insert fresh — kills stale tax after address changes); internal rows keep `tax_rate_id` + snapshot `rate`/`label`/`provider_id: 'internal'`.-- [x] `Spree.tax_provider` seam (global default → Internal; per-Market resolution arrives with the full tax plan).+- [x] `Spree.tax_provider` seam (global default → Internal; per-Market resolution arrives with the full tax plan). *Renamed `Spree.default_tax_provider` and now returns a class, with the tax plan's per-market selection.*- [x] Removed: `TaxRate.adjust`, `AdjustmentSource`, `Calculator::DefaultTax`, `Adjuster::Tax`. TaxRate becomes pure configuration.### T3.5 Typed recalculation + consumer rewrite-- [x] `OrderUpdater`: two-pass preserved — discount/fee adjusters persist the discounted taxable base (+ `pre_tax_amount`), then `Spree.tax_provider.estimate`; totals via grouped SUMs from typed tables; all denormalized columns kept (incl. `taxable_adjustment_total`/`non_taxable_adjustment_total`) + new `fee_total`; freeze guard from T3.1.+- [x] `OrderUpdater`: two-pass preserved — discount/fee adjusters persist the discounted taxable base (+ `pre_tax_amount`), then the sale's tax provider `estimate` (`Spree.tax_provider` at the time; now `owner.tax_provider`); totals via grouped SUMs from typed tables; all denormalized columns kept (incl. `taxable_adjustment_total`/`non_taxable_adjustment_total`) + new `fee_total`; freeze guard from T3.1.- [x] Consumers rewritten off `source_type`/`eligible`: the ~12 Order readers (`order_level_promo_total`, `shipping_discount`, `valid_promotion_ids`, `cart_promo_total`, `has_free_shipping?`, `fully_discounted?`, `taxable_amount`, …), `Promotion#credits_count` (distinct orders with the promotion's discounts), coupon handlers, `CouponCodesHandler`, `OrderPromotion#amount`, cross-adjustable negative-total guard, `OrderMerger`, `Cart::Empty`, refund calculators (`DefaultRefundAmount` reads the line's own discounts).- [x] Removed: `Spree::Adjustment` model, `Adjustable::AdjustmentsUpdater`, `Adjustable::Adjuster::*`, `PromotionAccumulator` (dead). `spree_adjustments` table **kept read-only** through 6.0 (audit + rollback reference).- [x] CanCanCan: grants on TaxLine/Discount/Fee replace Adjustment grants.diff --git a/docs/plans/6.0-tax-provider.md b/docs/plans/6.0-tax-provider.mdindex 5a0d7f6fc0..d01584a515 100644--- a/docs/plans/6.0-tax-provider.md+++ b/docs/plans/6.0-tax-provider.md@@ -97,7 +97,7 @@ Provider (order lifecycle)|---|---|---|| `Spree::TaxProvider::Base` | `core/app/models/spree/tax_provider/base.rb` | Contract: `estimate(owner, items = nil)`, `commit(owner)`, `void(owner)`, `refund(owner, amount)`, `exempt?(owner)` (stub, uncalled — removed by this revision) || `Spree::TaxProvider::Internal` | `core/app/models/spree/tax_provider/internal.rb` | Computes rate × discounted basis directly from TaxRate rows (VAT included-in-price math backed out of gross); replace-all `delete_all` per estimate; writes snapshot columns; preserves `pre_tax_amount`; **still Zone-based** (`TaxRate.match(owner.tax_zone)`); skips zero amounts (changed by this revision) |-| `Spree.tax_provider` | `core/lib/spree/core.rb` | Returns a fresh argless instance of the globally configured class (`Rails.application.config.spree.tax_provider`, default Internal). Per-market selection not yet implemented |+| `Spree.default_tax_provider` | `core/lib/spree/core.rb` | Returns the globally configured fallback **class** (default Internal); callers say `.new`. Renamed from `Spree.tax_provider`, which returned an instance and implied it was the only provider — selection is per-market, through `Spree::Purchase::Taxation#tax_provider` || `Spree::TaxLine` | `core/app/models/spree/tax_line.rb` + `spree_tax_lines` | Typed adjustment row: dual-FK cart/order owner, exactly-one adjustable (line item / fulfillment / fee), `amount`, `rate` + `label` + `provider_id` snapshots, `included`, `data` + `metadata` JSON || Order-level money freeze | `core/app/workflows/spree/carts/recalculate_totals.rb` | `money_frozen?` (completed order) skips `regenerate_typed_rows` — no adjusters, no estimate; `fold_typed_rows` always re-sums totals from rows || `set_tax_line_context` hook | same workflow | Context hook assembled before estimate; **not yet threaded into the provider call** (see Key Decisions — its role narrows to untyped provider extras) |@@ -116,7 +116,7 @@ Carts::RecalculateTotals#perform (single totals seam, cart + order twins)→ regenerate_typed_rows unless money_frozen?→ Spree.adjusters (discounts/fees on the taxable base)→ run_hooks :set_tax_line_context- → Spree.tax_provider.estimate(cart) # replace-all TaxLine write+ → cart.tax_provider.estimate(cart) # replace-all TaxLine write→ fold_typed_rows always — totals re-summed from rows```@@ -372,7 +372,7 @@ Multiple certificates = multiple entries. Certificate *lifecycle* (verified/expi### Provider is per-Market — selection, not construction-Markets represent regional commerce operations; each market selects its tax engine. The global config class (`Rails.application.config.spree.tax_provider`) stays as the **fallback default** (market with no explicit provider, store without markets).+Markets represent regional commerce operations; each market selects its tax engine. `Spree.default_tax_provider` stays as the **fallback** (market with no explicit provider, store without markets) — one config, returning a class, so a caller that only needs to name the default does not instantiate one.```rubyclass Spree::Market < Spree.base_class@@ -384,12 +384,12 @@ class Spree::Market < Spree.base_class# a provider needs (credentials, rates). The market selects WHICH class; it is# not constructor state.def tax_provider_instance- (tax_provider.presence || Rails.application.config.spree.tax_provider.to_s).constantize.new+ (tax_provider.presence || Spree.default_tax_provider.to_s).constantize.newendend```-Call sites migrate from `Spree.tax_provider` to the owner's market as per-market selection lands. Provider availability in the Market admin depends on Integration status (`available_for_store?`) and the capability declarations below. The registry shape from the 2026-07-30 note above — a small model per engine, identifier plus enabled — stays an option to weigh at implementation; `TaxLine.provider_id` keeps stamping its plain string key either way.+Call sites migrate from the global default to the owner's market as per-market selection lands — they call `owner.tax_provider`, which falls back to `Spree.default_tax_provider`. Provider availability in the Market admin depends on Integration status (`available_for_store?`) and the capability declarations below. The registry shape from the 2026-07-30 note above — a small model per engine, identifier plus enabled — stays an option to weigh at implementation; `TaxLine.provider_id` keeps stamping its plain string key either way.TaxRate is an internal-provider concept only. External providers ignore it — they have their own jurisdiction databases.diff --git a/spree/core/app/models/concerns/spree/purchase/taxation.rb b/spree/core/app/models/concerns/spree/purchase/taxation.rbindex 4557dbe93c..bffc4e6f00 100644--- a/spree/core/app/models/concerns/spree/purchase/taxation.rb+++ b/spree/core/app/models/concerns/spree/purchase/taxation.rb@@ -30,7 +30,7 @@ module Spree## @return [Spree::TaxProvider::Base]def tax_provider- market&.tax_provider_instance || Spree.tax_provider+ market&.tax_provider_instance || Spree.default_tax_provider.newend# The typed inputs every +estimate+ call carries. Assembled here ratherdiff --git a/spree/core/app/models/spree/adjusters/base.rb b/spree/core/app/models/spree/adjusters/base.rbindex ee36107467..80d9b28a3f 100644--- a/spree/core/app/models/spree/adjusters/base.rb+++ b/spree/core/app/models/spree/adjusters/base.rb@@ -4,7 +4,7 @@ module Spree# invoked once per order recalculation and owns a family of typed rows# (Discount or Fee) — it writes, refreshes and removes them so the rows# always reflect the order's current state. Tax is not an adjuster; it is- # written by +Spree.tax_provider+ after all adjusters ran.+ # written by the sale's tax provider after all adjusters ran.class Basedef self.adjust(order)new(order).updatediff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rbindex 3c32298fbe..ef3b37b8b5 100644--- a/spree/core/app/models/spree/market.rb+++ b/spree/core/app/models/spree/market.rb@@ -108,7 +108,7 @@ module Spree## @return [Spree::TaxProvider::Base]def tax_provider_instance- (tax_provider.presence || Spree.default_tax_provider_class.to_s).constantize.new+ (tax_provider.presence || Spree.default_tax_provider.to_s).constantize.newend# Returns supported locales as an array, always including default_localediff --git a/spree/core/app/models/spree/tax_line.rb b/spree/core/app/models/spree/tax_line.rbindex 82faf314b1..77f9b45f04 100644--- a/spree/core/app/models/spree/tax_line.rb+++ b/spree/core/app/models/spree/tax_line.rb@@ -1,6 +1,6 @@module Spree# A single tax charge on a line item, fulfillment or fee. Written exclusively- # by the tax provider (see Spree.tax_provider) with replace-all set semantics+ # by the tax provider (see Spree::Purchase::Taxation#tax_provider) with replace-all set semantics# per estimate. Snapshot columns (+rate+, +label+, +provider_id+) keep rows# self-describing after a TaxRate is deleted or when an external provider# computed them.diff --git a/spree/core/app/models/spree/tax_provider/base.rb b/spree/core/app/models/spree/tax_provider/base.rbindex 05d7dab948..ef582f6853 100644--- a/spree/core/app/models/spree/tax_provider/base.rb+++ b/spree/core/app/models/spree/tax_provider/base.rb@@ -65,7 +65,7 @@ module Spreename: display_name,available: available_for_store?(store),unsupported_capabilities: unsupported_capability_details,- default: name == Spree.default_tax_provider_class.to_s+ default: name == Spree.default_tax_provider.to_s}enddiff --git a/spree/core/app/models/spree/tax_rate.rb b/spree/core/app/models/spree/tax_rate.rbindex 7415c02118..9b39a1dda1 100644--- a/spree/core/app/models/spree/tax_rate.rb+++ b/spree/core/app/models/spree/tax_rate.rb@@ -1,7 +1,7 @@module Spree# Pure tax configuration read by the internal tax provider# (Spree::TaxProvider::Internal). Since 6.0 tax rates have no write path of- # their own — TaxLine rows are written exclusively by Spree.tax_provider.+ # their own — TaxLine rows are written exclusively by the sale's tax provider.class TaxRate < Spree.base_classhas_prefix_id :taxdiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 002fd20711..f7780d05b6 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -291,27 +291,26 @@ module SpreeRails.application.config.spree.adjusters = valueend- # The configured tax provider instance — the only sanctioned writer of- # {Spree::TaxLine} rows. Global default is {Spree::TaxProvider::Internal};- # per-Market resolution arrives with docs/plans/6.0-tax-provider.md.+ # The tax engine used when a market names none — the fallback behind+ # {Spree::Purchase::Taxation#tax_provider}, which is what call sites actually+ # use. Defaults to {Spree::TaxProvider::Internal}.#- # @return [Spree::TaxProvider::Base]- def self.tax_provider- Rails.application.config.spree.tax_provider.new- end-- # The store-wide default engine as a class, for callers that need to name it- # rather than use it — market selection constantizing its own choice, and the- # admin marking which entry is the default. Instantiating just to read a class- # name is what this avoids.+ # Spree.default_tax_provider = SpreeTaxAvalara::TaxProvider+ #+ # Returns the class rather than an instance: callers that want one say `.new`,+ # and the ones that only need to name it (market selection constantizing its+ # own choice, the admin marking which entry is the default) do not pay for an+ # object they discard. A String or Symbol is accepted and constantized, so an+ # initializer can name a provider before its class is autoloaded.## @return [Class]- def self.default_tax_provider_class- Rails.application.config.spree.tax_provider+ def self.default_tax_provider+ provider = Rails.application.config.spree.default_tax_provider+ provider.is_a?(Class) ? provider : provider.to_s.constantizeend- def self.tax_provider=(value)- Rails.application.config.spree.tax_provider = value+ def self.default_tax_provider=(value)+ Rails.application.config.spree.default_tax_provider = valueend# Tax engines a market can be pointed at. Provider gems append their own, so adiff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rbindex ec76b2edf3..ce7a27a060 100644--- a/spree/core/lib/spree/core/engine.rb+++ b/spree/core/lib/spree/core/engine.rb@@ -10,7 +10,7 @@ module Spree:dependencies,:payment_methods,:adjusters,- :tax_provider,+ :default_tax_provider,:tax_providers,:password_validator,:fulfillment_providers,@@ -173,8 +173,8 @@ module SpreeSpree::Adjusters::Promotion]- # The sanctioned TaxLine writer (see docs/plans/6.0-tax-provider.md).- Rails.application.config.spree.tax_provider = Spree::TaxProvider::Internal+ # The fallback engine when a market names none (see docs/plans/6.0-tax-provider.md).+ Rails.application.config.spree.default_tax_provider = Spree::TaxProvider::Internal# Engines a market can select. Provider gems append their own.Rails.application.config.spree.tax_providers = [Spree::TaxProvider::Internal]diff --git a/spree/core/spec/models/spree/tax_provider/base_spec.rb b/spree/core/spec/models/spree/tax_provider/base_spec.rbindex acce93d583..c1f96001b1 100644--- a/spree/core/spec/models/spree/tax_provider/base_spec.rb+++ b/spree/core/spec/models/spree/tax_provider/base_spec.rb@@ -88,6 +88,26 @@ describe Spree::TaxProvider::Base, type: :model doendend+ describe 'Spree.default_tax_provider' do+ it 'is the internal provider out of the box' do+ expect(Spree.default_tax_provider).to eq(Spree::TaxProvider::Internal)+ end++ it 'returns a class, so a caller that only names it pays for no instance' do+ expect(Spree.default_tax_provider).to be_a(Class)+ end++ it 'accepts a class name, for an initializer naming a provider before autoload' do+ original = Rails.application.config.spree.default_tax_provider+ Spree.default_tax_provider = 'Spree::TaxProvider::Internal'++ expect(Spree.default_tax_provider).to eq(Spree::TaxProvider::Internal)+ expect(Spree.default_tax_provider.new).to be_a(Spree::TaxProvider::Internal)+ ensure+ Spree.default_tax_provider = original+ end+ end+describe 'the internal provider' doit 'declares the domains rate configuration cannot express' doexpect(Spree::TaxProvider::Internal.unsupported_capabilities).to(