Skip to main content

Show and charge in your customer's currency

What you will build​

You show your plans in the currency your customer browses in, create the customer's subscription in that currency, and report each payment in it. You read what each invoice was charged at, and you learn where Black's exchange rates come from, how long Black uses a rate and what happens when none is available.

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 800 in USD is USD 8.00 and 2451 in KWD is KWD 2.451.

You set each price in one currency, as in Create a subscription and report its payments. The customer's currency is the one you show the customer and charge the customer in. Black knows nine currencies: AED, BHD, EUR, JPY, KWD, OMR, QAR, SAR and USD.

You collect the money from your own customer. Black converts the price, records the rate and tells you what to collect.

The samples are a vendor's. Each one creates a sub-account and works in its test environment, because the currencies and the test rates belong to an environment.

Steps​

  1. List the currencies you sell in. Send a PATCH request to /v1/settings with presentment_currencies, as ["USD","EUR"]. The response holds the list in code order.

  2. Give the test environment its rates. Send a PUT request to /v1/test-exchange-rates with a base and the rates of each currency against it. The response holds the set of rates, with an id that starts with fxr_. A live environment uses the rates that Black fetches.

  3. Read the plans in the customer's currency. Send a GET request to /v1/plans with currency set to the currency, as ?currency=USD. Each plan holds a presentment with the converted unit_amount, the exchange_rate, and the exchange_rate_set the price was converted at.

  4. Convert a discount. Send a GET request to /v1/conversions with the currency, the amounts and, if you want the same rates as the plans, the exchange_rate_set. The response holds each amount converted.

  5. Create the subscription in that currency. Send a POST request to /v1/subscriptions with currency and the exchange_rate_set that the plans showed. The response holds the subscription with presentment_currency.

  6. Read the first invoice. Its presentment holds what the customer owes in the customer's currency, the rate, its set and its time.

  7. Report the payment in the customer's currency. Send a POST request to /v1/invoices/{id}/mark-paid with the presentment's amount_remaining as the amount and its currency. The response holds the invoice, now paid.

Choose the currencies you sell in​

Black shows and charges in a currency only when the environment lists it. The list is empty in a new environment, so nothing is converted until you list a currency. Three settings control the conversion:

SettingDefaultAllowedWhat it holds and does
presentment_currencies[]Up to 20 currencies that Black knows, each once, in upper caseThe currencies the environment shows and charges in, besides the currency of each price. Black answers the list in code order.
exchange_rate_margin_percent0.00A percentage from 0 to 10 with at most two decimalsA margin that Black adds to the rate before it converts. Black answers it as a string with two decimals.
exchange_rate_hold_minutes600 to 1440How long a price that you showed stays good for a new subscription after a newer set of rates arrives.

The account's own client and the vendor change the three settings. See Change account settings for how a change is recorded.

A price in its own currency is never converted. A plan priced in SAR and read with currency=SAR shows its own unit_amount, and every rate field is null.

Read where the exchange rates come from​

Black fetches its rates from a rate service, an outside service that publishes the day's rates. It does not work out a rate itself.

  • When. Black fetches a set of rates every day at 00:05 Riyadh time. It also checks every hour and fetches again when the newest set is more than 24 hours old, so a day on which the rate service was down is caught up within the hour.
  • What it stores. Each fetch is one set of rates with an id that starts with fxr_, a source, a base currency, a taken_at time that the rate service gave, and one rate for each currency Black knows. Each rate is a string: what one unit of the base buys. Black never changes a set after it stores it.
  • How to read them. Send a GET request to /v1/exchange-rates for the set in force. Send a GET request to /v1/exchange-rates/{id} for any set that Black stored, however old, so you can read the rate that a charge names. An unknown id is refused with 404.
  • How long. Black uses a set for 48 hours after its taken_at. After that, and before the first fetch, no rate is in force.
  • When a rate is missing. With no rate in force, Black answers 503 with the code exchange_rate_unavailable and shows no converted price. A price in its own currency is still shown and sold. A renewal never waits for a rate: see "Read the renewal".

A test environment sets its own rates. Send a PUT request to /v1/test-exchange-rates with a base and rates, each rate a decimal string above zero with no sign and no exponent. The rates hold the base at exactly 1, every currency of the environment's list and every currency of its prices, so set the list and the prices first. Each PUT stores a new set that the environment uses from then on. A test set never goes stale, so a test clock that you move far ahead still converts. Send a DELETE request to /v1/test-exchange-rates to go back to the fetched sets. It answers 204. A live client that sends either request is refused with 403 and the code test_mode_required.

The first sample sets the three settings, gives the test environment rates, reads the set in force and the set by its id, and goes back to the fetched sets. 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\":\"Currency Rates $RUN\",\"contact_email\":\"owner@currency-rates.example\",\"external_reference\":\"currency-rates-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")

SETTINGS=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"presentment_currencies":["USD","EUR","KWD"],"exchange_rate_margin_percent":2.5,"exchange_rate_hold_minutes":30}')
has "$SETTINGS" '"presentment_currencies":["EUR","KWD","USD"]'
has "$SETTINGS" '"exchange_rate_margin_percent":"2.50"'
has "$SETTINGS" '"exchange_rate_hold_minutes":30'

SET=$(curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7504","EUR":"0.861234","KWD":"0.306512"}}')
has "$SET" '"source":"test"'
SET_ID=$(echo "$SET" | sed -n 's/.*"id":"\(fxr_[0-9a-z]*\)".*/\1/p')

IN_FORCE=$(curl --silent https://api.black.example/v1/exchange-rates "${H[@]}")
has "$IN_FORCE" "\"id\":\"$SET_ID\""
has "$IN_FORCE" '"EUR":"0.861234"'

BY_ID=$(curl --silent "https://api.black.example/v1/exchange-rates/$SET_ID" "${H[@]}")
has "$BY_ID" '"base":"USD"'

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

The first request answers 201 with the sub-account. The PUT answers 200 with the set, the two reads answer 200, and the DELETE answers 204.

Show a price in the customer's currency​

Send GET /v1/plans?currency=USD to read every plan with its price converted. The plan's own price stays as you set it. Beside it, presentment holds:

  • currency and unit_amount: the converted price.
  • exchange_rate: the rate of record, a string with 12 digits after the point.
  • exchange_rate_set, exchange_rate_source and exchange_rate_time: the set the price was converted at, where it came from, and its taken_at in Unix seconds.
  • honoured_until: always null on a read, because a read uses the newest set. The hold applies when you create a subscription.

The currency must be one that Black knows, in upper case, and either the plan's own or on the environment's list. Black converts once for all the plans of a read, so every price in the answer is at one set.

The table holds the worked example. One set has the base USD, with SAR at 3.7504, EUR at 0.861234 and KWD at 0.306512. The rate from SAR to a currency is that currency's rate divided by the rate of SAR. Black rounds the rate once, to 12 places, and then rounds each converted amount once, to the smallest unit of the customer's currency. It never converts a total: each line is converted, and the total is their sum.

Customer's currencyRate from SARMonthly price, SAR 29.99Yearly price, SAR 299.99
USD0.266638225256800, which is USD 8.007999, which is USD 79.99
EUR0.229637905290689, which is EUR 6.896889, which is EUR 68.89
KWD0.0817278157002451, which is KWD 2.45124518, which is KWD 24.518

With a margin of 2%, the rate to USD is 0.271970989761 and the monthly price is USD 8.16.

A discount that you show as a fixed amount lives in your own data, so Black never reads it. Convert it at the same rate as the price. Send GET /v1/conversions with currency and up to 20 amounts, each with an amount and a currency, all in one currency. Add exchange_rate_set to name the set the plans showed, or leave it out for the set in force. The response holds each amount converted, in the order you sent it, with the rate, its set, its source and its time. It writes nothing. An amount that is already in the currency comes back unchanged, with every rate field null. SAR 50.00 comes back as USD 13.33 at the rate of the table.

The next sample reads the plans in the three currencies and in SAR, converts a discount, and then sets a margin of 2%. 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\":\"Currency Plans $RUN\",\"contact_email\":\"owner@currency-plans.example\",\"external_reference\":\"currency-plans-$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 '{"presentment_currencies":["USD","EUR","KWD"]}' >/dev/null

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

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

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

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7504","EUR":"0.861234","KWD":"0.306512"}}' >/dev/null

USD=$(curl --silent "https://api.black.example/v1/plans?currency=USD" "${H[@]}")
has "$USD" '"currency":"USD","unit_amount":800,"exchange_rate":"0.266638225256"'
has "$USD" '"currency":"USD","unit_amount":7999,"exchange_rate":"0.266638225256"'

EUR=$(curl --silent "https://api.black.example/v1/plans?currency=EUR" "${H[@]}")
has "$EUR" '"currency":"EUR","unit_amount":689,"exchange_rate":"0.229637905290"'
has "$EUR" '"currency":"EUR","unit_amount":6889,"exchange_rate":"0.229637905290"'

KWD=$(curl --silent "https://api.black.example/v1/plans?currency=KWD" "${H[@]}")
has "$KWD" '"currency":"KWD","unit_amount":2451,"exchange_rate":"0.081727815700"'
has "$KWD" '"currency":"KWD","unit_amount":24518,"exchange_rate":"0.081727815700"'

SAR=$(curl --silent "https://api.black.example/v1/plans?currency=SAR" "${H[@]}")
has "$SAR" '"currency":"SAR","unit_amount":2999,"exchange_rate":null'

DISCOUNT=$(curl --silent --get https://api.black.example/v1/conversions "${H[@]}" \
--data-urlencode "currency=USD" \
--data-urlencode "amounts[0][amount]=5000" \
--data-urlencode "amounts[0][currency]=SAR")
has "$DISCOUNT" '"amounts":[{"amount":1333,"currency":"USD"}]'
has "$DISCOUNT" '"exchange_rate":"0.266638225256"'

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

MARGIN=$(curl --silent "https://api.black.example/v1/plans?currency=USD" "${H[@]}")
has "$MARGIN" '"currency":"USD","unit_amount":816,"exchange_rate":"0.271970989761"'

The first request answers 201 with the sub-account, and so do the creations of the product and the two prices. The other requests answer 200.

Create a subscription in the customer's currency​

Send a POST request to /v1/subscriptions, or to /v1/subscription-grants, with the same body as before and two more fields:

  • currency: the currency you showed the customer.
  • exchange_rate_set: the set that the plans showed. It is optional and only with currency. Without it, Black uses the set in force.

The customer's currency is set here and never changes. The response holds the subscription with presentment_currency, and null when the subscription is in its prices' currency. A currency that is the price's own is taken as any other create, and the subscription has no presentment_currency.

The first invoice is converted at the set that you named, so the customer is charged the price that you showed. Black keeps the set for the invoice. A repeat of the request under the same Idempotency-Key answers what Black saved, even when a newer set has arrived.

A set that a newer set has replaced stays good for exchange_rate_hold_minutes, 60 by default. A request that names a set after that is refused, and you read the plans again and show the new price. Black never replaces a set for you, so the customer is never charged a price that was not shown.

Read what an invoice was charged at​

An invoice keeps the amounts in the price's currency, as before. When the subscription has a customer's currency, the invoice's presentment is set, and it holds:

  • currency, subtotal, total, amount_due, amount_paid and amount_remaining, in the customer's currency.
  • exchange_rate, exchange_rate_set, exchange_rate_source and exchange_rate_time: the rate, its set, where it came from and its time.
  • exchange_rate_margin_percent: the margin that was charged, or null when there was none.

Each line holds presentment_amount, presentment_exchange_rate and presentment_exchange_rate_set. Black converts each line on its own and the invoice's total is the sum of its lines. Under tax rules, the presentment also holds tax and total_excluding_tax, and each line presentment_tax and presentment_amount_excluding_tax, in the customer's currency. See Charge tax with your own rules.

Black stores the rate when it makes the invoice and never changes it. A later set, a payment, a void and a due date leave the rate, the set and the time as they were. An invoice of a subscription without a customer's currency holds presentment as null.

Send GET /v1/invoices?presentment_currency=USD to list only the invoices in that customer's currency. A code that Black does not know is refused with 422 naming presentment_currency.

Report the payment in the customer's currency​

You collect the money in the customer's currency, so you report it in that currency. Send the presentment's amount_remaining as the amount and its currency to POST /v1/invoices/{id}/mark-paid. Black refuses a report in the price's currency, or with another amount, with 422 and the code parameter_invalid, naming currency or amount, and records nothing. The invoice's presentment then holds amount_paid and an amount_remaining of 0.

Read the reminder and the renewal​

The customer is told what the renewal charges and is charged exactly that. The rate changes from one renewal to the next, but never between the reminder and the renewal:

  • The reminder. customer.subscription.renewal_upcoming holds renewal.presentment with the amount and currency that the renewal charges, and the exchange_rate, exchange_rate_set, exchange_rate_source and exchange_rate_time of the set in force when Black records the reminder. Each item holds its presentment_amount. The reminder of a subscription without a customer's currency holds none of this.
  • The renewal. The renewal's invoice is converted at the set the reminder stated, whatever the rate is on the renewal day. A renewal that has no reminder before it, because renewals were stopped across it, uses the set in force on its day.
  • A missing rate. A renewal never waits for a rate. With none in force, Black uses the set and the margin of the last invoice the customer paid in that currency, so the renewal is made and the customer's period goes on.

Upgrade in the customer's currency​

An upgrade now is new money, so Black converts it on the day you ask. The offer's invoice converts the new price at the set in force that day, and converts the credit for the days already paid at the rate of the invoice it credits. The offer's presentment total is the sum of its lines. You report the offer paid in the customer's currency, as for any invoice. See Change a customer's plan for the offer itself.

With no rate in force, a change now is refused with 503 and the code exchange_rate_unavailable, and Black records nothing, so you ask again later.

The sample below runs the renewal and the upgrade on a test clock that starts on 1 January 2027. It creates the subscription in USD at SAR 3.7504, sets SAR 3.7510 before the reminder and SAR 3.60 before the renewal, and checks that the reminder and the renewal agree at USD 8.00. It then upgrades to the yearly price and reads the offer, with its price converted at 3.60 and its credit at the rate of the renewal. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes. See Test your integration with test clocks for how a clock works.

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\":\"Currency Renewal $RUN\",\"contact_email\":\"owner@currency-renewal.example\",\"external_reference\":\"currency-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 '{"presentment_currencies":["USD"]}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Currency 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\":2999,\"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\":29999,\"recurring\":{\"interval\":\"year\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7504"}}' >/dev/null

CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks "${H[@]}" \
--data "{\"name\":\"Currency renewal $RUN\",\"frozen_time\":1798761600}" \
| 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\":\"currency-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: currency-renewal-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}],\"currency\":\"USD\"}")
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\":800,\"currency\":\"USD\",\"payment_reference\":\"currency-renewal-first-$RUN\"}" >/dev/null

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7510"}}' >/dev/null

wait_for_clock() {
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
return 0
fi
done

echo "The clock is not ready."
exit 1
}

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

REMINDER=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.renewal_upcoming&customer=$CUSTOMER" "${H[@]}")
has "$REMINDER" '"presentment":{"amount":800,"currency":"USD","exchange_rate":"0.266595574513"'

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.60"}}' >/dev/null

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

ROLLED=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.updated&customer=$CUSTOMER&limit=1" "${H[@]}")
has "$ROLLED" '"billing_reason":"subscription_cycle"'
has "$ROLLED" '"presentment_amount":800'

RENEWAL=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION "${H[@]}" \
| sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')
RENEWAL_INVOICE=$(curl --silent https://api.black.example/v1/invoices/$RENEWAL "${H[@]}")
has "$RENEWAL_INVOICE" '"presentment":{"currency":"USD","subtotal":800,"total":800'
has "$RENEWAL_INVOICE" '"exchange_rate":"0.266595574513"'

curl --silent --request POST https://api.black.example/v1/invoices/$RENEWAL/mark-paid "${H[@]}" \
--data "{\"amount\":800,\"currency\":\"USD\",\"payment_reference\":\"currency-renewal-second-$RUN\"}" >/dev/null

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

OFFER_INVOICE=$(curl --silent https://api.black.example/v1/invoices/$OFFER "${H[@]}")
has "$OFFER_INVOICE" '"presentment":{"currency":"USD","subtotal":7562,"total":7562'
has "$OFFER_INVOICE" '"presentment_amount":8333,"presentment_exchange_rate":"0.277777777778"'
has "$OFFER_INVOICE" '"presentment_amount":-771,"presentment_exchange_rate":"0.266595574513"'

PAID=$(curl --silent --request POST https://api.black.example/v1/invoices/$OFFER/mark-paid "${H[@]}" \
--data "{\"amount\":7562,\"currency\":\"USD\",\"payment_reference\":\"currency-renewal-offer-$RUN\"}")
has "$PAID" '"status":"paid"'

UPGRADED=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.updated&customer=$CUSTOMER&limit=1" "${H[@]}")
has "$UPGRADED" '"presentment_amount":8333'
has "$UPGRADED" '"presentment_amount":-771'

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

Each request answers 200, except the creations, which answer 201. The reminder states USD 800 at the rate of the second set. The renewal is USD 800 at that set, and not USD 833 at the third. The offer's price line is USD 83.33 at the rate of the third set, its credit line is USD -7.71 at the rate of the renewal, and the offer's total is USD 75.62. The update that the payment of the offer records holds the same lines in data.invoice.

Handle a refusal​

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

  • currency_not_supported (422). The currency is not a currency Black knows, is not in upper case, or is not on the environment's list and is not the price's own. param names currency. Send a code in upper case that the list holds, or list it first in presentment_currencies.
  • exchange_rate_unavailable (503). No set is in force: the newest is more than 48 hours old, or Black has fetched none yet. Black shows no converted price and creates nothing. Send the request again later, and show the price in its own currency meanwhile.
  • exchange_rate_expired (409). The exchange_rate_set you named was replaced longer ago than exchange_rate_hold_minutes. param names exchange_rate_set. Read the plans again, show the new price, and create the subscription with the new set.

A set that holds no rate for a currency of the request answers 503 in the same way. A set that the environment cannot read, such as another test environment's, is refused with 404. A currency that the list does not hold fails the same way on a plans read, a conversion and a create.

The sample below runs the three refusals. It lists USD with a hold of 0 minutes and gives the test environment a set of rates that holds no rate for SAR, so the read of the plans in USD is refused with 503. It then gives the test environment complete rates, sends a currency that Black does not know, stores a second set and names the first in a create, which is refused with 409. 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\":\"Currency Refusals $RUN\",\"contact_email\":\"owner@currency-refusals.example\",\"external_reference\":\"currency-refusals-$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 '{"presentment_currencies":["USD"],"exchange_rate_hold_minutes":0}' >/dev/null

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1"}}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Currency 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\":2999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

UNAVAILABLE=$(curl --silent "https://api.black.example/v1/plans?currency=USD" "${H[@]}")
has "$UNAVAILABLE" '"code":"exchange_rate_unavailable"'

FIRST=$(curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7504"}}' | sed -n 's/.*"id":"\(fxr_[0-9a-z]*\)".*/\1/p')

UNSUPPORTED=$(curl --silent "https://api.black.example/v1/plans?currency=GBP" "${H[@]}")
has "$UNSUPPORTED" '"code":"currency_not_supported"'
has "$UNSUPPORTED" '"param":"currency"'

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7510"}}' >/dev/null

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

EXPIRED=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--header "Idempotency-Key: currency-refusals-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}],\"currency\":\"USD\",\"exchange_rate_set\":\"$FIRST\"}")
has "$EXPIRED" '"code":"exchange_rate_expired"'
has "$EXPIRED" '"param":"exchange_rate_set"'

The first request answers 201 with the sub-account, and so do the creations of the product, the price and the customer. The plans read in USD answers 503, the read in GBP answers 422, the create answers 409, and every other request answers 200.

A runnable sample​

The sample is a vendor's. It creates a sub-account, lists USD, creates a product with a monthly price of SAR 29.99, and gives the test environment its rates. It reads the plans in USD, creates a customer and a subscription in USD at the set the plans showed, and reads the first invoice. It reports the payment in SAR, which Black refuses, then in USD, which Black accepts. It reads the update that the payment records and lists the invoices in USD. 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\":\"Currency Demo $RUN\",\"contact_email\":\"owner@currency-demo.example\",\"external_reference\":\"currency-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 '{"presentment_currencies":["USD"]}' >/dev/null

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Currency 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\":2999,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

curl --silent --request PUT https://api.black.example/v1/test-exchange-rates "${H[@]}" \
--data '{"base":"USD","rates":{"USD":"1","SAR":"3.7504"}}' >/dev/null

PLANS=$(curl --silent "https://api.black.example/v1/plans?currency=USD" "${H[@]}")
has "$PLANS" '"currency":"USD","unit_amount":800,"exchange_rate":"0.266638225256"'
SET_ID=$(echo "$PLANS" | sed -n 's/.*"exchange_rate_set":"\(fxr_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"external_reference\":\"currency-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: currency-demo-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$MONTHLY\"}],\"currency\":\"USD\",\"exchange_rate_set\":\"$SET_ID\"}")
has "$SUBSCRIPTION" '"presentment_currency":"USD"'
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" '"currency":"SAR","subtotal":2999,"total":2999'
has "$FIRST" '"presentment":{"currency":"USD","subtotal":800,"total":800,"amount_due":800'
has "$FIRST" "\"exchange_rate_set\":\"$SET_ID\""

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

PAID=$(curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid "${H[@]}" \
--data "{\"amount\":800,\"currency\":\"USD\",\"payment_reference\":\"currency-demo-usd-$RUN\"}")
has "$PAID" '"status":"paid"'
has "$PAID" '"amount_paid":800,"amount_remaining":0'

UPDATED=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.updated&customer=$CUSTOMER&limit=1" "${H[@]}")
has "$UPDATED" '"presentment_currency":"USD"'
has "$UPDATED" '"amount_paid":800'
has "$UPDATED" "\"exchange_rate_set\":\"$SET_ID\""

LISTED=$(curl --silent "https://api.black.example/v1/invoices?presentment_currency=USD" "${H[@]}")
has "$LISTED" "\"id\":\"$INVOICE\""

The first request answers 201 with the sub-account, and so do the creations of the product, the price, the customer and the subscription. The report in SAR answers 422, naming currency, and the report in USD answers 200 with the invoice, now paid. The first invoice holds SAR 2999 and a presentment of USD 800 at the rate 0.266638225256, and the update that the payment records holds the same presentment in data.invoice.

What to do next​