Universal checkout
Render a prebuilt checkout form with card fields, wallet buttons, and a submit button inside your page.
Universal checkout renders a hosted checkout form inside a container on your page. It handles card fields, wallet buttons (Apple Pay and Google Pay), saved payment methods, and submission, with minimal code on your side.
Requirements
Before you begin, install and initialize Pay.com.js.
Build the checkout
Create a payment session on your server, then pass its client_secret to Pay.com.js and render the form.
- 1
Create a payment session
Your server must create a payment session before the client can start a checkout. Call
POST /v1/sessions/paymentto get aclient_secretand sessionid.amountis in minor units (the smallest unit of the currency), so2000in USD charges $20.00.const response = await fetch("https://api.sandbox.pay.com/v1/sessions/payment", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Basic " + btoa(":sk-pay-com-sandbox-…") }, body: JSON.stringify({ currency: "usd", amount: 2000, customer_reference_id: "927071e4-464c-4f5d-a079-4aa097f922ca", payment_method_types: ["card"] }) }); const session = await response.json();payment_method_typescontrols which payment methods appear at checkout. Card, Apple Pay, and Google Pay are included when you pass"card". Alternative payment methods such as Neosurf and paysafecard must be added explicitly, for example["card", "neosurf"]. Each payment method page lists the value to use. Only methods enabled on your account can be included (see Turn on payment methods).A successful response returns a short-lived
client_secretand sessionid:{ "id": "ps_217940156986951680", "resource": "payment_session", "amount": 2000, "currency": "usd", "client_secret": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "status": "open" }For all available parameters, see the payment sessions API reference.
- 2
Create a checkout instance
Pass the
client_secrettopay.checkout()along with callbacks to handle the payment result.const checkout = pay.checkout({ clientSecret: session.client_secret, onSuccess: (payment) => { console.log('Payment succeeded:', payment.transaction) }, onFailure: (error) => { console.log('Payment failed:', error.message) }, })Parameter Type Required Description clientSecretstringYes The client_secretfrom the payment session.onSuccess(payment) => voidNo Fired when the payment completes successfully. onFailure(error) => voidNo Fired when the payment fails. onPending(payment) => voidNo Fired when the payment is accepted but not final yet (for example, some alternative payment methods). For all options, see
pay.checkout(). - 3
Render the checkout form
Call
checkout.universal()to render the hosted form inside a container element.checkout.universal({ container: '#pay-com-checkout' })Parameter Type Required Description containerstringYes CSS selector for the DOM element where the form renders. alternatives'button' | 'radio'No Layout style for alternative payment methods. Some methods (for example, iDEAL) only appear with 'radio'. Default:'button'.toggles.submitButtonbooleanNo Set to falseto hide the built-in submit button and use your own. Default:true.styleobjectNo Style overrides for the form. See Customize the checkout and the style object reference. For all options, see
checkout.universal().Make sure the container element exists on the page:
<div id="pay-com-checkout"></div>
Listen for webhooks
A customer closing the browser or a network error can stop the callbacks from firing. Confirm the outcome on your backend with these payment session events:
payment_session.completed: The session was paid, and a charge or hold was created. Fulfill the order.payment_session.failed: The session ended without a successful charge. It can include several declined payment attempts.
Verify the Pay-Signature header on every event before you act on it. See Verify webhook signatures and Payment session events.
Complete example
Each tab renders the same checkout: with the npm package, in a React component, or with the script tag.
import Pay from '@pay-com/js'
// 1. Initialize the SDK with your merchant ID (not the session ID)
const pay = await Pay.com({
identifier: 'YOUR_MERCHANT_ID'
})
// 2. Create a checkout instance
const checkout = pay.checkout({
clientSecret: session.client_secret,
onSuccess: (payment) => {
console.log('Payment succeeded:', payment.transaction)
},
onFailure: (error) => {
console.log('Payment failed:', error.message)
},
})
// 3. Render the checkout form
checkout.universal({
container: '#pay-com-checkout'
})Next steps
Related checkout guides:
- Customize the checkout: Apply your brand's colors, typography, and spacing to the checkout form.
- Pay.com.js reference:
checkout.validate(),checkout.submit(), and the SDK's other methods.