Skip to main content

Block a customer

What you will build​

You block a customer with a reason, read that the customer's subscription has ended, and see which requests Black now refuses. You then unblock the customer, and read the events and the activity entries that Black recorded.

Before you start​

You need a test environment and an access token. See Authentication. You also need a customer. The sample creates one, with a subscription that is paid.

A block is for a customer who must not use the subscription, for a reason you keep. It differs from a flag, which only marks a customer for review: see Flag a customer and read the activity log. A block and a flag are independent, and a block leaves the flag as it is.

Steps​

  1. Block the customer. Send a POST request to /v1/customers/{id}/block with a reason of 1 to 500 characters, once trimmed. Send an Idempotency-Key header, so that you can repeat the request safely. The response holds the customer, and its block is an object with the reason and created, the time of the block in Unix seconds.

  2. Read the customer's features. In the lookup GET /v1/active-entitlements?customer={id} the customer holds nothing from the moment of the block. Black records customer.blocked, with the reason in data.reason and {"block":null} in data.previous_attributes, and, when the customer held features, entitlements.active_entitlement_summary.updated with an empty list.

  3. Read the customer's subscription a few seconds later, with GET /v1/subscriptions?customer={id}&status=ended. Every subscription the customer still held, whether unpaid, active, set to cancel or past_due, has ended. Its status is canceled, its status_label is expired, its cancellation_reason is blocked, its end_reason is customer_blocked, and its canceled_at and ended_at are the time of the block. Black records customer.subscription.deleted for each, with the system as its actor.

  4. Unblock the customer when the block is over. Send a POST request to /v1/customers/{id}/unblock with a reason of 1 to 500 characters. The response holds the customer, and its block is null. Black records customer.unblocked, with the reason in data.reason and the block the customer had in data.previous_attributes.block.

  5. Read the customers that are blocked with GET /v1/customers?blocked=true. blocked=false lists the others. The filter combines with the other filters of the list.

A customer with no subscription is only marked blocked. A request that sends block in the creation or the update of a customer is refused with 422 naming block: use the two endpoints.

Know what a block ends and what it refuses​

A block ends the subscriptions at once, but the end is a step that runs a few seconds after the answer. Black never refunds: a paid invoice stays paid, and an unpaid invoice stays open.

While the customer is blocked, these requests are refused with 409 and the code blocked:

  • A new subscription for the customer. The error names customer.
  • A report of payment, POST /v1/invoices/{id}/mark-paid, for any invoice of the customer's subscriptions. The invoice stays as it was.
  • An undo of a cancel, a change of plan and its undo.

A cancel is never refused, because it ends nothing that the block would not end.

The sample below shows the first refusal. It creates a customer, a product and a price, blocks the customer, and sends the creation of a subscription, which answers 409 with the code blocked.

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

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@example.com"}' \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')

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

curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/block \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"reason":"Repeated chargeback disputes"}'

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

Unblock and let the customer join again​

An unblock brings no subscription back. The customer may subscribe again, as a new subscription with a new first invoice, and the old one stays canceled with the end_reason customer_blocked. A payment that you report later for a subscription that the block reached is recorded, and never brings it back, whatever revival_days says.

An unblock sent before the ends have run is refused with 409 and the code customer_block_in_progress. Read the subscription until it is canceled, then send the unblock again.

Handle a refusal​

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

  • customer_already_blocked (409). The customer is already blocked, and Black changes nothing. To give another reason, unblock the customer, then block again.
  • customer_not_blocked (409). You sent an unblock for a customer that has no block. Read the customer: its block is null, so there is nothing to remove.
  • customer_block_in_progress (409). The block's ends have not all run. Wait a few seconds and send the unblock again.
  • parameter_invalid (422) naming reason. The reason is missing, blank or longer than 500 characters. Send a reason of 1 to 500 characters. Any other field is refused naming it.
  • resource_not_found (404). The customer is unknown or deleted, or belongs to another environment or account.

Read the activity log​

Each block and each unblock writes an activity entry, customer.blocked and customer.unblocked, with the actor, the reason, the address of the caller and the ID of the event. Each subscription that the block ends writes subscription.canceled, with the system as its actor. Read them with GET /v1/activity-entries?customer={id}. See Flag a customer and read the activity log.

A runnable sample​

The sample creates a customer, a product and a monthly price of 60 SAR, creates a subscription and reports its first invoice paid. It blocks the customer with a reason, reads the subscription until it has ended, reads the event and the activity entries, unblocks the customer and reads the customer. It writes PLACEHOLDER_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 keys and references end in a number made from the time, so that it runs again in the same environment.

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

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

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

SUBSCRIPTION_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: omar-block-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\":\"omar-block-payment-$RUN\"}"

curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/block \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: omar-block-$RUN" \
--data '{"reason":"Repeated chargeback disputes"}'

for attempt in 1 2 3 4 5 6 7 8 9 10; do
STATE=$(curl --silent https://api.black.example/v1/subscriptions/$SUBSCRIPTION \
--header "Authorization: Bearer $TOKEN")
if [[ "$STATE" == *'"status":"canceled"'* ]]; then
break
fi
sleep 1
done

if [[ "$STATE" != *'"status":"canceled"'* ]]; then
echo "The subscription has not ended."
exit 1
fi

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

curl --silent "https://api.black.example/v1/activity-entries?customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN"

curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/unblock \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: omar-unblock-$RUN" \
--data '{"reason":"Reviewed with the hotel"}'

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

Each request answers 200, except the creations, which answer 201. The block answers the customer with its block. The subscription is read until its status is canceled, with the end_reason customer_blocked. The unblock answers the customer with block null, and the last read shows the same.

What to do next​