Skip to main content

Charge tax with your own rules

What you will build​

You set the tax rate of each country your customers live in, say whether your prices include tax, and read what Black charges and records for a customer in each country. You learn how Black finds the rate, how it works out and rounds the tax, which figures every invoice and event carries, and what stays with you: the tax invoice itself.

Before you start​

You need an access token and a product with a recurring price. See Authentication. Amounts are whole numbers of the currency's smallest unit, so 4999 is SAR 49.99.

You collect the money from your own customer, as in Create a subscription and report its payments. Black works out the tax and records it on each invoice. It never collects tax and never pays it to a tax authority.

The samples are a vendor's. They create a sub-account and set its tax settings, because only the vendor changes the tax mode and whether prices include tax.

Steps​

  1. Choose the tax mode. Send a PATCH request to /v1/settings with tax_mode set to rules. The response holds the settings, with tax_mode rules.

  2. Say whether your prices include tax. Send prices_include_tax as true or false in the same request or a later one. With true, the price you set is what the customer pays, and the tax is a part of it. With false, Black adds the tax to the price.

  3. Set the rate of each country. Send vat_rates with a country code in upper case and a rate for each country, as {"SA":15}. Send vat_default_rate for the rate of every other country. The response holds each rate as a string with two decimals.

  4. Read what a customer in a country pays. Send a GET request to /v1/plans with country set to the customer's country code, as ?country=SA. Each plan holds a tax_quote with the price, the tax, the price without tax and the total.

  5. Create the customer's subscription. Send a POST request to /v1/customers with an address that holds the country, then a POST request to /v1/subscriptions. The first invoice holds the tax, and its total is the total that the plan showed.

  6. Report the payment. Send a POST request to /v1/invoices/{id}/mark-paid with the invoice's total as the amount. Black records the payment, and the customer.subscription.updated it causes holds the invoice's figures in data.invoice.

Choose a tax mode​

The setting tax_mode holds one of three modes:

ModeWhat Black does
noneThe default. Black adds no tax to anything. An invoice holds no tax fields, and the figures are the price.
rulesBlack applies the rates that you set in your settings, as the next sections explain.
serviceBlack refuses this value today, with 422 and the code parameter_invalid, and names tax_mode. No tax service is connected.

A change of mode applies to the invoices that Black makes afterwards. An invoice that Black has made keeps its figures.

The tax settings are these:

SettingDefaultAllowedWhat it holds and does
tax_modenonenone or rulesHow Black taxes invoices.
prices_include_taxtruetrue or falseWhether a price already includes the tax.
vat_rates{}A country code in upper case and a rate from 0 to 100 with at most two decimals, for each countryThe rate of each country. Send null for a country to remove its rate, and the other countries keep theirs.
vat_default_ratenullA rate from 0 to 100 with at most two decimals, or nullThe rate of a customer whose country has no rate in vat_rates. Send null to remove it.
tax_registration_numbernullUp to 64 letters, digits, spaces and hyphensYour tax registration number. Black copies it onto each taxed invoice.
tax_seller_countrynullA country code in upper case, or nullThe country you sell from.

Two clients change settings. The vendor changes every setting, with Black-Account naming the sub-account. The account's own client changes vat_rates, vat_default_rate, tax_registration_number and tax_seller_country, and reads the rest. The account's own client that sends tax_mode or prices_include_tax is refused with 403 and the code setting_set_by_vendor, whatever the value is. See Change account settings for the settings that are not about tax and for how a change is recorded.

The first request below comes from the client of an account. It tries to change the tax mode, and Black refuses it with 403. The sample writes PLACEHOLDER_ACCESS_TOKEN where the access token of the account's own client goes:

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--data '{"tax_mode":"rules"}'

The next sample is the vendor's. It creates a sub-account and sets its tax settings. It adds the rates of two countries, edits one and removes it, and reads the settings. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes. A test environment takes five writes a second, so the sample waits a second after each write.

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Settings $RUN\",\"contact_email\":\"owner@tax-settings.example\",\"external_reference\":\"tax-settings-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

ADDED=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":true,"vat_rates":{"SA":15,"AE":5},"tax_seller_country":"SA","tax_registration_number":"300000000000003"}')
has "$ADDED" '"tax_mode":"rules"'
has "$ADDED" '"vat_rates":{"AE":"5.00","SA":"15.00"}'

EDITED=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"vat_rates":{"AE":6}}')
has "$EDITED" '"vat_rates":{"AE":"6.00","SA":"15.00"}'

REMOVED=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"vat_rates":{"AE":null}}')
has "$REMOVED" '"vat_rates":{"SA":"15.00"}'

SETTINGS=$(curl --silent https://api.black.example/v1/settings "${H[@]}")
has "$SETTINGS" '"tax_registration_number":"300000000000003"'
has "$SETTINGS" '"tax_seller_country":"SA"'

The first request answers 201 and the others 200. Each change answers all the settings as they are after it. The rate of AE is 5.00 after the first change, 6.00 after the second, and gone after the third, while SA keeps 15.00 throughout.

Find the rate Black applies​

Black reads the rate when it decides an invoice, from the customer's country, which is the country of the customer's address. It takes the first of these that holds:

  1. The rate of that country in vat_rates. Black records the lines of an invoice taxed at one rate as a group, in tax_details.breakdown, and the group names the rate's ID in reference, with source rules.
  2. The vat_default_rate, when the country has no rate or the customer has no country. The group has reference default.
  3. No rate. The tax is 0, total_excluding_tax equals total, and the group is at 0.00 with reference none.

A customer with no country is the third case when you set no default rate. You can send the country when you create the subscription or the grant: send customer_country, a country code in upper case, and Black sets it as the country of the customer's address before it finds the rate. Black records the change as a customer update.

The sample below makes three customers. The first lives in DE, which has no rate and no default, and holds no tax. A default rate of 15 is then set, and the second customer in DE pays 15 and the group's reference is default. The third customer has no address, and the subscription's customer_country of AE gives the customer that country and the rate of AE, which is 5. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes.

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Rates $RUN\",\"contact_email\":\"owner@tax-rates.example\",\"external_reference\":\"tax-rates-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":true,"vat_rates":{"SA":15,"AE":5}}'

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

FIRST=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-de-1-$RUN\",\"address\":{\"country\":\"DE\"}}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-de-1-$RUN" \
--data "{\"customer\":\"$FIRST\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
INVOICE=$(echo "$SUBSCRIPTION" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

NONE=$(curl --silent https://api.black.example/v1/invoices/$INVOICE "${H[@]}")
has "$NONE" '"total":4999,"tax":0,"total_excluding_tax":4999'
has "$NONE" '"reference":"none"'

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"vat_default_rate":15}'

SECOND=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-de-2-$RUN\",\"address\":{\"country\":\"DE\"}}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-de-2-$RUN" \
--data "{\"customer\":\"$SECOND\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
INVOICE=$(echo "$SUBSCRIPTION" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

DEFAULT=$(curl --silent https://api.black.example/v1/invoices/$INVOICE "${H[@]}")
has "$DEFAULT" '"total":4999,"tax":652,"total_excluding_tax":4347'
has "$DEFAULT" '"reference":"default"'

THIRD=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-ae-$RUN\"}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-ae-$RUN" \
--data "{\"customer\":\"$THIRD\",\"customer_country\":\"AE\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
INVOICE=$(echo "$SUBSCRIPTION" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

AE=$(curl --silent https://api.black.example/v1/invoices/$INVOICE "${H[@]}")
has "$AE" '"total":4999,"tax":238,"total_excluding_tax":4761'
has "$AE" '"country":"AE"'

The first request answers 201 with the sub-account, and so do the creations of the product, the price, the three customers and the three subscriptions. The other requests answer 200. The first invoice holds no tax, the second 652 at the default rate, and the third 238 at the rate of AE.

Read prices with tax included and added​

Black reads the price you set as a total that includes the tax, or as a net that the tax is added to, as prices_include_tax says. The customer is charged the invoice's total, and that is the figure your report of payment must carry: Black refuses a report with another amount, as it does for any invoice.

The table holds the figures for a monthly price of SAR 49.99 and a yearly price of SAR 499.00 at 15% in SAR, both ways:

PricePrices include tax: total, tax, without taxPrices exclude tax: total, tax, without tax
Monthly, 49.994999, 652, 43475749, 750, 4999
Yearly, 499.0049900, 6509, 4339157385, 7485, 49900

An invoice holds total, tax and total_excluding_tax, and each line holds tax, amount_excluding_tax and tax_percent. The line's amount is its total when prices include tax, and its net when prices exclude tax.

The plans read gives the same figures before the customer buys. Send GET /v1/plans?country=SA, and each plan holds a tax_quote with country, amount (the price as you set it), tax, amount_excluding_tax, total, tax_percent and prices_include_tax. The total is what the first invoice of that plan charges for a customer in that country. A country that is not a country code in upper case is refused with 422 and the code parameter_invalid, naming country. Without country, or while tax_mode is none, the plans hold no tax_quote.

The sample below sets prices that exclude tax and reads the plans for SA. It creates a customer in SA and its subscription, and reads the first invoice. It then reports the invoice's net, 4999, as paid, and Black refuses the report with 422 because the invoice's total is 5749. It reports 5749, and Black accepts it. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes.

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Added $RUN\",\"contact_email\":\"owner@tax-added.example\",\"external_reference\":\"tax-added-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":false,"vat_rates":{"SA":15}}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":49900,\"recurring\":{\"interval\":\"year\",\"interval_count\":1}}" >/dev/null

PLANS=$(curl --silent "https://api.black.example/v1/plans?country=SA" "${H[@]}")
has "$PLANS" '"amount":4999,"tax":750,"amount_excluding_tax":4999,"total":5749,"tax_percent":"15.00","prices_include_tax":false'
has "$PLANS" '"amount":49900,"tax":7485,"amount_excluding_tax":49900,"total":57385'

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-added-$RUN\",\"address\":{\"country\":\"SA\"}}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-added-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
INVOICE=$(echo "$SUBSCRIPTION" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

FIRST=$(curl --silent https://api.black.example/v1/invoices/$INVOICE "${H[@]}")
has "$FIRST" '"total":5749,"tax":750,"total_excluding_tax":4999'

curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":4999,\"currency\":\"SAR\",\"payment_reference\":\"tax-added-net-$RUN\"}"

curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":5749,\"currency\":\"SAR\",\"payment_reference\":\"tax-added-total-$RUN\"}"

The first request answers 201 with the sub-account, and so do the creations of the product, the two prices, the customer and the subscription. The report of 4999 answers 422 and the code parameter_invalid, naming amount, and the report of 5749 answers the invoice, now paid.

Read how Black rounds the tax​

Black works out the tax once for each group of lines taxed at the same rate, on the group's total, and rounds it to the nearest unit, half away from zero. It never adds up the tax of rounded lines. It then shares the group's tax among the group's lines, so that the lines' tax adds up to the invoice's tax exactly. A rounding difference never shows as a figure of its own: it sits in one line, never more than one unit from that line's own share.

A price shown alone and the same price on an invoice with a credit can therefore differ by a unit. The yearly price of SAR 499.00 shows a tax of 6509 in the plans read. On the upgrade below, the yearly line holds 6508, because the invoice's tax of 6074 is shared between the yearly line and the credit.

An upgrade with a credit shows the rule. A customer pays the monthly price of 4999 and upgrades to the yearly price of 49900 with 20 whole days left of a 30-day month. The credit is 4999 × 20 ÷ 30, which is 3333 once rounded. See Change a customer's plan for how Black works out an upgrade. The credit is at the rate of the invoice it credits, and the new price at the rate in force now. Both are 15% here, so they form one group:

Prices include taxPrices exclude tax
Yearly line4990049900
Credit line-3333-3333
The group, with tax4656746567 without tax
Tax of the group60746985
Tax of the yearly line, and of the credit6508 and -4347485 and -500
total_excluding_tax4049346567
total4656753552

The two samples below run the upgrade on a test clock that starts on 1 April 2027 at noon, so that the month has 30 days. They move the clock 9 days on, which leaves 20 whole days, because Black does not count the day of the change. They write PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes. See Test your integration with test clocks for how a clock works. The first sample is for prices that include tax:

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Upgrade $RUN\",\"contact_email\":\"owner@tax-upgrade.example\",\"external_reference\":\"tax-upgrade-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":true,"vat_rates":{"SA":15}}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

YEARLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":49900,\"recurring\":{\"interval\":\"year\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks "${H[@]}" \
--data "{\"name\":\"Tax upgrade $RUN\",\"frozen_time\":1806580800}" \
| sed -n 's/.*"id":"\(clock_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-upgrade-$RUN\",\"address\":{\"country\":\"SA\"},\"test_clock\":\"$CLOCK\"}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-upgrade-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
SUBSCRIPTION=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"id":"\(sub_[0-9a-z]*\)".*/\1/p')
INVOICE=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":4999,\"currency\":\"SAR\",\"payment_reference\":\"tax-upgrade-first-$RUN\"}" >/dev/null

curl --silent --request POST https://api.black.example/v1/test-clocks/$CLOCK/advance "${H[@]}" \
--data '{"frozen_time":1807358400}' >/dev/null

for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
sleep 1
STATE=$(curl --silent https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}")
if [[ "$STATE" == *'"status":"ready"'* ]]; then
break
fi
done

if [[ "$STATE" != *'"status":"ready"'* ]]; then
echo "The clock is not ready."
exit 1
fi

OFFER_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions/$SUBSCRIPTION/change-plan "${H[@]}" \
--header "Idempotency-Key: tax-upgrade-change-$RUN" \
--data "{\"price\":\"$YEARLY\",\"when\":\"now\"}")
OFFER=$(echo "$OFFER_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

UPGRADE=$(curl --silent https://api.black.example/v1/invoices/$OFFER "${H[@]}")
has "$UPGRADE" '"total":46567,"tax":6074,"total_excluding_tax":40493'
has "$UPGRADE" '"amount":49900,"tax":6508,"amount_excluding_tax":43392'
has "$UPGRADE" '"amount":-3333,"tax":-434,"amount_excluding_tax":-2899'

curl --silent --request DELETE https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}"

The second sample is the same upgrade for prices that exclude tax:

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Upgrade Added $RUN\",\"contact_email\":\"owner@tax-upgrade.example\",\"external_reference\":\"tax-upgrade-added-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":false,"vat_rates":{"SA":15}}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

YEARLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":49900,\"recurring\":{\"interval\":\"year\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks "${H[@]}" \
--data "{\"name\":\"Tax upgrade added $RUN\",\"frozen_time\":1806580800}" \
| sed -n 's/.*"id":"\(clock_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-upgrade-added-$RUN\",\"address\":{\"country\":\"SA\"},\"test_clock\":\"$CLOCK\"}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-upgrade-added-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
SUBSCRIPTION=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"id":"\(sub_[0-9a-z]*\)".*/\1/p')
INVOICE=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":5749,\"currency\":\"SAR\",\"payment_reference\":\"tax-upgrade-added-first-$RUN\"}" >/dev/null

curl --silent --request POST https://api.black.example/v1/test-clocks/$CLOCK/advance "${H[@]}" \
--data '{"frozen_time":1807358400}' >/dev/null

for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
sleep 1
STATE=$(curl --silent https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}")
if [[ "$STATE" == *'"status":"ready"'* ]]; then
break
fi
done

if [[ "$STATE" != *'"status":"ready"'* ]]; then
echo "The clock is not ready."
exit 1
fi

OFFER_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions/$SUBSCRIPTION/change-plan "${H[@]}" \
--header "Idempotency-Key: tax-upgrade-added-change-$RUN" \
--data "{\"price\":\"$YEARLY\",\"when\":\"now\"}")
OFFER=$(echo "$OFFER_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

UPGRADE=$(curl --silent https://api.black.example/v1/invoices/$OFFER "${H[@]}")
has "$UPGRADE" '"total":53552,"tax":6985,"total_excluding_tax":46567'
has "$UPGRADE" '"amount":49900,"tax":7485,"amount_excluding_tax":49900'
has "$UPGRADE" '"amount":-3333,"tax":-500,"amount_excluding_tax":-3333'

curl --silent --request DELETE https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}"

Each request answers 200, except the creations, which answer 201. The offer is an open invoice with the totals in the table.

Change a rate​

A rate you change applies from the next invoice that Black decides. An invoice that Black has already made keeps its figures, so the invoice for the first month stays at the rate that was in force when it was made. The renewal of a subscription is decided at its renewal, so it uses the rate in force then.

The sample below makes a subscription at 15%, changes the rate of SA to 5, and moves a test clock past the renewal. The first invoice still holds a tax of 652, and the renewal holds 238. The renewal's reminder, customer.subscription.renewal_upcoming, holds the tax at the rate in force when Black records the reminder, in data.renewal. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes.

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Renewal $RUN\",\"contact_email\":\"owner@tax-renewal.example\",\"external_reference\":\"tax-renewal-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":true,"vat_rates":{"SA":15}}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks "${H[@]}" \
--data "{\"name\":\"Tax renewal $RUN\",\"frozen_time\":1806580800}" \
| sed -n 's/.*"id":"\(clock_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-renewal-$RUN\",\"address\":{\"country\":\"SA\"},\"test_clock\":\"$CLOCK\"}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-renewal-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
SUBSCRIPTION=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"id":"\(sub_[0-9a-z]*\)".*/\1/p')
FIRST=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/invoices/$FIRST/mark-paid "${H[@]}" \
--data "{\"amount\":4999,\"currency\":\"SAR\",\"payment_reference\":\"tax-renewal-first-$RUN\"}" >/dev/null

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"vat_rates":{"SA":5}}' >/dev/null

curl --silent --request POST https://api.black.example/v1/test-clocks/$CLOCK/advance "${H[@]}" \
--data '{"frozen_time":1809172801}' >/dev/null

for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
sleep 1
STATE=$(curl --silent https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}")
if [[ "$STATE" == *'"status":"ready"'* ]]; then
break
fi
done

if [[ "$STATE" != *'"status":"ready"'* ]]; then
echo "The clock is not ready."
exit 1
fi

RENEWED=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION "${H[@]}")
RENEWAL=$(echo "$RENEWED" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

FIRST_INVOICE=$(curl --silent https://api.black.example/v1/invoices/$FIRST "${H[@]}")
has "$FIRST_INVOICE" '"total":4999,"tax":652,"total_excluding_tax":4347'

RENEWAL_INVOICE=$(curl --silent https://api.black.example/v1/invoices/$RENEWAL "${H[@]}")
has "$RENEWAL_INVOICE" '"total":4999,"tax":238,"total_excluding_tax":4761'

REMINDER=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.renewal_upcoming&customer=$CUSTOMER" "${H[@]}")
has "$REMINDER" '"tax":238'
has "$REMINDER" '"total_excluding_tax":4761'

curl --silent --request DELETE https://api.black.example/v1/test-clocks/$CLOCK "${H[@]}"

Read what an invoice and its events carry​

Under rules, each invoice, each line and each event about an invoice carries net, tax and gross. Black stores them when it makes the invoice and never changes them afterwards, so a later payment, a void or a change of rate leaves them as they were.

  • The invoice holds tax, total_excluding_tax and total, and tax_details. tax_details holds the mode, prices_include_tax, the country the rate was found for, your seller_tax_registration_number, and the breakdown: one group for each rate, with its percent, the taxable_amount, the tax, the source (rules) and the reference (the rate's ID, default or none).
  • Each line holds tax, amount_excluding_tax and tax_percent. A credit line holds them below zero.
  • The invoice events, invoice.created, invoice.finalized, invoice.paid and invoice.voided, hold the invoice with these fields.
  • The subscription update that a payment or a renewal causes, customer.subscription.updated, holds the figures of that invoice in data.invoice: tax, total and total_excluding_tax, beside the invoice's id.
  • The renewal reminder, customer.subscription.renewal_upcoming, holds the tax of the renewal in data.renewal.tax and data.renewal.total_excluding_tax, beside amount.

Under none, an invoice holds no tax, no total_excluding_tax and no tax_details, as it did before you chose a mode. An invoice taxed at 0% holds a tax of 0, so you can tell an account that applies no tax from an account that charges 0%. See Events for every event and for what each one holds.

Issue your own tax invoice​

You are the seller. Black does not issue tax invoices and does not issue e-invoices.

Black's invoice is a billing record. It holds the figures that Black decided, and you use them to issue your own tax invoice to your customer. Black does not send the invoice to the tax authority. It carries no QR code and no stamp.

E-invoicing is yours. In Saudi Arabia, the e-invoicing rules are those of ZATCA, the Zakat, Tax and Customs Authority, and your own system follows them.

Where an invoice is in another currency, your tax invoice states its tax in riyals, at the central bank's rate. Black's figures are in the invoice's currency.

Black applies the rates and choices that you set, and gives no advice on which rate applies to which customer. Your own tax adviser confirms the rates and the choices you set, before you charge a customer with them.

Handle a refusal​

Each refusal gives an error with a code. See Errors for every code.

  • setting_set_by_vendor (403). Your account's client sent tax_mode or prices_include_tax, and only the vendor changes them. Leave the field out and ask your vendor, or send the request as the vendor with Black-Account.
  • parameter_invalid (422). A value is out of bounds, of the wrong kind or not a setting: a rate below 0 or above 100, a rate with more than two decimals, a country that is not a country code in upper case, a tax_registration_number of more than 64 characters or with another character than a letter, a digit, a space or a hyphen, or a tax_mode of service. param names the field. Correct it and send the change again: Black changes nothing when it refuses any part of the request.
  • billing_engine_unavailable (503). Black could not work out the tax of a plan for a country, or of an invoice. Send the request again.

A runnable sample​

The sample is a vendor's. It creates a sub-account, sets the tax mode, prices that include tax and a rate of 15% for SA, and creates a product with a monthly price of SAR 49.99 and a yearly price of SAR 499.00. It reads the plans for SA, creates a customer in SA and a subscription to the monthly price, and reads the first invoice. It reports the invoice paid with its total, and reads the event that the payment records. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes, reads each ID from the response before it, and needs sed. A test environment takes five writes a second, so the sample waits a second after each write. Its names and references end in a number made from the time, so that it runs again.

TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Tax Demo $RUN\",\"contact_email\":\"owner@tax-demo.example\",\"external_reference\":\"tax-demo-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"tax_mode":"rules","prices_include_tax":true,"vat_rates":{"SA":15},"tax_seller_country":"SA","tax_registration_number":"300000000000003"}'

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Tax Prime $RUN\"}" | sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

MONTHLY=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":4999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":49900,\"recurring\":{\"interval\":\"year\",\"interval_count\":1}}" >/dev/null

PLANS=$(curl --silent "https://api.black.example/v1/plans?country=SA" "${H[@]}")
has "$PLANS" '"amount":4999,"tax":652,"amount_excluding_tax":4347,"total":4999,"tax_percent":"15.00","prices_include_tax":true'
has "$PLANS" '"amount":49900,"tax":6509,"amount_excluding_tax":43391,"total":49900'

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"tax-demo-$RUN\",\"address\":{\"country\":\"SA\"}}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

SUBSCRIPTION=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: tax-demo-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}]}")
INVOICE=$(echo "$SUBSCRIPTION" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')

FIRST=$(curl --silent https://api.black.example/v1/invoices/$INVOICE "${H[@]}")
has "$FIRST" '"total":4999,"tax":652,"total_excluding_tax":4347'
has "$FIRST" '"tax":652,"amount_excluding_tax":4347,"tax_percent":"15.00"'
has "$FIRST" '"seller_tax_registration_number":"300000000000003"'

curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":4999,\"currency\":\"SAR\",\"payment_reference\":\"tax-demo-$RUN\"}"

UPDATED=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.updated&customer=$CUSTOMER&limit=1" "${H[@]}")
has "$UPDATED" '"tax":652'
has "$UPDATED" '"total":4999'
has "$UPDATED" '"total_excluding_tax":4347'

The first request answers 201 with the sub-account, and so do the creations of the product, the two prices, the customer and the subscription. The other requests answer 200. The plans for SA hold a tax_quote of 652 on 4999 for the monthly price and 6509 on 49900 for the yearly price. The first invoice holds total 4999, tax 652 and total_excluding_tax 4347, one group at 15.00 with source rules, and your registration number. The report answers the invoice, now paid, and the subscription update that it causes holds the same figures in data.invoice.

What to do next​