Skip to main content

Test with test clocks, then go live

This page shows you how to test an integration in the test environment of your account, and what to check before you switch to the live environment.

Know what test and live are​

An account has two environments, test and live. Each has its own API client, its own publishable keys, its own data and its own settings. You exchange the client ID and secret of an environment for an access token, as Authentication explains.

A token of the test environment reaches the test data of the account and nothing else. A token of the live environment reaches the live data and nothing else. Test data never meets live data: a customer, subscription, invoice, event or webhook endpoint that you make in one environment does not exist in the other, and no request moves one across. A publishable key starts with pk_test_ in a test environment and pk_live_ in a live one, and a test key never works on live data. See Manage an app's keys and origins.

A test environment takes five writes a second, the same limit as a live one. Test clock requests with a live token are refused with 403 and the code test_mode_required.

Test time with a test clock​

A test clock holds the time of the customers you put on it. You move the clock forward, and Black runs what falls due on the way: a renewal, an overdue invoice, grace, the end of a subscription. You test in seconds what takes a month. The guide Test your integration with test clocks explains each request, what moves with the clock and what keeps the real time, and has a runnable sample of a renewal that is paid.

The two samples below follow the unpaid renewal and the cancel at the period's end. Each makes its own product, price and clock, reads each ID from the response before it, and deletes its clock at the end. Each needs sed.

Test an unpaid renewal through grace to its end​

The first sample reports the first invoice paid, then leaves the renewal invoice unpaid and advances the clock three times:

  1. To one second after the end of the period. The renewal runs, and the subscription has a new open invoice.
  2. To one second after the invoice's due_date. Black records invoice.overdue, and the subscription is past_due with the label grace_period. Its grace_period_end says when grace ends.
  3. To one second after grace_period_end. The subscription is canceled, its end_reason is payment_failed, and the invoice is still open.

A subscription keeps giving its features in grace while the environment's keeps_entitlements_in_grace is true. See "Handle an unpaid renewal" in Create a subscription and report its payments.

TOKEN=PLACEHOLDER_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM

advance_clock() {
curl --silent --request POST https://api.black.example/v1/test-clocks/$CLOCK/advance \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"frozen_time\":$1}"
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 \
--header "Authorization: Bearer $TOKEN")
if [[ "$STATE" == *'"status":"ready"'* ]]; then
return 0
fi
done
echo "The clock is not ready."
exit 1
}

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Unpaid Clock $RUN\"}" \
| sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

PRICE=$(curl --silent --request POST https://api.black.example/v1/prices \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":6000,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

NOW=$(date +%s)
CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Unpaid renewal $RUN\",\"frozen_time\":$NOW}" \
| sed -n 's/.*"id":"\(clock_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Huda\",\"email\":\"huda$RUN@example.com\",\"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 \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: huda-subscription-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$PRICE\"}]}")
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 \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"amount\":6000,\"currency\":\"SAR\",\"payment_reference\":\"huda-first-$RUN\"}"

PERIOD_END=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"current_period_end":\([0-9]*\).*/\1/p')
advance_clock $((PERIOD_END + 1))

RENEWAL_INVOICE=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')
DUE_DATE=$(curl --silent https://api.black.example/v1/invoices/$RENEWAL_INVOICE \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"due_date":\([0-9]*\).*/\1/p')
advance_clock $((DUE_DATE + 1))

IN_GRACE=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN")
echo "$IN_GRACE"
GRACE_END=$(echo "$IN_GRACE" | sed -n 's/.*"grace_period_end":\([0-9]*\).*/\1/p')
advance_clock $((GRACE_END + 1))

curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN"

curl --silent https://api.black.example/v1/invoices/$RENEWAL_INVOICE \
--header "Authorization: Bearer $TOKEN"

curl --silent --request DELETE https://api.black.example/v1/test-clocks/$CLOCK \
--header "Authorization: Bearer $TOKEN"

Each request answers 200, except the creations, which answer 201. After the second advance, the subscription read is past_due with the status_label grace_period and a grace_period_end. After the third, the subscription is canceled with the end_reason payment_failed, and the renewal invoice is still open. The deletion answers deleted true.

Test a cancel at the period's end​

The second sample reports the first invoice paid and cancels the subscription at the end of the period. The subscription stays active with the label pending_cancel until the clock passes cancel_at. Then it is canceled with the end_reason period_end, and no renewal invoice follows. See Cancel a subscription.

TOKEN=PLACEHOLDER_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Cancel Clock $RUN\"}" \
| sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')

PRICE=$(curl --silent --request POST https://api.black.example/v1/prices \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":6000,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')

NOW=$(date +%s)
CLOCK=$(curl --silent --request POST https://api.black.example/v1/test-clocks \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Cancel at period end $RUN\",\"frozen_time\":$NOW}" \
| sed -n 's/.*"id":"\(clock_[0-9a-z]*\)".*/\1/p')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Salma\",\"email\":\"salma$RUN@example.com\",\"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 \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: salma-subscription-$RUN" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$PRICE\"}]}")
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 \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"amount\":6000,\"currency\":\"SAR\",\"payment_reference\":\"salma-first-$RUN\"}"

curl --silent --request POST https://api.black.example/v1/subscriptions/$SUBSCRIPTION/cancel \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: salma-cancel-$RUN" \
--data '{"when":"period_end"}'

PERIOD_END=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"current_period_end":\([0-9]*\).*/\1/p')

curl --silent --request POST https://api.black.example/v1/test-clocks/$CLOCK/advance \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"frozen_time\":$((PERIOD_END + 1))}"

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 \
--header "Authorization: Bearer $TOKEN")
if [[ "$STATE" == *'"status":"ready"'* ]]; then
break
fi
done

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

curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN"

curl --silent --request DELETE https://api.black.example/v1/test-clocks/$CLOCK \
--header "Authorization: Bearer $TOKEN"

Each request answers 200, except the creations, which answer 201. The cancel answers the subscription with cancel_at_period_end true. After the advance, the subscription read is canceled with the end_reason period_end. The deletion answers deleted true.

Know what you cannot test​

The first release has no card simulator. Black does not take card payments in this release, so there are no test cards and no card outcomes to test. Your account collects the money and reports each payment to Black, as Create a subscription and report its payments explains: test that report, and test what your integration does when the report does not come, with a test clock.

Work through the go-live checklist​

Go through each line with the live environment before real customers use your integration.

  • Use the live client. Get the live client ID and secret from Black, keep them in environment variables of your server, and read the access token with them. Replace the test client in your configuration. Never put a secret in browser or mobile code. See Authentication.
  • Use the live publishable keys. Create a publishable key for each app in the live environment, with its allowed origins on https, and replace the pk_test_ keys. See Manage an app's keys and origins.
  • Register the live webhook endpoints. A webhook endpoint belongs to one environment, so register each endpoint again in the live environment, and store its new whsec_ secret where your server reads it. See Webhooks.
  • Verify the webhooks. Check the signature of every delivery with the live secret and answer with a 2xx status. Read the endpoint's deliveries to confirm that the first live events arrived.
  • Review the settings. Settings belong to one environment, so the live environment does not take the values you tried in the test environment. Read GET /v1/settings in the live environment, and review grace, days_until_due, revival_days, reminder_lead_days, cancellation_reason_required and the tax settings. See Change your account's settings.
  • Make the products and prices again. Test data never meets live data, so create the live products, prices and features, as the guides explain.
  • Report each payment. Confirm that your integration reports every payment it takes, so that a subscription never goes into grace by mistake.

What to do next​