Skip to main content

Overview

A tax provider decides how a market’s tax is worked out. Spree ships with Spree::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

Declare secrets as :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:
The row is written once the caller’s database transaction has ended, committed or rolled back, so a call that fails inside a checkout transaction is still recorded. A failure to write is reported and swallowed: the log never breaks the call it records. Never record credentials, and record only while the merchant has switched logging on, because tax requests carry customer addresses. Rows are deleted after 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 on Spree::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. items names 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 whose provider_id is 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 rows included to match. When it is nil, ask the owner (owner.tax_inclusive?). Set each item’s pre_tax_amount too, to its price less any included tax, as Spree::TaxProvider::Internal does: the order’s pre-tax totals and product revenue read it.
  • Fail closed, but only when the service fails. Raise Spree::Tax::ProviderUnavailable when your service could not answer (timeout, outage, rate limit): core refuses the checkout and the customer can retry. Raise Spree::Tax::CalculationRefused when 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.
  • estimate is 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:
Compute it yourself only when your service must price the credit, for example at the rates in force on the day of the sale (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

Register inside 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.

4. Use it

A merchant connects the integration under Settings → Integrations, then opens a market under Settings → Markets and picks your provider as its Tax engine. Markets left alone keep the store’s default provider.

Testing

Test the provider directly, with your service’s client stubbed:
To check your request and response shapes against the real service, record its answers once against the vendor’s sandbox, for example with VCR, and replay them in CI. Record them rather than writing them by hand, so the tests prove what the service actually answers.