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 merchants register a business's tax number

Łukasz Adamczyk35249ab6e645
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies/tax_identifiers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies/tax_identifiers_controller.rb
new file mode 100644
index 0000000000..fc351de410
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/companies/tax_identifiers_controller.rb
@@ -0,0 +1,63 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ module Companies
+ # A business's own tax registration — the number that goes on its
+ # invoices, which outranks the buyer's own when the two differ.
+ #
+ # Mirrors the customer registrations endpoint, including manual
+ # re-validation: a registry answers only "valid now", so a number
+ # verified last year may have been deregistered since.
+ class TaxIdentifiersController < BaseController
+ before_action :authorize_parent_access!
+ # Re-declaring the filter replaces the inherited options, so the
+ # standard actions have to be listed alongside the custom one.
+ before_action :set_resource, only: [:show, :update, :destroy, :validate]
+
+ # POST /api/v3/admin/companies/:company_id/tax_identifiers/:id/validate
+ def validate
+ authorize_resource!(@resource, :update)
+
+ unless @resource.validatable?
+ render_error(
+ code: 'tax_id_not_validatable',
+ message: "No validator is registered for tax identifier kind '#{@resource.kind}'",
+ status: :unprocessable_content
+ )
+ return
+ end
+
+ Spree::TaxIdentifiers::ValidateJob.perform_later(@resource.id)
+ @resource.update_columns(validation_status: 'pending', validated_at: nil, updated_at: Time.current)
+
+ render json: serialize_resource(@resource.reload), status: :accepted
+ end
+
+ protected
+
+ def model_class
+ Spree::TaxIdentifier
+ end
+
+ def serializer_class
+ Spree.api.admin_tax_identifier_serializer
+ end
+
+ def scope
+ @parent.tax_identifiers
+ end
+
+ def parent_association
+ :tax_identifiers
+ end
+
+ def permitted_params
+ params.permit(:kind, :value)
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rb
index db504a5a96..799a5b0be5 100644
--- a/spree/api/config/routes.rb
+++ b/spree/api/config/routes.rb
@@ -405,6 +405,14 @@ Spree::Core::Engine.add_routes do
resources :companies do
resources :locations, controller: 'companies/locations', only: [:index, :show, :create]
+ # The business's own registration — the number on its invoices, which
+ # outranks the buyer's own. Same shape as the customer's.
+ resources :tax_identifiers, controller: 'companies/tax_identifiers' do
+ member do
+ post :validate
+ end
+ end
+
# Exemption evidence. Accepting or withdrawing one is its own action —
# never mass assignment — and a verified certificate is revoked
# rather than deleted.
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb
new file mode 100644
index 0000000000..e84279b6b3
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb
@@ -0,0 +1,116 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::Companies::TaxIdentifiersController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:company) { create(:company, store: store) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'lists the business registrations' do
+ create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat', value: 'DE123456789')
+
+ get :index, params: { company_id: company.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ row = json_response['data'].first
+ expect(row['id']).to start_with('txi_')
+ expect(row['kind']).to eq('eu_vat')
+ expect(row['value']).to eq('DE123456789')
+ end
+
+ it '404s under another store company' do
+ get :index, params: { company_id: create(:company, store: create(:store)).prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe 'POST #create' do
+ # Phase 3 made a business registration outrank the buyer's; without this
+ # endpoint that behaviour was only reachable from a console.
+ it 'registers a number against the business' do
+ post :create, params: { company_id: company.prefixed_id, kind: 'eu_vat', value: 'DE123456789' }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['value']).to eq('DE123456789')
+ expect(company.tax_identifiers.sole.kind).to eq('eu_vat')
+ end
+
+ it 'takes effect on a sale for that business' do
+ create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat', value: 'DE222222222')
+ location = create(:company_location, company: company)
+ customer = create(:customer)
+ create(:tax_identifier, customer: customer, kind: 'eu_vat', value: 'DE111111111')
+ cart = create(:cart, store: store, customer: customer, company_location: location)
+
+ expect(cart.resolved_tax_identifier.value).to eq('DE222222222')
+ end
+
+ it 'holds one registration per kind' do
+ create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat', value: 'DE123456789')
+
+ post :create, params: { company_id: company.prefixed_id, kind: 'eu_vat', value: 'DE999999999' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
+ it 'requires a kind and a value' do
+ post :create, params: { company_id: company.prefixed_id, kind: 'eu_vat' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
+ # Format is only checked when a validator is registered for the kind; a
+ # stock install registers none, so anything well-formed enough is accepted.
+ it 'checks the format when a validator is installed' do
+ validator = Class.new(Spree::TaxIdentifiers::Validator::Base) do
+ def self.valid_format?(value) = value.to_s.start_with?('DE')
+ end
+ stub_const('SpecEuVatValidator', validator)
+ Spree.tax_identifier_validators['eu_vat'] = 'SpecEuVatValidator'
+
+ post :create, params: { company_id: company.prefixed_id, kind: 'eu_vat', value: '!!' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ ensure
+ Spree.tax_identifier_validators.delete('eu_vat')
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'corrects the number' do
+ identifier = create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat', value: 'DE123456789')
+
+ patch :update, params: { company_id: company.prefixed_id, id: identifier.prefixed_id, value: 'DE987654321' }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(identifier.reload.value).to eq('DE987654321')
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'removes the registration' do
+ identifier = create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat')
+
+ delete :destroy, params: { company_id: company.prefixed_id, id: identifier.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:no_content)
+ expect(company.tax_identifiers.reload).to be_empty
+ end
+ end
+
+ describe 'POST #validate' do
+ it 'reports when no validator is installed for the kind' do
+ identifier = create(:tax_identifier, customer: nil, company: company, kind: 'eu_vat')
+
+ post :validate, params: { company_id: company.prefixed_id, id: identifier.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['code']).to eq('tax_id_not_validatable')
+ end
+ end
+end