Create a checkout
Bind a checkout to an authorized session and register callbacks that receive the payment result.
The pay.checkout() method binds a checkout to an authorized session and registers the callbacks that receive the result of the payment flow. Call this before rendering a form or submitting a payment.
const checkout = pay.checkout(options)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
clientSecret | string | () => Promise<string> | Yes | The client_secret returned from creating a session on your server, or an async function that returns it. Authorizes the checkout on the client. |
onSuccess | (payment: PaymentResult) => void | No | Callback fired when the checkout flow completes successfully in the browser. |
onFailure | (error: PaymentError) => void | No | Callback fired when the checkout flow encounters an error. |
onPending | (payment: PaymentResult) => void | No | Callback fired instead of onSuccess when the payment is accepted but not final yet (for example, some alternative payment methods). Confirm the final status with webhooks. |
locale | string | No | Checkout language, for example 'de-DE'. See Localization. |
throwOnSubmitFailure | boolean | No | When true, checkout.submit() rejects if the payment fails. Default: true. |
mode | 'integrated' | 'modal' | No | Controls whether the form renders inline in the container passed to checkout.universal() ('integrated') or opens in a modal from a placed button ('modal'). Default: 'integrated'. |
toggles | object | No | Checkout-wide display options. These are separate from the toggles of checkout.universal(). |
toggles.displayFailureMessages | boolean | No | Show a failed attempt's message in the form. The customer closes it to try again. Default: false. |
toggles.displayEndOfSessionFailureMessages | boolean | No | Show a message in the form when the session expires or has no attempts left. Default: true. |
The onSuccess and onPending callbacks receive a PaymentResult object:
| Field | Type | Description |
|---|---|---|
amount | number | The charged amount in minor units. 0 for setup sessions. |
customer | string | The Pay.com customer ID. |
paymentMethod | string | Stored payment method ID (pm_card_xxx). Present when setup_future_usage was set on the session. |
status | string | Transaction status, for example succeeded for a charge, requires_capture for a hold, or pending (passed to onPending). |
transaction | string | The charge (chrg_…), hold (hld_…), or setup session (ss_…) ID. |
The onFailure callback receives a PaymentError object:
| Field | Type | Description |
|---|---|---|
message | string | A human-readable description of the failure reason. |
data | object | Additional failure details, when available. |
Returns
A checkout instance used to render the form, validate fields, and submit payments.
Example
const checkout = pay.checkout({
clientSecret: CLIENT_SECRET,
onSuccess: (payment) => {
console.log('Payment succeeded:', payment.transaction)
},
onFailure: (error) => {
console.log('Payment failed:', error.message)
},
})Was this page helpful?