Handle Senfenico API errors
Every response is a success, a refusal or an unknown outcome. A refusal is not resent as is, and an unknown outcome is checked before any new attempt.
The response envelope
Section titled “The response envelope”Success:
{ "status": true, "message": "Charge attempted", "data": { "reference": "CH261126-101502-ORANGE_BF-8b41079ee8", "status": "send_otp" }}Business or validation failure:
{ "status": false, "message": "Invalid data provided. Please check the input and try again.", "error_code": "INVALID_DATA", "errors": { "amount": ["Ensure this value is greater than or equal to 100."], "payment_method_details": { "phone": ["Phone number must be 8 digits long."] } }}- Base your logic on
error_code, never onmessage(technical English text that may change). errorsis only present for validation errors; it can be nested.- Responses have no
descriptionordescription_frfield. The text shown to the customer is yours to write.
HTTP code and status: check both
Section titled “HTTP code and status: check both”| HTTP | Typical cases | Body |
|---|---|---|
200 |
Successful read; result of POST /charges/submit (success or failure); charge initialization refused by the operator |
envelope, status true or false |
201 |
Charge, checkout or transfer created | envelope, status: true |
202 |
Bulk transfer accepted (see failed_transfers) |
envelope |
400 |
INVALID_DATA, BATCH_SIZE_LIMIT_EXCEEDED, UNSUPPORTED_PROVIDER |
envelope + optional errors |
403 |
Missing or invalid key; IP_NOT_ALLOWED; MERCHANT_INSUFFICIENT_FUNDS; more rarely UNAUTHORIZED_CHARGE_PROVIDER |
envelope, status as a string for a refused key |
404 |
Object not found, or created in the other mode (test/live) | envelope, sometimes without error_code |
429 |
Too many requests | {"detail": "…"}, not the envelope |
500 |
INTERNAL_ERROR, or an unexpected error |
envelope, or an HTML page |
A refused key returns "status": "False" as a string: compare strictly to true and test the HTTP code first (see Missing or invalid key).
Classify every response
Section titled “Classify every response”| Outcome | Condition | Action |
|---|---|---|
| Success | HTTP 2xx and status === true |
carry on |
| Refused | HTTP 400, 403, 404, or HTTP 200 with status: false |
don’t resend the same request; fix it or tell the customer |
| Unknown | timeout, network failure, HTTP 5xx, non-JSON body |
check before any new create |
| Slow down | HTTP 429 |
wait, then retry |
/** 'ok' | 'refused' | 'unknown' | 'throttled' */function classify(int $http, ?array $json): string{ if ($http === 0 || $http >= 500 || $json === null) { return 'unknown'; // network, 5xx, HTML } if ($http === 429) { return 'throttled'; } if ($http >= 200 && $http < 300 && ($json['status'] ?? null) === true) { return 'ok'; } return 'refused';}def classify(http: int, body) -> str: """'ok' | 'refused' | 'unknown' | 'throttled'""" if http == 0 or http >= 500 or not isinstance(body, dict): return "unknown" # network, 5xx, HTML if http == 429: return "throttled" if 200 <= http < 300 and body.get("status") is True: return "ok" return "refused"/** 'ok' | 'refused' | 'unknown' | 'throttled' */export function classify(http, body) { if (!http || http >= 500 || typeof body !== 'object' || !body) { return 'unknown'; // network, 5xx, HTML } if (http === 429) return 'throttled'; if (http >= 200 && http < 300 && body.status === true) return 'ok'; return 'refused';}Error codes and what to show
Section titled “Error codes and what to show”Base your logic on error_code. For each code, Error codes gives the cause, a message to show the customer and what to do next. Never show the raw message, which is technical English.
Two cases need special care. CHARGE_NOT_FOUND after /charges/submit can mean the charge is already paid, so re-read it before showing anything. MERCHANT_INSUFFICIENT_FUNDS (HTTP 403) is about your own balance, and no transfer was created.
Timeout while creating a charge
Section titled “Timeout while creating a charge”Creating a charge calls the operator and can take several tens of seconds. Set a 90 s timeout on charge creation and OTP submission, never less than 60 s (see Limits), and 20 s on reads. If your code gives up before the response, the charge may exist, and resending it would create a second one. Search for it by ext_id first, following Search before recreating, which gives the code and also covers transfers, /charges/submit and checkouts.
Checklist
Section titled “Checklist”- Every response is classified: success (
2xxandstatus === true), refused, unknown, slow down. -
statusis compared strictly totrue(authentication errors return the string"False"). - Non-JSON bodies (
429, HTML500) don’t crash your code. - Logic relies on
error_code, and the customer sees a message you wrote. - A charge initialization with HTTP
200andstatus: falseis treated as a failure. - After an unknown outcome, you search by
ext_idbefore recreating a charge or transfer. -
CHARGE_NOT_FOUNDafter a submit triggers a re-read of the charge.