# Sales tax

Every tax your account charges is calculated one of two ways, chosen separately for test
mode and live mode:

- **Your own tax rates** (the default). The rates you create with `POST /v1/tax_rates` or
  in the dashboard: a global rate, rates attached to a product, rates for a country, state
  or city, and a default rate. See [Products: Tax rates](./05-products.md#tax-rates).
- **Anrok**, from your own Anrok account. Connect it in **Dashboard → Settings →
  Integrations → Sales tax**: paste your Anrok API key, test the connection, then switch it
  on. While Anrok is switched on for a mode, it calculates every tax in that mode and none
  of your own tax rates are used, not even while Anrok is down.

`POST /v1/tax/calculate` says which one answered: `automatic_tax` is `true` for Anrok and
`false` for your own rates.

With Anrok, each product is taxed by the tax category you pick for it in the dashboard (the
product's page). A product without one, and a line with no product, uses your default
category.

---

## What is taxed

| Where the payment is taken | Taxed? | The address Anrok prices |
|---|---|---|
| A store checkout (a storefront cart) | Yes, from the moment the checkout opens, and again when the address changes | The shipping address, else the billing address |
| A payment link, including one made with `POST /v1/checkout-links` | Yes: the link's `amount` plus the tax. A `DONATION` link never | The billing address, which the page then asks every buyer for |
| A subscription: its first period, every renewal, and the invoices made for them | Yes, unless tax collection is turned off on the subscription | The customer's saved address, else the address of their latest checkout |
| An invoice you create in the dashboard | Yes, when it is finalized (sent). The dashboard offers no tax rate of its own on the invoice | The customer's saved address, else the address of their latest checkout |
| A payment intent (Payment Element) | **Never.** The intent's `amount` is the price | None |
| `POST /v1/charges` | **Never.** Your server sets the amount | None |
| `POST /v1/tax/calculate` | Calculates only; nothing is charged | The `address` you send |

With your own rates, the location rates use the shipping address, and a global or default
rate applies with no address at all.

To charge tax on a payment intent or on `POST /v1/charges`, calculate it first with
`POST /v1/tax/calculate` and include it in `amount`.

On a payment link or a store checkout with Anrok on, the hosted page asks every buyer for a
billing address, even when the link does not collect one. As soon as the address is
complete, the order summary and the Pay button show the tax, before the buyer presses Pay.

---

## The address Anrok needs

An address Anrok can price has:

- a 2-letter country code, such as `US`;
- a postal code; in the US it must start with a 5-digit ZIP code (`98101` or `98101-1234`);
- a state or province code in the US and Canada, such as `WA`.

The city is optional. With anything less, Anrok is not asked, and:

- `POST /v1/tax/calculate` answers `200` with zero tax on every line (`amount_tax: 0`, an
  empty `tax_breakdown`) and `automatic_tax: true`. Do not show a final total until the
  buyer has entered an address.
- A hosted checkout shows no tax yet and will not charge. If the payment is attempted
  anyway, the buyer sees "Enter the country, state and ZIP code so sales tax can be
  calculated." and nothing is charged.
- A subscription period is charged without tax. Save an address on the customer in the
  dashboard (Customers) before its next charge.
- An invoice you create in the dashboard cannot be finalized, and stays a draft: the
  dashboard says Anrok needs the customer's address. Save an address on the customer, then
  finalize the invoice again. An invoice is never sent without its tax.

Exemption certificates you keep in Anrok for a customer apply when the sale names that
customer, as a subscription's charges do. `POST /v1/tax/calculate` takes no customer, so
they do not apply to it.

---

## When Anrok cannot answer

| What happened | `POST /v1/tax/calculate` | Hosted checkout and payment link |
|---|---|---|
| Anrok could not resolve the address | `400` `invalid_argument`, `message`: "Sales tax could not be calculated for this address. Check the address and try again." Correct the address and call again | The buyer sees the same message. Nothing is charged |
| Anrok is busy, or did not answer this request while it answers others | `503` `upstream_error`. Nothing was written; retry after a moment | The checkout cannot take the payment just then and the buyer tries again. Nothing is charged |
| Anrok is down, or your Anrok setup is broken (a rejected API key, a product ID your Anrok account does not have) | `200`, with Anrok's last answer for the same ZIP code and tax category, else for the same state, else zero tax. `automatic_tax` stays `true` | The same tax, and the payment goes through: a payment never fails because Anrok is down |

Sales charged without Anrok's own answer (Anrok down, or no address) are listed in
**Dashboard → Finance → Tax reports**, under "Needs your attention", with what to fix.

See [Errors](./10-errors.md) for the error envelope.

---

## Where the tax shows up

| Where | Field |
|---|---|
| `POST /v1/tax/calculate` | `amount_tax` and `tax_breakdown` on each line, `tax_amount_exclusive` / `tax_amount_inclusive` / `amount_total`, and `automatic_tax` |
| `GET /v1/charges/{id}`, a charge taken by a hosted checkout, a payment link, a store cart or a hosted invoice | `amount_details.tax`: see [Charges](./03-charges.md) |
| The `checkout.session.completed` and `payment_link.completed` webhooks | `amountDetails.taxAmount`: see [Events](./19-events.md) |
| `GET /v1/invoices` and `GET /v1/invoices/{id}` | `tax`, beside `subtotal` and `total`: see [Invoices](./18c-invoices.md) |
| The `invoice.*` webhooks | `detail.taxAmount`: see [Webhooks](./16-webhooks.md) |
| The buyer's receipt (its page, email and PDF), the hosted invoice page and its PDF | Each tax by its jurisdiction and rate, such as "Washington 6.5%" and "Seattle 3.85%", when they are known; a single Tax amount otherwise |

An invoice created in the dashboard carries its tax in `tax` and `total`, and in the
`invoice.*` webhooks, from the moment it is finalized: a draft carries none.

A checkout charge of 150.00 plus 10.55% Washington sales tax reads:

```json
"amount": 16583,
"amount_details": { "subtotal": 15000, "discount": 0, "shipping": 0, "tax": 1583, "tax_included": 0 }
```

With Anrok, a `tax_breakdown` row on `POST /v1/tax/calculate` is one jurisdiction Anrok
named: `tax_rate` is `null` (no rate of yours applied), `display_name` and `jurisdiction`
are the jurisdiction, and `city` is `null`:

```json
{ "tax_rate": null, "display_name": "Denver (local)", "tax_type": "SALES_TAX",
  "percentage": "4.81", "jurisdiction": "Denver (local)", "country": "US", "state": "CO",
  "city": null, "inclusive": false, "taxable_amount": 15000, "amount": 722 }
```

Paid sales on Anrok are also recorded in your Anrok account, with their refunds, voids
and lost disputes, so your Anrok returns include them.
