Connect a payment gateway
What you will build
You connect the payment gateway account of one environment to Black, read the connection, replace its credentials and remove it. You learn what Black checks before it keeps a credential, what Black registers at the gateway for you, who may do each step, and which errors you can meet.
Before you start
You need an access token and a gateway account. See Authentication. In a test environment you can use the practice gateway, simulator, with no gateway account: see The practice gateway. For Checkout.com see Checkout.com.
Each environment has its own connection, so a test environment and a live environment never share one. An environment has one connection at a time.
The money always goes to your own gateway account. Black holds no money and never sees a card number: a card is entered in the gateway's own fields, and Black receives only a token that stands for it.
Steps
-
Connect the gateway. Send a
POSTrequest to/v1/gateway-connectionswithgateway,secret_key,prefix,processing_channelandpublic_key. The response holds the connection, withstatusactiveand thenotification_urlthat Black registered with the gateway. It holds no part of the secret key. -
Read the connection. Send a
GETrequest to/v1/gateway-connections, or to/v1/gateway-connections/{id}. The response shows the gateway, the status, the prefix, the processing channel, the public key and the notification address, and never the secret key. -
Replace the credentials when the gateway account gives you new ones. Send a
POSTrequest to/v1/gateway-connections/{id}/replacewithsecret_key,prefix,processing_channelandpublic_key. Black checks the new credentials first. The response holds the same connection ID with the new settings.When the new credentials are kept, Black registers the notification workflows it needs under them, then removes at the gateway the ones it registered for the old credentials: the one for payments and the one for information requests. You need not delete them in the gateway's dashboard. A workflow Black could not remove stays there, and you may remove it yourself. A failed removal never fails the replacement.
Replace the credentials under the same prefix, with a new secret key, public key or processing channel, and your customers' saved cards stay chargeable. Replace them under another prefix, which for Checkout.com is another client, and the cards saved through the connection become unusable: see below.
-
Remove the connection. Send a
DELETErequest to/v1/gateway-connections/{id}. While subscriptions that Black charges are live, sendconfirmwith the valuetruein the request, or Black refuses the removal with409and the codeconfirmation_required. The response holds the connection withstatusdisconnectedand nonotification_url. Black deletes the stored credentials and the key it made for the gateway's notifications, and sets the payment mode back toaccount_collects. Where the stored credentials still allow it, Black also removes at the gateway the notification workflows it registered for the connection, so you need not delete them in the gateway's dashboard. One Black could not remove stays there, and you may remove it yourself. Black removes only workflows it registered, never one you made, and a failed removal never fails the disconnection.
Know what happens to the saved cards
A gateway keeps a saved card in the account it was saved in, so a card saved through a connection can be charged only through a connection to that same account. When you remove a connection, or replace its credentials under another prefix, Black sets the status of every card saved through it that is active, or that still waits for the bank, to unusable.
An unusable card is final: connecting again, or replacing the credentials back to the old prefix, does not bring it back. It reads unusable when you retrieve it, and it behaves as a card that is not there:
- It leaves the lists of the customer's cards.
- Black never charges it, and a charge sent with no token does not use it.
- It is never chosen as the default, whether you set one, or Black picks the next card after a removal.
- It never counts as another card of the customer.
When an unusable card was a customer's default, Black clears the customer's default_payment_method and records customer.updated, with the card in previous_attributes.default_payment_method, so you can ask the customer for a new card. No other event tells of a card made unusable. See Events.
A card saved while the connection changes follows the same rule. When a customer adds a card through a connection that is disconnected, or that moves to another prefix at that moment, the request answers 201 and the card is unusable. A card that a payment saves, after you connect again, is stored on the new connection.
Know what happens to a payment in flight
Black sends a payment again when an earlier send gave no answer, and checks a payment while it waits on the bank. If the connection changes in between, Black never sends or reads the payment through the new one. The connection has changed when you connect anew, replace the credentials under another prefix (another client, for Checkout.com), or when no connection is active: it is disconnected, or failing. Black does not call the gateway for that payment.
Black holds the payment with its outcome unknown. It never fails it, because the payment may have gone through at the former client. The payment's invoice answers payment_in_progress while the payment is held.
Black tells you once, with the event invoice.payment_outcome_unknown, which holds the invoice as data.object and the facts attempt, amount, currency and connection, the connection the payment went through. Black records an activity entry of the same name on the invoice. See Events.
When your own connection and client are active again, Black reads the held payment again and settles it as any payment that waits on the bank. That happens when you replace the credentials back to the same client, or when a failing connection works again. You then have no need to settle the payment by hand. A payment you already reported as taken is never failed by that read.
Settle a payment whose outcome is unknown
Settle the held payment yourself once you know what became of it, when the connection does not return to the same client. The steps below use the attempt from the event.
- Look the payment up in the gateway's own dashboard, under your former client. Find the payment whose reference is the
attemptfrom the event. The result is that you know whether the gateway took the money, and, if it did, the payment'spay_…ID. - Send
POST /v1/invoices/{invoice}/payment-outcomewith a key that holds the permissioninvoices.mark_paid. The body holdsattempt, the value from the event, andoutcome, eithertakenornot_taken. The response is the invoice as it is now. - When the outcome is
taken, addgateway_payment, thepay_…ID from the former client's dashboard. If Black recorded an ID for the payment, yours must be the same one. Fornot_taken, send nogateway_payment: the request is refused with422.
curl -X POST "$BLACK_API/v1/invoices/inv_…/payment-outcome" \
-H "Authorization: Bearer $BLACK_API_KEY" \
-H "Content-Type: application/json" \
-d '{"attempt": "att_…", "outcome": "taken", "gateway_payment": "pay_…"}'
When the outcome is taken, Black records the payment and pays the invoice, and tells you with the event invoice.paid. When the outcome is not_taken, the payment fails with the reason connection_changed, Black tells you with the event invoice.payment_failed, and the invoice can be paid again.
Send the report with an Idempotency-Key, so that you can repeat it safely. A repeat of a report that settled the payment answers exactly as the first did, marked with the Idempotent-Replayed header, and records nothing again. Sending the same key with a different body once the payment is settled is refused with 409 and the code idempotency_key_reused. A report that Black refused is judged again when you repeat it, and gets the same status and error under its own request ID. When Black cannot reach its calculation service and answers 503, send the same report again: Black keeps your claim, and the repeat settles the payment.
Black refuses the request in these cases, and changes nothing:
409with the codepayment_outcome_known: the payment is already settled, by an earlier report or by Black.409with the codepayment_in_progress: Black can still reach the payment through the active connection, or another report for the same payment is being applied. Wait a moment, then read the invoice again.409with the codepayment_reference_in_use: thepay_…ID already belongs to another payment. Check that you copied the right ID.404: the invoice, or theattempt, is not in this environment.
A payment you settle as taken stays at the former client. If you refund it later, Black refuses with payment_at_former_client: see the next section.
Refund a payment taken before the connection changed
A refund goes only to the client that holds the payment. When you send POST /v1/invoices/{invoice}/refunds for a payment taken through a former connection, or under a former Checkout.com client, Black refuses it with 409 and the code payment_at_former_client. It records nothing and sends nothing to the gateway. The same answer comes when that former connection is disconnected and no connection is active. Refund the payment at the former client, in the gateway's own dashboard, then record the refund in Black: see the next section. A payment whose connection is still the active one is refunded as before.
A refund that Black had accepted before the connection changed is held. It stays pending, Black never sends it to the new client, and its amount stays counted against what is left to refund. Black tells you once, with the event invoice.refund_outcome_unknown, which holds the invoice as data.object and the facts refund, amount, currency and reason, here connection_changed. Black records an activity entry of the same name on the invoice. See Events.
Record a refund you made at the former client
Record the refund yourself once you have made it at the former client, so that the invoice shows it as refunded. Black makes no call to the gateway for it.
- Refund the payment in the gateway's dashboard, under your former client. The result is the refund's action ID,
act_…for Checkout.com. - Send
POST /v1/invoices/{invoice}/refundswith a key that holds the permissioninvoices.refund, and addgateway_action, theact_…ID from the dashboard, to the usual body. The response is the refund, recorded assucceeded, and the invoice shows it as refunded.
curl -X POST "$BLACK_API/v1/invoices/inv_…/refunds" \
-H "Authorization: Bearer $BLACK_API_KEY" \
-H "Content-Type: application/json" \
-d '{"amount": 500, "gateway_action": "act_…"}'
Without gateway_action, a refund of such a payment is refused with 409 and the code payment_at_former_client. Black refuses the request in these cases, and changes nothing:
422: the invoice's payment is one Black can still refund itself, sogateway_actionhas no place in the request, or the ID is not in the gateway's form.409with the codepayment_reference_in_use: another refund of the environment holds thatact_…ID. Check that you copied the right one.
Settle a refund that is held
A refund that Black held across the change of client stays pending, and its amount stays counted against what is left to refund. Settle it yourself once you know what became of it.
- Look the refund up in the gateway's dashboard, under your former client. Find the refund whose reference is its
re_…ID. The result is that you know whether the former client took it, and, if it did, the refund'sact_…ID. - Send
POST /v1/invoices/{invoice}/refund-outcomewith a key that holds the permissioninvoices.refund. The body holdsrefund, there_…ID, andoutcome, eithersucceededorfailed. The response is the refund as it is now. - When the outcome is
succeeded, addgateway_action, theact_…ID from the dashboard. Forfailed, send nogateway_action: the request is refused with422.
curl -X POST "$BLACK_API/v1/invoices/inv_…/refund-outcome" \
-H "Authorization: Bearer $BLACK_API_KEY" \
-H "Content-Type: application/json" \
-d '{"refund": "re_…", "outcome": "succeeded", "gateway_action": "act_…"}'
When the outcome is succeeded, the refund uses the amount it held and the invoice shows it as refunded. When the outcome is failed, Black releases the amount, so the invoice can be refunded again. Black tells you of either with the event refund.updated.
Black refuses the request in these cases, and changes nothing:
409with the coderefund_outcome_known: the refund is already settled.409with the codepayment_in_progress: Black can still reach the payment through the active connection, or a send of the refund is under way. Wait a moment, then try again.409with the codepayment_reference_in_use: theact_…ID already belongs to another refund.404: the invoice, or the refund, is not in this environment.
A refund that the former client accepted, and that is held when the client changes, is told to you once with the event invoice.refund_outcome_unknown, with the reason connection_changed.
The connection's status is active while the gateway accepts the credentials, failing when the gateway refuses the stored ones, and disconnected after step 4. Black sends the events gateway_connection.created, gateway_connection.updated and gateway_connection.disconnected when each change happens, with the connection as data.object and never a secret. See Events.
Check what Black checks and keeps
Black asks the gateway to accept the credentials for the environment's kind before it keeps them. A test environment takes the gateway's test credentials, and a live environment its live ones. When the gateway refuses them, or does not answer, Black keeps nothing and sends an error, so you can send the request again.
Black then registers its own address for the gateway's notifications, with a signing key that Black makes. You copy no address and no key.
Black keeps the secret key and the signing key encrypted, and never shows either again. It writes neither in an answer, an event, the activity log or a log line. Only the prefix, the processing channel and the public key come back, because the public key is public.
The three settings besides the secret key belong to the gateway account:
| Field | What it holds |
|---|---|
prefix | Letters and digits that name your own address at the gateway. |
processing_channel | The gateway's identifier of the channel that processes your payments. |
public_key | The key your page uses to collect a card in the gateway's fields. |
Choose who connects and who changes the payment mode
The environment's own client, or its vendor with Black-Account, connects, replaces and removes a gateway. A team member who signs in to the console needs the permission gateways.manage, which the Owner and Admin roles hold: see Sign in, set roles and invite your team.
Only the vendor changes the payment mode, as with the other settings that Change account settings lists. The mode says who charges the customer:
| Mode | Who charges | What tells Black the result |
|---|---|---|
account_collects | You, with your own gateway, outside Black. This is the default. | Your report of the payment: see Create a subscription and report its payments. |
black_charges | Black, through the environment's connection. | The gateway's answer and its notification. |
The vendor switches an environment to black_charges once the environment has an active connection. Without one, Black refuses the switch with 409 and the code gateway_not_connected. Removing the connection sets the mode back to account_collects. Connecting a gateway changes nothing in how an account that collects is billed: the mode decides who charges.
Fix an error
gateway_already_connected(409). The environment already has a connection. Replace its credentials, or remove it before you connect another.gateway_credentials_invalid(422). The gateway refused the credentials, and Black kept nothing.paramissecret_key. Check the secret key, the prefix, the processing channel and the public key, then send the request again.gateway_credentials_wrong_environment(422). The credentials belong to the other side of your gateway account, its test side or its live side. Send test credentials to a test environment and live credentials to a live one.gateway_credentials_missing_permission(422). The gateway accepted the key, but the key lacks a permission that Black uses. Give the key the permissions that Checkout.com describes, then connect again.gateway_unavailable(503). The gateway did not answer, and Black kept nothing. Send the request again later.setting_set_by_vendor(403). The environment's own client tried to change the payment mode. Ask your vendor to change it.gateway_not_connected(409). You setpayment_modetoblack_chargesin an environment with no active connection. Connect a gateway, or replace the credentials of a failing connection, then send the change again.
A request with a field that is not in the list above is refused with 422 and the code parameter_invalid, naming the field.
A runnable sample
The first request comes from the client of an account. It tries to change the payment mode, and Black refuses it with 403. The sample writes PLACEHOLDER_ACCESS_TOKEN where the access token of the account's own client goes:
curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--data '{"payment_mode":"black_charges"}'
The next sample is the vendor's. It creates a sub-account, connects the practice gateway to its test environment, reads the connection, connects a second one and is refused, replaces the credentials, switches the payment mode to black_charges, removes the connection, then tries black_charges again and is refused. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes and the placeholders sk_test_… and pk_test_… where gateway keys go. A test environment takes five writes a second, so the sample waits a second after each write.
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; }; }
lacks() { [[ "$1" != *"$2"* ]] || { echo "The answer holds: $2"; exit 1; }; }
ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts "${H[@]}" \
--data "{\"name\":\"Gateway $RUN\",\"contact_email\":\"owner@gateway.example\",\"external_reference\":\"gateway-$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" '"status":"active"'
has "$CONNECTED" '"notification_url":"'
lacks "$CONNECTED" 'sk_test_PLACEHOLDER'
CONNECTION=$(echo "$CONNECTED" | sed -n 's/.*"id":"\(gwc_[0-9a-z]*\)".*/\1/p')
READ=$(curl --silent https://api.black.example/v1/gateway-connections/$CONNECTION "${H[@]}")
has "$READ" '"gateway":"simulator"'
lacks "$READ" 'secret_key'
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"}'
REPLACED=$(curl --silent --request POST https://api.black.example/v1/gateway-connections/$CONNECTION/replace "${H[@]}" \
--data '{"secret_key":"sk_test_PLACEHOLDER_TWO","prefix":"practice","processing_channel":"pc_second","public_key":"pk_test_PLACEHOLDER_TWO"}')
has "$REPLACED" "\"id\":\"$CONNECTION\""
has "$REPLACED" '"processing_channel":"pc_second"'
SWITCHED=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"payment_mode":"black_charges"}')
has "$SWITCHED" '"payment_mode":"black_charges"'
REMOVED=$(curl --silent --request DELETE https://api.black.example/v1/gateway-connections/$CONNECTION "${H[@]}")
has "$REMOVED" '"status":"disconnected"'
has "$REMOVED" '"notification_url":null'
REFUSED=$(curl --silent --request PATCH https://api.black.example/v1/settings "${H[@]}" \
--data '{"payment_mode":"black_charges"}')
has "$REFUSED" '"code":"gateway_not_connected"'
The account is created and the connection made with 201. The read, the replace, the switch to black_charges and the removal answer 200. The second connection answers 409 with gateway_already_connected, and the switch to black_charges after the removal answers 409 with gateway_not_connected, because the environment has no connection any more.
What to do next
- Read The practice gateway to try every outcome of a payment without a gateway account.
- Read Checkout.com for what the connection needs from your Checkout.com account.
- Read Change account settings for the other settings only the vendor changes.
- Read Events for the events of a gateway connection.