Skip to Content
Docs

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.

Universal checkout with card fields, a save-card checkbox, and a Pay button

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. 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 when the payment is accepted but not final yet (for example, some alternative payment methods).

    For all options, see pay.checkout().

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

  3. 3

    Render the checkout form

    Call checkout.universal() to render the hosted form inside a container element.

    checkout.universal({
      container: '#pay-com-checkout'
    })
    ParameterTypeRequiredDescription
    containerstringYesCSS selector for the DOM element where the form renders.
    alternatives'button' | 'radio'NoLayout style for alternative payment methods. Some methods (for example, iDEAL) only appear with 'radio'. Default: 'button'.
    toggles.submitButtonbooleanNoSet to false to hide the built-in submit button and use your own. Default: true.
    styleobjectNoStyle 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:

Was this page helpful?