Error handling
PaygreenJS distinguishes between recoverable errors — issues the buyer can fix themselves (invalid card field, declined card, expired date) — and unexpected errors that interrupt the payment flow (invalid public key, unauthorized domain, network failure).
Recoverable errors are exposed through dedicated events (*_FIELD_ONCHANGE, TOKEN_FAIL, PAYMENT_FAIL), while unexpected failures go through the generic ERROR event.
PaygreenJS surfaces errors through three channels. Each one is meant for a different UX.
| You want to… | Listen to | Fires for |
|---|---|---|
| Show an inline error under a card field | PAN_FIELD_ONCHANGE, EXP_FIELD_ONCHANGE, CVV_FIELD_ONCHANGE | Real-time field validation (typos, expired date, short CVV…). |
| Show a banner when card tokenization fails | TOKEN_FAIL | Card declined by the issuer, 3DS rejection, generic tokenize failure. |
| Show a final "payment failed" screen | PAYMENT_FAIL | The Payment Order reached a terminal failure status. |
| Catch unexpected integration/runtime errors | ERROR | Invalid public key, unauthorized domain, network failure, PSP-side error. |
Inline field errors
While the buyer types in a card field, PaygreenJS fires PAN_FIELD_ONCHANGE, EXP_FIELD_ONCHANGE and CVV_FIELD_ONCHANGE. Each event tells you whether the field is valid and, if not, what went wrong and provides a translated error message ready to display.
Event payload
{
valid: boolean | null, // true when valid, false when invalid, null when incomplete
error?: string, // machine-readable error code (see table below)
message?: string, // same error, translated in the active language
// PAN only:
issuer?: string,
availableIssuers?: string[],
networks?: string[],
}
event.detail.messageis already translated to the language set bylang:ininit()or bysetLanguage(). If you maintain your own copy, ignoremessageand switch onerrorinstead.
Error codes
| Code | Field | When |
|---|---|---|
pan_too_short | PAN | Number is shorter than expected for the detected brand. |
pan_too_long | PAN | Number is longer than expected for the detected brand. |
invalid_pan | PAN | Failed the Luhn check. |
cvv_too_short | CVV | Shorter than 3 digits (or 4 for Amex). |
exp_invalid_format | EXP | Not in MM/YY format. |
exp_expired_date | EXP | Date is in the past. |
Example — display the translated message
const panError = document.querySelector('#pan-error');
const expError = document.querySelector('#exp-error');
const cvvError = document.querySelector('#cvv-error');
paygreenjs.on(paygreenjs.Events.PAN_FIELD_ONCHANGE, (event) => {
panError.textContent = event.detail.message ?? '';
});
paygreenjs.on(paygreenjs.Events.EXP_FIELD_ONCHANGE, (event) => {
expError.textContent = event.detail.message ?? '';
});
paygreenjs.on(paygreenjs.Events.CVV_FIELD_ONCHANGE, (event) => {
cvvError.textContent = event.detail.message ?? '';
});The message refreshes automatically if you call
paygreenjs.setLanguage().
Tokenization failures
TOKEN_FAIL fires when card tokenization fails (issuer decline, 3DS rejection, unexpected tokenize error, etc.).
paygreenjs.on(paygreenjs.Events.TOKEN_FAIL, (event) => {
// event.detail = { error, code, message }
showBanner(event?.detail?.message);
if (event?.detail?.error === 'tokenize_invalid_card') {
// Card was refused — ask the buyer to try another card.
}
});Possible values
| Field | Values |
|---|---|
error | tokenize_invalid_card | tokenize_unauthorized | tokenize_unknown_error |
code | invalid_pan | invalid_cvv | invalid_exp | absent |
message | Translated human-readable message. |
Terminal payment failure
PAYMENT_FAIL fires when a Payment Order reaches a terminal failure state.
paygreenjs.on(paygreenjs.Events.PAYMENT_FAIL, (event) => {
console.log(event?.detail?.error?.message);
});Useful fields available on event?.detail?.error:
method— active payment methodmessage— human-readable error message translated to the active language.
Anything else — the ERROR event
ERROR eventpaygreenjs.on(paygreenjs.Events.ERROR, (event) => {
console.error('PaygreenJS error', event.detail);
});The ERROR event is also emitted when init() fails — for example:
| Cause | event.detail.message |
|---|---|
Missing #paygreen-container | Missing paygreen-container |
| Missing public key | You must provide a valid PayGreen Public Key |
Invalid mode | Invalid mode type |
mode='payment' without paymentOrder.id | You must provide a valid Payment Order ID |
mode='payment' without paymentOrder.objectSecret | You must provide the secret attached to the Payment Order |
mode='instrument' without paymentMethod | Instrument mode require payment method. |
Unsupported paymentMethod | Provided method is not available |
buyer.id missing when required | Buyer is required for authorized instrument... |
Invalid lang | You must provide a valid locale : en,fr,de,es,it |
Updated 3 days ago