Show a signed-in customer their own account
What you will build
You build the pages where a signed-in customer sees their own account: their profile, what they hold, their subscriptions and invoices, and their cards. The customer can change their name, phone and addresses, change their email once they have confirmed the new one, add a card, choose the card that pays their subscriptions and remove a card.
Every call here comes from the customer's own page or app, with the customer's session, and never from your server. No path carries a customer ID: Black answers for the customer the session belongs to, and for no other.
Before you start
You need an app with a publishable key, in the environment where the customer signs in: see Register an app and Create a publishable key. A publishable key is an identifier, not a secret: your page or app sends it in the Black-Publishable-Key header, and a web page is called from one of the app's allowed origins.
You need a way for the customer to sign in. Choose Sign customers in with email and password or Sign customers in with your own sign-in.
To let a customer add a card, the environment needs a connected payment gateway: see Connect a payment gateway. In a test environment the practice gateway needs no gateway account: see The practice gateway.
Steps
Sign the customer in
- Sign the customer in with one of the two guides above. The answer holds the customer's session. Keep its
access_tokenwhere your page or app keeps its session, and send it asAuthorization: Bearer <access token>with the publishable key inBlack-Publishable-Keyin every call below. Renew the pair before the access token ends with Refresh a customer session.
Read the customer's account
-
Read the profile. Send a
GETrequest to/v1/client/customer. The response is200and holds the customer'sname,email,phone,addressandshipping, the ID of the default card, the first page of their subscriptions and of their invoices (ten of each, newest first, each list withhas_moreand theurlthat reads the next page), their active cards with the default first, and what they hold now. See Show the customer of the session. -
Read what the customer holds. Send a
GETrequest to/v1/client/entitlements. The response is200and lists each feature the customer holds now, with its lookup key, its name and the subscription that gives it. The list is never paged. Use it to decide what your page shows. See List the entitlements of the session's customer. -
List the customer's subscriptions. Send a
GETrequest to/v1/client/subscriptions. The response is200and holds a page of the customer's subscriptions of every status, ended ones included. Read one withGET /v1/client/subscriptions/{subscriptionId}. See List the subscriptions of the session's customer. -
List the customer's invoices. Send a
GETrequest to/v1/client/invoices. The response is200and holds a page of the customer's invoices. Read one withGET /v1/client/invoices/{invoiceId}. See List the invoices of the session's customer.
A subscription or an invoice of another customer answers 404 with resource_not_found, the same answer as an ID that does not exist. Both lists page as every list does: limit up to 100, starting_after and ending_before.
Change the customer's profile
- Send the change. Send a
PATCHrequest to/v1/client/customerwith any ofname,phone,addressandshipping. A field you leave out keeps its value, and a field sent empty ornullis cleared. The response is200and holds the profile as it is after the change. See Update the customer of the session.
A customer changes only those four fields and their email, which the next steps explain. Any other field, such as metadata, is refused with 422 and parameter_invalid, naming the field, whatever its value.
Change the customer's email
The new email takes effect only once the customer proves they can read its inbox, so a stolen session cannot move the account to an address the thief owns.
-
Ask for the change. Send a
PATCHrequest to/v1/client/customerwith the newemailand aredirect_urlon one of the app's allowed origins. A customer who has a password also sends it ascurrent_password. A customer who signs in with an email link or with Google sends no password, and their session must have started in the last 10 minutes: for an older session the response is401withsign_in_again_required, so sign the customer in again and repeat the request. The response is200and still shows the old email. Black mails a link to the new address, ending in#black_email_change=<token>after yourredirect_url, and mails the old address to say a change was asked for. The email stays the old one until the link is used. A new request replaces the link of an earlier one. -
Confirm the change. Your page at
redirect_urlreads the token from the address and, in the customer's own session, sends aPOSTrequest to/v1/client/auth/email/changewith thetoken. The response is200and holds the customer with the new email. The new email now signs the customer in, Black mails the old address again to say the email changed, and no session ends. The link counts only in a session of the customer who asked, and only once: in another customer's session, or when it is expired, used or replaced, it is refused with422andlink_invalid, and nothing changes. See Update the customer of the session and Confirm the signed-in customer's new email with its link.
A mobile app (kind ios or android) has no allowed origins, and changes the email in one of two ways. An app registered with return_urls sends a redirect_url exactly equal to one of them, and the mail holds that address with the token as above: test on a device, iOS and Android, that your app receives the whole link, fragment included. An app registered without return_urls sends no redirect_url: one sent by such an app answers 422 with parameter_invalid. Black mails an 8-digit code to the new address instead, such as 1234 5678, which works once and for 24 hours.
To confirm with the code, the app sends a POST request to /v1/client/auth/email/change with the code, in place of the token, in the customer's own session. The response is the same 200. Any failure answers 422 with link_invalid, naming code. Ten requests that send a code in an hour for one customer give 429 with rate_limited. See the mobile section of the email and password guide.
Black accepts three requests with an email for one customer in an hour, and three links to one address in 10 minutes: a fourth is refused with 429 and rate_limited, and sends nothing. A change of the name, phone or addresses alone is not counted. An email that another customer of the environment uses to sign in is refused with 409 and email_in_use, and nothing changes. Sending the email the customer already has cancels a change that waits.
Manage the customer's cards
A card is saved from a single-use token that the payment gateway's own card fields make in the customer's browser or app. The card number, expiry and security code go from the customer straight to the gateway: never send them to Black, and never read or keep them on your page. A request that holds a field named number, card_number, cvc or cvv is refused with 422. See Build a hosted checkout page for the gateway's card fields and the practice gateway's test tokens.
-
List the cards. Send a
GETrequest to/v1/client/payment-methods. The response is200and holds the customer's active cards, the default first. A card that waits for the bank, failed, was removed or isunusableis not in the list. A card isunusablewhen the account removed the gateway connection it was saved through, or replaced it under another prefix: the customer adds a new card. See List the payment methods of the session's customer.When you list the cards, Black first settles each of the customer's cards that waits for the bank and whose bank step is done. A card whose step was approved becomes active and is listed, first when it becomes the default. A card whose step was refused is marked failed and left out. When the gateway does not answer, the card stays waiting and the list still answers
200. -
Add a card. Send a
POSTrequest to/v1/client/payment-methodswith the gateway'stoken, areturn_urlon one of the app's allowed origins and thecurrencythe card is verified in. A customer who has been invoiced before can leavecurrencyout, and Black uses the one of their last invoice. A customer who has never been invoiced must send it, or the request is refused with422namingcurrency: Black verifies the card with a payment of 0 in the currency it will be charged in, an environment can sell in several currencies and charge a customer in their own, and until a first invoice nothing says which one is theirs. The card becomes the default unless you sendset_defaultasfalse. See Create a payment method of the session's customer. The response is one of:
201and the card, active, when the gateway accepts it.201and the card withstatusunusable, when the connection it was saved through was removed, or replaced under another prefix, at that moment. The card is never charged: the customer adds another.202and a card with anext_actionwhen the customer's bank asks them to authenticate. Send the customer tonext_action.redirect_urlin the same tab. The bank sends them back to yourreturn_url, and the card shows in the list once it is active.402withcard_declined, or503withgateway_unavailable. Nothing is saved.
A retry with the same Idempotency-Key answers the card the first try saved and never saves a second.
A mobile app registered with return_urls sends a return_url exactly equal to one of them, and the bank sends the customer to it. A mobile app registered without return_urls sends no return_url: one sent by such an app answers 422 with parameter_invalid. The bank then returns the customer to a page of Black's own, which says only "You can return to the app now", in English or Arabic. Open the bank's address in a browser view the customer can close, then read the card's result with GET /v1/client/payment-methods (step 9): the card is in the list once it is active, and absent when it failed.
-
Choose the default card. Send a
POSTrequest to/v1/client/payment-methods/{paymentMethodId}/default. The response is200and holds the customer's profile with the new default card. Their subscriptions are charged with it from the next charge. A card that is not one of the customer's active cards answers404withresource_not_found. See Set the default payment method of the session's customer. -
Remove a card. Send a
DELETErequest to/v1/client/payment-methods/{paymentMethodId}. The response is200and holds the card as it was. When the card removed is the default and the customer has another active card, the most recently added active card becomes the default. When another of the customer's cards is being removed at the same moment, the response is409withpayment_method_removal_in_progress: try again. See Delete a payment method of the session's customer.
What a customer never sees
A customer never sees what the account keeps for itself: metadata, flags, internal notes and the gateway's own messages. Do not look for them in these answers; your server reads them with its own credentials.
Handle each refusal
Each refusal is an error with a code: see Errors for every code.
401. The session is missing, expired or ended. Ask the customer to sign in again, or renew the session with its refresh token.404resource_not_found. The subscription, invoice or card is not the customer's, or does not exist. Read the list again.401sign_in_again_required. A customer with no password asked to change their email in a session older than 10 minutes. Nothing changed and nothing was sent. Sign the customer in again, then repeat the request.422parameter_invalid. A field is wrong. The error'sparamnames it: a field the customer may not change, areturn_urlthat is not on one of the app's allowed origins, acurrencya customer who was never invoiced left out, or a card field that must never be sent.422link_invalid. The link of an email change is expired, used, replaced or sent in another customer's session. Nothing changed. Ask for the change again.409email_in_use. Another customer of the environment already uses the new email to sign in. Nothing changed and nothing was sent. Ask the customer for another email.409last_payment_method. The card is the customer's only active card, and a subscription of theirs is charged automatically. Add another card first, then remove this one.409payment_method_removal_in_progress. Another of the customer's cards is being removed at this moment, so nothing changed. Try the removal again.402card_declined. The customer's bank refuses the card. Ask for another card.429rate_limit_exceeded. A limit of the Client API is reached, or the customer or their address has too many declined card adds in a short time. Themessagenames the limit, such as "this customer's reads are limited to 20 a second". Wait for the seconds in theRetry-Afterheader, then try again.429rate_limited. The customer asked for a fourth email change in an hour, or a fourth sign-in link was asked for one email in 10 minutes. Wait for the seconds in theRetry-Afterheader, then try again.
The Client API's limits are the same in a test environment as in a live one. A customer's session may send 20 reads and 5 writes a second, and every 429 names the limit it met.
A runnable sample
The sample is a vendor's. It creates a sub-account, an app and its publishable key, turns on the email and password method, connects the practice gateway, and signs a customer up and in. It reads the profile, the entitlements, the subscriptions and the invoices, changes the customer's name, asks for a change of email, lists the cards, adds a card with the practice token tok_simulator_succeeds, adds a second card, makes the first the default and removes the second. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes and the placeholders sk_test_… and pk_test_… where gateway keys go, and needs sed. The email and names end in a number made from the time, the password is for the sample only, and the calls of the customer's session follow one another without waiting, because the Client API's limits are the same in a test environment as in a live one. The sample waits one second twice: before the gateway connection, because the five writes of your own access token fall in one second, and before the default card, because a customer's session takes five writes a second and the sample has made four by then, with two to come. It asks for the email change with the password, shows that the email stays the old one until the link is used, and does not confirm it, because the link arrives by email.
TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
EMAIL="own-account-$RUN@example.com"
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\":\"Own Account $RUN\",\"contact_email\":\"owner@own-account.example\",\"external_reference\":\"own-account-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")
APP=$(curl --silent --request POST https://api.black.example/v1/apps "${H[@]}" \
--data '{"name":"Own Account Demo","kind":"web","allowed_origins":["https://app.example.com"]}' \
| sed -n 's/.*"id":"\(app_[0-9a-z]*\)".*/\1/p')
KEY=$(curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" "${H[@]}" \
| sed -n 's/.*"\(pk_test_[0-9A-Za-z]*\)".*/\1/p')
curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"sign_in":{"password":true}}'
curl --silent --request POST https://api.black.example/v1/gateway-connections "${H[@]}" \
--data '{"gateway":"simulator","secret_key":"sk_test_PLACEHOLDER","prefix":"practice","processing_channel":"pc_practice","public_key":"pk_test_PLACEHOLDER"}'
curl --silent --request POST https://api.black.example/v1/client/auth/sign-up \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data "{\"email\":\"$EMAIL\",\"password\":\"sample-password-one\",\"redirect_url\":\"https://app.example.com/verify\"}"
SIGNED_IN=$(curl --silent --request POST https://api.black.example/v1/client/auth/sign-in \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data "{\"email\":\"$EMAIL\",\"password\":\"sample-password-one\"}")
SESSION=$(echo "$SIGNED_IN" | sed -n 's/.*"access_token":"\([^"]*\)".*/\1/p')
C=(--header "Black-Publishable-Key: $KEY" --header "Origin: https://app.example.com" \
--header "Authorization: Bearer $SESSION" --header "Content-Type: application/json")
PROFILE=$(curl --silent https://api.black.example/v1/client/customer "${C[@]}")
has "$PROFILE" "\"email\":\"$EMAIL\""
curl --silent https://api.black.example/v1/client/entitlements "${C[@]}"
curl --silent https://api.black.example/v1/client/subscriptions "${C[@]}"
curl --silent https://api.black.example/v1/client/invoices "${C[@]}"
CHANGED=$(curl --silent --request PATCH https://api.black.example/v1/client/customer "${C[@]}" \
--data '{"name":"Layla Demo","phone":"+966500000000"}')
has "$CHANGED" '"name":"Layla Demo"'
ASKED=$(curl --silent --request PATCH https://api.black.example/v1/client/customer "${C[@]}" \
--data "{\"email\":\"new-$EMAIL\",\"redirect_url\":\"https://app.example.com/new-email\",\"current_password\":\"sample-password-one\"}")
has "$ASKED" "\"email\":\"$EMAIL\""
curl --silent https://api.black.example/v1/client/payment-methods "${C[@]}"
FIRST=$(curl --silent --request POST https://api.black.example/v1/client/payment-methods "${C[@]}" \
--data '{"token":"tok_simulator_succeeds","return_url":"https://app.example.com/cards","currency":"SAR"}')
CARD_ONE=$(echo "$FIRST" | sed -n 's/.*"id":"\(pm_[0-9a-z]*\)".*/\1/p')
SECOND=$(curl --silent --request POST https://api.black.example/v1/client/payment-methods "${C[@]}" \
--data '{"token":"tok_simulator_succeeds","return_url":"https://app.example.com/cards","currency":"SAR","set_default":false}')
CARD_TWO=$(echo "$SECOND" | sed -n 's/.*"id":"\(pm_[0-9a-z]*\)".*/\1/p')
# A customer may write 5 times a second; the sample has made 4, so it waits a second before the 5th and 6th.
sleep 1
curl --silent --request POST "https://api.black.example/v1/client/payment-methods/$CARD_ONE/default" "${C[@]}"
curl --silent --request DELETE "https://api.black.example/v1/client/payment-methods/$CARD_TWO" "${C[@]}"
The sub-account, the app, its key and the gateway connection answer 201 with the settings change answering 200. The sign-up and the sign-in answer 201. The profile, the entitlements, the subscriptions and the invoices answer 200, as do the change of the name, the request for the email change, which still shows the old email, and the first list of cards. The two cards answer 201, and the default and the removal answer 200.
What to do next
- Read how a customer signs in in Sign customers in with email and password and Sign customers in with your own sign-in.
- Read how the gateway's card fields and the test tokens work in Build a hosted checkout page.
- Read what a customer holds, from your server, in Check what a customer holds.
- Read every error code in Errors.