Headless checkout
Collect payment details in your own checkout UI, including Apple Pay and Google Pay buttons, and submit them with Pay.com.js.
Headless checkout gives you full control over your checkout UI. You build your own payment form, collect card details or trigger wallet payments, and submit them through Pay.com.js. No prebuilt form is rendered.
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 submit the payment from your own 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 instead of onSuccesswhen the payment is accepted but not final yet (for example, some alternative payment methods). - 3
Submit a payment
Call
checkout.headless()with the payment method type and the data collected from your form.Card payment
Pass the card details your form collected:
try { const payment = await checkout.headless('card', { number: '4111111111111111', cvv: '123', expirationMonth: '12', expirationYear: '28', name: 'Jane Smith', }) } catch (error) { console.log('Payment failed:', error.message) }Field Type Required Description numberstringYes Card number, no spaces or dashes. cvvstringYes Card security code. Not required in payout sessions. expirationMonthstringYes Two-digit month, for example '12'.expirationYearstringYes Two-digit year, for example '28'.namestringNo Cardholder name as it appears on the card. checkout.headless()resolves with the payment result and also callsonSuccess(oronPendingfor a pending result). If the payment fails, it callsonFailureand rejects, so always wrap the call intry/catch.Other payment methods
Headless checkout also supports wallet and bank redirect payments:
// Apple Pay await checkout.headless('applepay') // Google Pay await checkout.headless('googlepay') // iDEAL (requires BIC code) await checkout.headless('ideal', { bic: 'SNSBNL2A' }) // GiroPay await checkout.headless('giropay')These calls reject on failure in the same way, so wrap them in
try/catchtoo. Before you show Apple Pay or Google Pay buttons, check that the customer's browser can pay with them. See Apple Pay and Google Pay.
Apple Pay and Google Pay
With Universal checkout, Apple Pay and Google Pay are included automatically. With Headless checkout, you check whether each wallet is available in the customer's browser, then call checkout.headless() with the wallet type when the customer clicks its button.
Apple Pay and Google Pay must be activated on your Pay.com merchant account. To confirm that they're on, check your payment methods configuration in the Pay.com dashboard, as Turn on payment methods describes.
- 1
Check wallet availability
Before rendering wallet buttons in your UI, check whether Apple Pay or Google Pay are available in the customer's browser using
checkout.canMakePayments(). It returns a boolean for each wallet, so you can display only the payment options the customer's device supports.const canMakePayments = await checkout.canMakePayments() // { applepay: true, googlepay: false }Only render a wallet button if the corresponding value is
true. - 2
Trigger the payment
When the customer clicks a wallet button, call
checkout.headless()with the wallet type. Pay.com.js handles the payment sheet, authorization, and outcome.async function handleApplePay() { try { await checkout.headless('applepay') } catch (error) { // onFailure has already been called with the same error } } async function handleGooglePay() { try { await checkout.headless('googlepay') } catch (error) { // onFailure has already been called with the same error } }onSuccessfires when the payment completes.onFailurefires if the customer cancels or the payment is declined. Both are the callbacks you registered withpay.checkout()when you created the checkout instance. For their arguments, seepay.checkout().
Listen for webhooks
Wallet payments send the same webhook events as card payments. Subscribe to these payment session events to confirm the outcome on your backend, independently of the browser:
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 submits a card payment from your own form: with the npm package, or in a React component.
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. Submit a card payment
try {
const payment = await checkout.headless('card', {
number: '4111111111111111',
cvv: '123',
expirationMonth: '12',
expirationYear: '28',
name: 'Jane Smith',
})
} catch (error) {
// onFailure has already been called with the same error
}Next steps
Related checkout guides:
- Display available payment methods: Build a dynamic payment method selector using
getPaymentMethods(). - Pay with a saved card: Let returning customers pay with
getExistingSources()andcheckout.pay(). - Pay.com.js reference: The SDK's methods, including
checkout.headless()andcheckout.canMakePayments().