Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Let each tax provider describe itself to the admin

Łukasz Adamczykc41cfcaf0b9d
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.rb
index c8aa9d688c..62941174b6 100644
--- 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.rb
@@ -4,7 +4,9 @@ module Spree
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.
+ # classes registered in code, not rows — so there is no serializer, and
+ # each class describes itself (`to_api_hash`), the same shape
+ # `/promotion_rules/types` and `/collection_rules/types` use.
#
# 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
@@ -14,19 +16,7 @@ module Spree
# 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
- }
+ render json: { data: Spree.tax_providers.map { |provider| provider.to_api_hash(current_store) } }
end
end
end
diff --git a/spree/core/app/models/spree/tax_provider/base.rb b/spree/core/app/models/spree/tax_provider/base.rb
index 10b2cefb42..843d442123 100644
--- a/spree/core/app/models/spree/tax_provider/base.rb
+++ b/spree/core/app/models/spree/tax_provider/base.rb
@@ -28,6 +28,31 @@ module Spree
true
end
+ # The name a merchant sees when choosing an engine. Override in a provider
+ # gem — a class name mangled into words rarely matches what the service is
+ # actually called.
+ #
+ # @return [String]
+ def self.display_name
+ name.demodulize.titleize
+ end
+
+ # How this engine describes itself to the admin, mirroring
+ # Spree::PreferenceSchema#subclasses_with_preference_schema: the class
+ # being described owns its own presentation, not the controller.
+ #
+ # @param store [Spree::Store]
+ # @return [Hash]
+ def self.to_api_hash(store)
+ {
+ id: name,
+ name: display_name,
+ available: available_for_store?(store),
+ unsupported_capabilities: unsupported_capabilities.map(&:to_s),
+ default: name == Rails.application.config.spree.tax_provider.to_s
+ }
+ end
+
# Recomputes tax for the given items and writes the TaxLine rows.
# Replace-all set semantics per target item, and one row for every item
# the provider formed a treatment for — zero-amount treatments included.
diff --git a/spree/core/db/migrate/20260806000003_create_spree_tax_identifiers.rb b/spree/core/db/migrate/20260806000003_create_spree_tax_identifiers.rb
index d7ebb3e382..146622c26a 100644
--- a/spree/core/db/migrate/20260806000003_create_spree_tax_identifiers.rb
+++ b/spree/core/db/migrate/20260806000003_create_spree_tax_identifiers.rb
@@ -34,6 +34,10 @@ class CreateSpreeTaxIdentifiers < ActiveRecord::Migration[8.1]
t.timestamps
end
- add_index :spree_tax_identifiers, [:customer_id, :kind]
+ # One registration per kind for a customer. Cart and order rows leave
+ # customer_id null, and NULLs never collide in a unique index, so this
+ # constrains the profile rows only — which is where duplicates would make
+ # resolution ambiguous.
+ add_index :spree_tax_identifiers, [:customer_id, :kind], unique: true
end
end
diff --git a/spree/core/spec/models/spree/tax_provider/base_spec.rb b/spree/core/spec/models/spree/tax_provider/base_spec.rb
index 481e1611a3..e41fcbaf21 100644
--- a/spree/core/spec/models/spree/tax_provider/base_spec.rb
+++ b/spree/core/spec/models/spree/tax_provider/base_spec.rb
@@ -42,6 +42,29 @@ describe Spree::TaxProvider::Base, type: :model do
end
end
+ describe 'self-description for the admin' do
+ it 'presents id, name, availability and limits' do
+ expect(Spree::TaxProvider::Internal.to_api_hash(order.store)).to eq(
+ id: 'Spree::TaxProvider::Internal',
+ name: 'Internal',
+ available: true,
+ unsupported_capabilities: %w[us_local_tax reverse_charge oss_thresholds],
+ default: true
+ )
+ end
+
+ it 'lets a provider gem name itself rather than wear a mangled class name' do
+ stub_const('SpreeTaxAvalara::TaxProvider', Class.new(described_class) do
+ def self.display_name
+ 'Avalara AvaTax'
+ end
+ end)
+
+ expect(SpreeTaxAvalara::TaxProvider.to_api_hash(order.store)[:name]).to eq('Avalara AvaTax')
+ expect(SpreeTaxAvalara::TaxProvider.to_api_hash(order.store)[:default]).to be(false)
+ end
+ end
+
describe 'the internal provider' do
it 'declares the domains rate configuration cannot express' do
expect(Spree::TaxProvider::Internal.unsupported_capabilities).to(