Skip to Content
Docs

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.

A Headless checkout form styled by the merchant, with card number, cardholder name, expiration date, and CVV inputs

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. 1

    Create a payment session

    Your server must create a payment session before the client can start a checkout. Call POST /v1/sessions/payment to get a client_secret and session id. amount is in minor units (the smallest unit of the currency), so 2000 in 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_types controls 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_secret and session id:

    {
      "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. 2

    Create a checkout instance

    Pass the client_secret to pay.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)
      },
    })
    ParameterTypeRequiredDescription
    clientSecretstringYesThe client_secret from the payment session.
    onSuccess(payment) => voidNoFired when the payment completes successfully.
    onFailure(error) => voidNoFired when the payment fails.
    onPending(payment) => voidNoFired instead of onSuccess when the payment is accepted but not final yet (for example, some alternative payment methods).

    onSuccess, onFailure, and onPending are browser-only callbacks. Never rely on them for order fulfillment. Confirm payments on your server with webhooks.

  3. 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)
    }
    FieldTypeRequiredDescription
    numberstringYesCard number, no spaces or dashes.
    cvvstringYesCard security code. Not required in payout sessions.
    expirationMonthstringYesTwo-digit month, for example '12'.
    expirationYearstringYesTwo-digit year, for example '28'.
    namestringNoCardholder name as it appears on the card.

    checkout.headless() resolves with the payment result and also calls onSuccess (or onPending for a pending result). If the payment fails, it calls onFailure and rejects, so always wrap the call in try/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/catch too. 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. 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. 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
      }
    }

    onSuccess fires when the payment completes. onFailure fires if the customer cancels or the payment is declined. Both are the callbacks you registered with pay.checkout() when you created the checkout instance. For their arguments, see pay.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:

Was this page helpful?