Overview
A tax provider decides how a market’s tax is worked out. Spree ships withSpree::TaxProvider::Internal, which prices from the tax rates you set up in the admin, and with Avalara AvaTax as the spree_avalara gem. Each market chooses its provider, so one store can use its own rates in a simple market and a tax service in a complicated one.
Write a provider when a service outside Spree should calculate the tax: a tax engine, an ERP, an in-house rules service. spree_avalara (spree/providers/avalara in the Spree repository) is a complete provider built on this contract, and the best place to see each piece working.
See Taxes for how tax lines, tax categories and markets fit together.
What you will build
The provider is a stateless strategy object. It never stores credentials: those belong to the integration, which is what gives merchants one place to see everything connected to their store.
1. Store credentials in an integration
:password preferences: Spree masks them on read and keeps the stored value when the masked one comes back.
can_connect? runs when an admin activates the integration or clicks Test connection. Return false with a connection_error_message and activation is refused with your message.
Record calls to your service
call_log? = true tells the admin that this integration keeps a call log: the integration’s sheet in the dashboard shows its latest calls, and its call log page lists them all. A call’s request and response are shown only to staff who also hold read_orders, because they carry what your service was sent; override self.call_log_body_permission when another permission covers that data. Write each call with log_call:
Spree::Config.integration_log_retention_days (30 by default) by Spree::Integrations::PurgeLogsJob, which the host app schedules daily.
2. Implement the provider
The contract
Every method below is defined onSpree::TaxProvider::Base. Core calls them; you never call them yourself.
commit, void, refund and commit_replacement do nothing by default, which is right for a provider whose rows are the whole record. estimate, estimate_refund and estimate_replacement raise until you implement them.
Rules for estimate
- Rewrite everything you delete.
itemsnames what changed, but a service that prices a whole document may rewrite every row on the owner, provided it writes a row for each item it swept. Delete only rows whoseprovider_idis yours. - Write a row for every item you formed a treatment for, zero-amount ones included: a zero-rated line says why it is zero in
taxability_reason. - Follow
tax_inclusive:. It says whether the market’s prices include tax. Pass it to your service and mark the rowsincludedto match. When it is nil, ask the owner (owner.tax_inclusive?). Set each item’spre_tax_amounttoo, to its price less any included tax, asSpree::TaxProvider::Internaldoes: the order’s pre-tax totals and product revenue read it. - Fail closed, but only when the service fails. Raise
Spree::Tax::ProviderUnavailablewhen your service could not answer (timeout, outage, rate limit): core refuses the checkout and the customer can retry. RaiseSpree::Tax::CalculationRefusedwhen it answered no (an address it will not accept): core shows your message so the customer can correct it. Anything else reaches the customer as a broken checkout. - Have no opinion when disconnected. With no active integration, sweep your rows and return. Report the misconfiguration to your error tracker if you like, but never block a cart because a merchant switched an integration off.
estimateis never called on a placed order. The order’s money is frozen once it is placed.
Refunds
The customer is owed the tax they paid.Spree::TaxProvider::RecordedShare answers each returned unit’s share of the tax the sale actually charged, which is what spree_avalara uses:
tax_date). If your service cannot be reached, raise Spree::Tax::ProviderUnavailable: the return is refused until it can, rather than refunding tax nobody worked out.
Optional declarations
These are class methods, read while the admin presents the provider.3. Register both classes
after_initialize: Spree sets up its own lists after your initializers run, so an entry added at the top level of the file would be dropped. A gem registers from its engine’s config.after_initialize block in the same way.
The provider’s API name comes from its module: SpreeAcmeTax::TaxProvider is acme_tax. That is the value a market’s tax_provider takes, and what GET /api/v3/admin/tax_providers lists.

