Skip to main content

Switch a service off and on

What you will build​

You switch an account's service off, read what the lookup and a sale answer while it is off, and see that a renewal that falls due waits. You then switch the service on and read what each waiting subscription does. You learn that no renewal is charged while the service is off and that nobody is billed for the days off.

Before you start​

You need a vendor's access token and a sub-account, with the sub-account's ID in the Black-Account header. See Create and act for sub-accounts. You also need a subscription that renews: the sample makes one on a test clock, as Test your integration with test clocks explains.

Only the vendor switches the service. The account's own client that sends service_off is refused with 403 and the code setting_set_by_vendor. See Change account settings.

Steps​

  1. Switch the service off. Send a PATCH request to /v1/settings with {"service_off":true} and the Black-Account header. The response holds the settings, with service_off true. Black records settings.updated.

  2. Read what a sale answers. GET /v1/plans answers sales_open false, and sales_closed_reasons holds service_off. A request to create a subscription is refused with 409 and the code sales_closed.

  3. Read what the lookup answers. GET /v1/active-entitlements?customer={id} answers an empty data for every customer, whatever the customer holds. Your own system sees that nobody holds a feature while the service is off.

  4. Let a renewal fall due. When the date of a renewal passes, Black charges nothing and records no event. Read the subscription with GET /v1/subscriptions/{id}: it keeps its status, and its current_period_end stays at the old date, now in the past. Black sends no renewal reminder for it.

  5. Switch the service on. Send {"service_off":false}. The response holds the settings, with service_off false. A few seconds later each waiting subscription has a new period. Read it again: its current_period_start is the moment you switched the service on, and its current_period_end is one reminder lead time later, 7 days by default. Black records customer.subscription.updated for each, with the system as its actor and the old period in previous_attributes.

  6. Read the reminder. Black records customer.subscription.renewal_upcoming for each waiting subscription at once, with the amount that the next renewal bills. Read it with GET /v1/events?type=customer.subscription.renewal_upcoming&customer={id}. See Read the reminder before a renewal. The sales are open again, and the lookup gives the customer's features.

Know what the days off cost​

Nobody is billed for the days the service is off, and no day is given back. The new period that starts when you switch the service on is free of charge: Black makes no invoice for it. The next renewal falls due at the end of that period, and Black charges it once.

A subscription that is behind on payment, past_due, gets the new period too. It keeps its status, and its reminder is recorded when you report its open invoice paid.

An end that someone asked for still happens on its date while the service is off, such as a cancel at the period's end. An invoice that was issued before you switched the service off stays open, and you may report it paid.

Know what a waiting subscription refuses​

While a subscription waits for its renewal, a cancel at the period's end and a change of plan are refused with 409 and the code renewal_deferred. Black records nothing. Send the same request, with the same Idempotency-Key, once the service is on: it is decided afresh. A cancel now is allowed, and ends the subscription.

A move of customers to a new price that reaches a waiting subscription waits too. It takes effect one renewal after the resumed one, so that the reminder always names what the renewal charges. See Change a customer's plan.

Handle a refusal​

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

  • setting_set_by_vendor (403). The account's own client sent service_off. Send the request as the vendor, with Black-Account.
  • account_required (403). The vendor's client sent the request without Black-Account. Add the header with the sub-account's ID.
  • sales_closed (409). You created a subscription while the service is off. Switch the service on, then send the request again.
  • renewal_deferred (409). You cancelled at the period's end, or changed the plan, for a subscription that waits. Switch the service on, then send the request again.

A runnable sample​

The first sample shows the refusal of a sale. It creates a sub-account, a product, a monthly price and a customer, switches the service off, and sends the creation of a subscription, which answers 409 with the code sales_closed.

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

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts \
--header "Authorization: Bearer $TOKEN" \
--data name="Switch Refusal $RUN" \
--data contact_email=owner@switch-refusal.example \
--data external_reference=switch-refusal-$RUN \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Prime Off $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 "Black-Account: $ACCOUNT" \
--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')

CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"name":"Faisal","email":"faisal@example.com"}' \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"service_off":true}'

curl --silent --request POST https://api.black.example/v1/subscriptions \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$PRICE\"}]}"

The second sample is the whole flow. It creates a sub-account, a product, a monthly price of 60 SAR, a test clock set to the real time, and a customer on it with a subscription whose first invoice it reports paid. It switches the service off, reads that sales are closed and that the lookup is empty, advances the clock past the renewal date, and reads that the subscription has not renewed. It switches the service on, reads the subscription until its period is new, and reads the reminder. 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 end in a number made from the time, so that it runs again.

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

ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts \
--header "Authorization: Bearer $TOKEN" \
--data name="Switch Demo $RUN" \
--data contact_email=owner@switch-demo.example \
--data external_reference=switch-demo-$RUN \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')

PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Prime Switch $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 "Black-Account: $ACCOUNT" \
--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 "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: switch-clock-$RUN" \
--data "{\"name\":\"Switch test $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 "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Rana\",\"email\":\"rana@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 "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: switch-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 "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data "{\"amount\":6000,\"currency\":\"SAR\",\"payment_reference\":\"switch-payment-$RUN\"}"

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

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"service_off":true}'

curl --silent https://api.black.example/v1/plans \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT"

curl --silent "https://api.black.example/v1/active-entitlements?customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT"

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

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" \
--header "Black-Account: $ACCOUNT")
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" \
--header "Black-Account: $ACCOUNT"

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"service_off":false}'

for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
sleep 1
RESUMED=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT")
if [[ "$RESUMED" != *"\"current_period_end\":$PERIOD_END,"* ]]; then
break
fi
done

if [[ "$RESUMED" == *"\"current_period_end\":$PERIOD_END,"* ]]; then
echo "The subscription has not started a new period."
exit 1
fi

curl --silent "https://api.black.example/v1/events?type=customer.subscription.renewal_upcoming&customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT"

The first sample answers 201 for the sub-account, the product, the price and the customer, 200 for the switch, and 409 with the code sales_closed for the subscription. In the second sample, the sub-account, the product, the price, the clock, the customer and the subscription answer 201, and the other requests answer 200. The plans read answers sales_open false with service_off in sales_closed_reasons, and the lookup answers an empty data. After the advance, the subscription is active and its current_period_end is the date it held before. After the switch on, its period is new, and the last read lists one reminder whose data.renewal.amount is 6000.

What to do next​