Listen for checkout events
Register a callback for the events a checkout emits, from the form being ready to a payment's result.
The checkout.on() method registers a callback for an event the checkout emits, such as the form becoming ready or a payment succeeding. Use it to update your page as the checkout changes.
checkout.on(checkout.EVENT_TYPES.PAYMENT_SUCCESS, (payment) => {
console.log('Paid:', payment.transaction)
})Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
eventName | checkout.EVENT_TYPES value | Yes | The event to listen for. Each value is the event's name. For example, checkout.EVENT_TYPES.PAYMENT_SUCCESS is 'payment_success'. In TypeScript, pass the EVENT_TYPES value. |
callback | (payload) => void | Yes | Called each time the event is emitted, with the event's payload when it has one. |
checkout.once() takes the same parameters and calls the callback only the first time. checkout.removeListener(eventName, callback) removes a callback. Listeners stay registered for the page, across pay.checkout() calls, until you remove them.
Events
The checkout emits events for payments, setup sessions, the session, and the form.
Payments
Payment events follow each payment attempt, from its start to its result.
EVENT_TYPES | Event | Payload | When |
|---|---|---|---|
PAYMENT_PROCESSING | payment_processing | None | A payment starts. The customer submits the form or taps a wallet or payment method button, or you call checkout.headless() or checkout.pay(). |
PAYMENT_SUCCESS | payment_success | The PaymentResult object, matching what onSuccess receives. | A payment succeeds. |
PAYMENT_PENDING | payment_pending | The payment, as for payment_success. | A payment was accepted but isn't final yet. Confirm its status with webhooks. |
PAYMENT_FAILURE | payment_failure | The PaymentError object ({ message, data }), matching what onFailure receives. | A payment attempt fails. |
PAYMENT_PENDING_APPROVAL | payment_pending_approval | The payment, as the API returned it. | In Universal checkout, a payment came back pending, and the form keeps checking until its status is final. |
pay.checkout() describes PaymentResult and PaymentError. checkout.pay() doesn't call onSuccess, onPending, or onFailure, but it emits these events.
Setup sessions
A setup session saves a payment method without charging it. These events mark its progress.
EVENT_TYPES | Event | Payload | When |
|---|---|---|---|
SETUP_PROCESSING | setup_processing | None | In a Universal checkout setup session, the customer submits the form. |
SETUP_SUCCESS | setup_success | The setup result, matching what onSuccess receives. | In a Universal checkout setup session, the payment method is saved. |
The session
Both session events mean the session can't take another payment.
EVENT_TYPES | Event | Payload | When |
|---|---|---|---|
SESSION_EXPIRED | session_expired | None | In Universal checkout, the session's client_secret expired, so the form can't take a payment anymore. |
MAX_ATTEMPTS_REACHED | max_attempts_reached | The error of the last attempt. | In Universal checkout, the session's last attempt failed, so the session can't be paid again. This event comes before that attempt's payment_failure. |
The form
Form events report when the checkout loads, and when the form's validity, card funding type, or enabled state changes.
EVENT_TYPES | Event | Payload | When |
|---|---|---|---|
CONTENT_READY | content_ready | None | The Universal checkout form has loaded. Call checkout.updateMetadata() after it. |
HEADLESS_READY | headless_ready | None | Headless checkout has loaded the session. |
FORM_VALIDATION_UPDATED | form_validation_update | { validationResult }, where validationResult is true when every field is valid. | The Universal checkout form becomes valid or invalid. |
CARD_FUNDING_TYPE_UPDATE | card_funding_type_update | { fundingType }, for example 'debit', or null when unknown. | In Universal checkout, the funding type of the card being entered or selected changes. The SDK looks it up only while you listen for this event, and then passes it to onValidateSession as fundingType. |
SET_DISABLE_UI | set_disable_ui | { data }, where data is true when the form was disabled and false when it was enabled. | The Universal checkout form was disabled or enabled, for example by setDisabled() on the object checkout.universal() returns. |
Example
checkout.on(checkout.EVENT_TYPES.PAYMENT_PROCESSING, () => {
showSpinner()
})
checkout.on(checkout.EVENT_TYPES.PAYMENT_SUCCESS, (payment) => {
hideSpinner()
console.log('Paid:', payment.transaction)
})
checkout.on(checkout.EVENT_TYPES.PAYMENT_FAILURE, (error) => {
hideSpinner()
console.log('Payment failed:', error.message)
})