Read the reminder before a renewal
What you will build
You set the lead time of the reminder, read the event that Black records before a renewal, and read the amount the renewal bills. You then see that the reminder is held while renewals are stopped and comes back when they start again.
Before you start
You need an access token and a subscription that is active, as Create a subscription and report its payments explains. The sample is a vendor's, so it needs 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. It moves time on a test clock, as Test your integration with test clocks explains.
Your account charges its own customers, so the amount in the reminder is what you collect at the renewal. Black holds no card number. Use the reminder to tell your customer what the renewal costs.
Steps
-
Set the lead time. Send a
PATCHrequest to/v1/settingswithreminder_lead_days, a whole number of days from 1 to 30. The default is 7. The response holds the settings. The account's own client or its vendor may set it. See Change account settings. -
Wait for the reminder. Black records
customer.subscription.renewal_upcomingthe lead time before each renewal. Nothing is sent when you set the lead time: the event is recorded when its date comes. -
Read the reminder. Send a
GETrequest to/v1/events?type=customer.subscription.renewal_upcoming&customer={id}. Each event holds the subscription indata.object, and the renewal indata.renewal. -
Read the renewal.
data.renewalholdsrenews_at, the time of the renewal in Unix seconds,period_end, the end of the period that the renewal bills,amount, the total the renewal bills,currency, anditems. Each item holds itsprice, itsquantity, itsamountandprice_changes, which istruewhen the item's price is not the one the subscription holds now.
Know which subscriptions get a reminder
A subscription that is active gets one reminder for each renewal, and never two for the same renewal. A subscription that is unpaid, one that is set to cancel and one that has ended get none. A free plan gets none, because it does not renew: see Give a customer a plan for free and upgrade it. A subscription that is past_due gets its reminder once you report its open invoice paid.
A change of plan set for the end of the period shows in the reminder: the item holds the new price, and its price_changes is true. See Change a customer's plan. A price with a period shorter than the lead time gets its reminder at once, at the moment it sets its renewal.
On a test clock, Black records the reminder when the clock passes the lead time, and not before. The event's created is the real time that Black recorded it, and not the clock's time.
Know what a changed lead time does
When you make the lead time longer, each reminder still to come moves to the new lead time. A subscription whose new reminder date has already passed gets its reminder at once. A longer lead time can therefore remind many customers on the same day. A subscription that has already been reminded for its renewal is not reminded again.
When you make the lead time shorter, Black sends nothing at the change. Each reminder moves when it falls due.
Know when the reminder comes back
While renewals are stopped, Black records no reminder. A vendor stops and resumes renewals with the setting renewals_stopped. When renewals start again, each reminder that was skipped is recorded, once. When the service is on again after being off, each waiting subscription gets its reminder at once: see Switch a service off and on.
Handle a refusal
Each refusal gives an error with a code. See Errors for every code.
parameter_invalid(422).reminder_lead_daysis not a whole number from 1 to 30.paramnames it. Send a whole number in that range.setting_set_by_vendor(403). Your client sentrenewals_stopped, and only the vendor changes it. Send the request as the vendor, withBlack-Account.
A runnable sample
The sample is a vendor's. 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 sets the lead time to 14 days and stops renewals. It advances the clock to one second after the reminder date, and reads that no reminder is recorded. It resumes renewals and reads the reminder until it is recorded. 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="Reminder Demo $RUN" \
--data contact_email=owner@reminder-demo.example \
--data external_reference=reminder-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 Reminder $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: reminder-clock-$RUN" \
--data "{\"name\":\"Reminder 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\":\"Layla\",\"email\":\"layla@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: reminder-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\":\"reminder-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 '{"reminder_lead_days":14}'
curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"renewals_stopped":true}'
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: reminder-advance-$RUN" \
--data "{\"frozen_time\":$((PERIOD_END - 14 * 86400 + 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" \
--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/events?type=customer.subscription.renewal_upcoming&customer=$CUSTOMER" \
--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 '{"renewals_stopped":false}'
for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
sleep 1
REMINDERS=$(curl --silent "https://api.black.example/v1/events?type=customer.subscription.renewal_upcoming&customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT")
if [[ "$REMINDERS" == *'"renews_at"'* ]]; then
break
fi
done
if [[ "$REMINDERS" != *'"renews_at"'* ]]; then
echo "The reminder is not recorded."
exit 1
fi
curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Content-Type: application/json" \
--data '{"reminder_lead_days":7}'
The sub-account, the product, the price, the clock, the customer and the subscription answer 201, and the other requests answer 200. The first read of the events, taken while renewals are stopped, holds an empty data. The second read, after renewals resume, holds one event whose data.renewal.amount is 6000, whose currency is SAR, whose renews_at is the current_period_end that the subscription held, and whose price_changes is false.
What to do next
- Read how a subscription renews and what a clock moves in Test your integration with test clocks.
- Read how a switched-off service resumes in Switch a service off and on.
- Read every event, including the reminder, in Events, and every error code in Errors.