The practice gateway
What it does
The practice gateway, simulator, is a payment gateway that Black provides in every test environment. It answers at once, from the token it receives, with the outcome that token stands for. It calls no one and moves no money, so you can connect a gateway and read every token and its outcome before you hold a gateway account.
Once the practice gateway is connected, you can set the payment mode black_charges, and Black charges through it: every payment, renewal and retry gets the outcome of its token. You send a token as the payment_token of a new subscription, or to POST /v1/invoices/{invoice}/pay for an invoice that is open. Without an active connection, Black refuses black_charges with 409 and the code gateway_not_connected. See Connect a payment gateway.
A token stands in for a card. The practice gateway has no card fields, and Black never takes a card number.
Before you start
You need an access token for a test environment. See Authentication. The practice gateway does not exist in a live environment: a live environment that asks for it is refused, and the list of tokens answers 404 with resource_not_found.
Connect it
-
Send a
POSTrequest to/v1/gateway-connectionswithgatewayset tosimulator. The practice gateway accepts anysecret_key,prefix,processing_channelandpublic_key, so you can send the placeholderssk_test_…andpk_test_…. The response holds the connection, withstatusactive. -
Read the tokens. Send a
GETrequest to/v1/test-helpers/simulator-tokens. The response lists every token, with what a payment answers with the customer present and with the customer absent.
Connect a payment gateway walks through replacing and removing the connection.
Configuration reference
| Setting | What it does | Values it accepts | Default |
|---|---|---|---|
gateway | Picks the practice gateway. | simulator, in a test environment only | None: required |
secret_key | Stands in for a secret key. Black keeps it encrypted and never shows it. | Any text of up to 255 characters | None: required |
prefix | Stands in for the prefix of your address at a gateway. | Letters and digits, up to 64 | None: required |
processing_channel | Stands in for a processing channel. | Any text of up to 255 characters | None: required |
public_key | Stands in for a public key. | Any text of up to 255 characters | None: required |
What is supported
- A connection that the environment holds, read, replaced and removed like any gateway's, with its
notification_url. - A list of the tokens, with what each answers with the customer present and with the customer absent.
- Connections kept apart: two environments, or two accounts, never see each other's saved methods or payments.
It does not support a live environment, a card number, a payment today, or a call to a real gateway.
Test data
The practice gateway reads these tokens. Any other token is declined with incorrect_number. The sample after the table checks every row against the API, so the table and the API cannot differ without the check failing.
| Token | Customer present | Customer absent | Decline reason | Renewable |
|---|---|---|---|---|
tok_simulator_succeeds | succeeded | succeeded | None | Yes |
tok_simulator_declined | declined | declined | card_declined | No |
tok_simulator_insufficient_funds | declined | declined | insufficient_funds | No |
tok_simulator_expired | declined | declined | expired_card | No |
tok_simulator_requires_action | requires_action | succeeded | None | Yes |
tok_simulator_saves_then_fails | succeeded | declined | card_declined | Yes |
tok_simulator_wallet_mada | succeeded | customer_must_be_present | None | No |
A renewable token's saved method can be charged when the customer is absent. tok_simulator_saves_then_fails is for a first payment that works and a renewal that fails. tok_simulator_wallet_mada stands for a mada wallet, which a gateway cannot charge again without the customer.
The sample lists the tokens as the account's own client and checks each token and its outcomes, then connects the practice gateway and removes it. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes. An environment holds one connection, so the sample removes the one it makes.
TOKEN=PLACEHOLDER_ACCESS_TOKEN
H=(--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json")
has() { [[ "$1" == *"$2"* ]] || { echo "The answer does not hold: $2"; exit 1; }; }
TOKENS=$(curl --silent https://api.black.example/v1/test-helpers/simulator-tokens "${H[@]}")
[[ "$(echo "$TOKENS" | grep -o '"object":"simulator_token"' | wc -l)" -eq 7 ]] || { echo "The list does not hold 7 tokens."; exit 1; }
has "$TOKENS" '"token":"tok_simulator_succeeds","customer_present":"succeeded","customer_absent":"succeeded","decline_reason":null,"renewable":true'
has "$TOKENS" '"token":"tok_simulator_declined","customer_present":"declined","customer_absent":"declined","decline_reason":"card_declined","renewable":false'
has "$TOKENS" '"token":"tok_simulator_insufficient_funds","customer_present":"declined","customer_absent":"declined","decline_reason":"insufficient_funds","renewable":false'
has "$TOKENS" '"token":"tok_simulator_expired","customer_present":"declined","customer_absent":"declined","decline_reason":"expired_card","renewable":false'
has "$TOKENS" '"token":"tok_simulator_requires_action","customer_present":"requires_action","customer_absent":"succeeded","decline_reason":null,"renewable":true'
has "$TOKENS" '"token":"tok_simulator_saves_then_fails","customer_present":"succeeded","customer_absent":"declined","decline_reason":"card_declined","renewable":true'
has "$TOKENS" '"token":"tok_simulator_wallet_mada","customer_present":"succeeded","customer_absent":"customer_must_be_present","decline_reason":null,"renewable":false'
CONNECTED=$(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"}')
has "$CONNECTED" '"gateway":"simulator"'
CONNECTION=$(echo "$CONNECTED" | sed -n 's/.*"id":"\(gwc_[0-9a-z]*\)".*/\1/p')
curl --silent --request DELETE https://api.black.example/v1/gateway-connections/$CONNECTION "${H[@]}"
The list holds seven tokens, and the sample fails when it does not. The tokens answer 200, the connection 201, and its removal 200.
Answer the practice bank's step
tok_simulator_requires_action stands for a card whose bank asks the customer to authenticate. A payment with it, made while the customer is present, is not settled at once: the payment answers requires_action, and its next_action holds a redirect_url. With the practice gateway, that address is a page Black serves, the practice bank. A real bank's page takes its place when you connect a real gateway.
-
Send the customer to the
redirect_url, in the same tab, as you would send them to a real bank. The address is outside/v1and needs no API key, because the customer opens it in a browser. It is signed, so a changed address is refused, and it stays valid for 25 hours. -
Choose the answer on the page. The practice bank offers two buttons, Approve and Fail. Approve makes the payment succeed, and the subscription becomes active. Fail declines the payment with
authentication_failed. -
The customer returns to your page. After either answer, Black sends the customer to the success address when the payment succeeded and to the failure address when it did not, and adds
simulator_payment_idto the address. A payment made with no return address shows a page that says the answer was sent. A charge that your server makes with the Server API, withPOST /v1/invoices/{invoice}/payor as the first payment or the upgrade-now charge of a subscription, and that sends noreturn_url, brings the customer, after the bank's step, to Black's own page at/bank-steps/return, which says "You can return to the app now". With areturn_url, the customer returns to it as before. Areturn_urlthat is empty is refused with422.
Black learns the answer the way it learns a real bank's: the practice gateway sends its notification, and Black settles the payment from it. The payment can therefore stay waiting for a short while after the customer is back, so read the payment or the checkout session again, as for any bank's step, and do not read the return as proof of payment. The first answer is kept: opening the step again and choosing the other button settles nothing a second time.
The practice bank exists only in a test environment, for a payment made through the practice gateway's connection. In a live environment, or for a real gateway's connection, its address answers 404 with resource_not_found. When the customer is absent, the same token succeeds at once and needs no step.
Send the practice gateway's notices
The practice gateway tells Black what happens to a payment through notices, as a real gateway does. Each notice is a JSON body with an id, which is its event ID, and a type. The practice gateway sends these types:
| Type | What it tells | Fields in its body |
|---|---|---|
payment_settled | The final answer of a payment. | reference, outcome |
refund_settled | A refund paid out, or refused with a reason. | payment, refund, reference, accepted, reason, amount, currency |
dispute_opened | A dispute of a payment. | payment, dispute, amount, currency, category, reason_code, evidence_due_by |
dispute_updated | A later state of a dispute. | dispute, status, category, reason_code, evidence_due_by, payment, amount, currency, arbitration |
information_requested | A bank's request for information about a payment. | payment, request, category, reason_code, respond_by |
category, reason_code and evidence_due_by are optional in a dispute notice, and evidence_due_by and respond_by are ISO 8601 times. Black refuses a time that is not in a date form. dispute_updated can leave status empty, for a notice that changes the details and no state. Set arbitration to true and its status is the outcome of an arbitration, which can move a dispute that was settled as lost to won. A notice of any other type is kept and ignored.
information_requested opens no dispute. For a payment that Black took, Black records invoice.payment_information_requested with the request, the category, the reason code and the time to answer by.
Every notice reaches the notification_url of the connection, signed with a key that Black made for the connection. You never hold that key, so a body you post to that address yourself is refused with 401 and notification_signature_invalid. To send a notice, ask Black to make the practice gateway send it.
Send a notice from the practice gateway
Send a POST request to /v1/gateway-connections/{id}/simulated-notices, with the ID of the practice gateway's connection. The route works in a test environment only, and your access token needs the permission gateways.manage. The body has a type, which is dispute_opened, dispute_updated or information_requested. The other types of the table settle a payment or a refund, which Black does itself, so you do not send them.
| Type | Fields you send |
|---|---|
dispute_opened | invoice, required, the ID of an invoice whose payment the practice gateway took. amount, optional, from 1 up to the payment's amount, and all of it when you leave it out. category, reason_code and evidence_due_by, optional. |
dispute_updated | dispute, required, the ID (dp_…) of a dispute that the practice gateway told Black of. status, arbitration, category, reason_code and evidence_due_by, of which you send at least one. arbitration goes with a status only. |
information_requested | invoice, required, as for a dispute. category, reason_code and respond_by, optional. |
category and reason_code hold 1 to 64 characters. evidence_due_by and respond_by are ISO 8601 times. A field that the type does not take is refused with 422 that names it.
The answer is 202, with the notice as Black received it, in a gateway_notification object. Black then processes the notice as it processes a real one, in the background, so the events follow a moment later: read them again, as you do after a bank's step. A repeat of the request with the same Idempotency-Key is answered again and sends nothing new. Each request without one sends a new notice.
The effects are real in that test environment. A dispute opened records dispute.created, flags the customer and ends their subscription. A later state of a dispute records dispute.updated. A request for information records invoice.payment_information_requested and opens no dispute.
Black refuses a request in these cases:
403withtest_mode_required. The environment is live.404withresource_not_found. The environment has no such connection, or the connection is not the practice gateway's. A404that namesinvoiceordisputemeans the environment has no such invoice or dispute, or the payment is not one that this connection took.409withgateway_not_connected. The connection is disconnected.409withinvoice_not_paid. The invoice has no payment to dispute.422that namesinvoice. You reported the payment yourself, so the practice gateway has no payment to dispute.
The sample is a vendor's, because only the vendor sets the payment mode. It creates a sub-account, connects the practice gateway and sets the payment mode to black_charges. It creates a customer, a product and a monthly price of 30 SAR, and starts a subscription with tok_simulator_succeeds, which pays the first invoice. It sends a dispute_opened notice for that invoice, reads the events of dispute.created, up to ten times, until one names the invoice, and removes the connection.
TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM
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\":\"Notices $RUN\",\"contact_email\":\"owner@notices.example\",\"external_reference\":\"notices-$RUN\"}" \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
H+=(--header "Black-Account: $ACCOUNT")
CONNECTED=$(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"}')
has "$CONNECTED" '"gateway":"simulator"'
CONNECTION=$(echo "$CONNECTED" | sed -n 's/.*"id":"\(gwc_[0-9a-z]*\)".*/\1/p')
curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"payment_mode":"black_charges"}'
CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers "${H[@]}" \
--data "{\"name\":\"Omar\",\"email\":\"omar-$RUN@notices.example\"}" \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')
PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products "${H[@]}" \
--data "{\"name\":\"Prime Notices $RUN\"}" \
| sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')
PRICE=$(curl --silent --request POST https://api.black.example/v1/prices "${H[@]}" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":3000,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')
SUBSCRIBED=$(curl --silent --request POST https://api.black.example/v1/subscriptions "${H[@]}" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$PRICE\"}],\"payment_token\":\"tok_simulator_succeeds\"}")
INVOICE=$(echo "$SUBSCRIBED" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')
NOTICE=$(curl --silent --request POST https://api.black.example/v1/gateway-connections/$CONNECTION/simulated-notices "${H[@]}" \
--data "{\"type\":\"dispute_opened\",\"invoice\":\"$INVOICE\",\"reason_code\":\"10.4\"}")
has "$NOTICE" '"object":"gateway_notification"'
for attempt in 1 2 3 4 5 6 7 8 9 10; do
EVENTS=$(curl --silent "https://api.black.example/v1/events?type=dispute.created" "${H[@]}")
if [[ "$EVENTS" == *"$INVOICE"* ]]; then
break
fi
sleep 1
done
if [[ "$EVENTS" != *"$INVOICE"* ]]; then
echo "No dispute.created event names the invoice."
exit 1
fi
curl --silent --request DELETE https://api.black.example/v1/gateway-connections/$CONNECTION "${H[@]}" \
--data '{"confirm":true}'
The sub-account, the connection, the customer, the product, the price and the subscription answer 201, and the settings change and the removal answer 200. The notice answers 202. The events are read until a dispute.created event names the invoice.
Events it produces
The practice gateway produces the events of a gateway connection, as any gateway does: gateway_connection.created when you connect it, gateway_connection.updated when you replace its credentials, and gateway_connection.disconnected when you remove it. See Events. It can also send a bank's request for information about a payment, which Black tells as invoice.payment_information_requested, and the dispute notices that Black reads, which record dispute.created and dispute.updated. You can send these notices yourself, as Send a notice from the practice gateway shows.
Going live
The practice gateway has no live form. Before you go live, connect a real gateway to the live environment, such as Checkout.com, with that gateway's live credentials. The live environment's connection is its own, and nothing from the test environment moves to it.
Limits and troubleshooting
resource_not_found(404) from the list of tokens. The environment is live. Use a test environment.parameter_invalid(422) naminggateway, when you connectsimulatorto a live environment. The practice gateway exists in test environments only. Connect a real gateway instead.gateway_already_connected(409). The environment has a connection already. Replace its credentials, or remove it first.parameter_invalid(422) naming another field. A required field is missing or too long.
For the errors of replacing and removing a connection, see Connect a payment gateway.