Skip to Content
Docs

Accept APMs with Headless checkout

List the alternative payment methods available to a payment session, collect their fields, and start each payment with Pay.com.js.

With Headless checkout, you build your own payment method selector, and Pay.com.js starts the payment when the customer selects a method. checkout.getPaymentMethods() returns the methods available to the session, and checkout.headless() starts the payment.

Requirements

Before you start, make sure you have:

Accept an APM payment

An APM payment with Headless checkout takes five steps.

  1. 1

    Create a payment session

    On your server, create a payment session with POST /v1/sessions/payment, and add each method's type to payment_method_types next to "card".

    {
      "amount": 10000,
      "currency": "eur",
      "customer_reference_id": "cust-001",
      "payment_method_types": ["card", "ideal", "skrill"]
    }

    Amounts are in the currency's minor unit, so 10000 is €100.00. customer_reference_id is your own ID for the customer, and it's required. Return the session's client_secret to your page, and initialize Pay.com.js with it.

  2. 2

    Fetch the available payment methods

    The methods available for a payment depend on your account configuration, the session's currency, and the customer's region. Call checkout.getPaymentMethods() after the checkout session is initialized, instead of hard-coding the list. Don't use checkout.canMakePayments() for this, because it reports only Apple Pay and Google Pay.

    const paymentMethods = await checkout.getPaymentMethods()

    The array is in the order of your payment methods configuration. Each entry has a paymentMethodType, which you pass to checkout.headless(), and an optional name to display. Card entries also have supportedCardBrands, and a method that asks the customer for details has a fields array. iDEAL's entry, trimmed to its fields, asks for the customer's bank:

    {
      "paymentMethodType": "ideal",
      "fields": [
        {
          "id": "bic",
          "label": "iDEAL Bank",
          "type": "select",
          "options": [
            { "label": "Rabobank", "value": "RABONL2U" },
            { "label": "ABN Amro", "value": "ABNANL2A" }
          ]
        }
      ]
    }
  3. 3

    Render the payment method selector

    Create a button or tab for each entry, in the array's order. Use name as its label, and keep the entry to start the payment.

    paymentMethods.forEach((method) => {
      const button = document.createElement('button')
      button.textContent = method.name ?? method.paymentMethodType
      button.addEventListener('click', () => {
        selectedMethod = method
      })
      container.appendChild(button)
    })

    For card entries, supportedCardBrands lists the card brands to show icons for.

  4. 4

    Collect the method's fields

    When the selected method has a fields array, render an input for each field before you start the payment. Use each field's label as the input's label, and its id as the key when you start the payment.

    • type is the input to render: input, date, checkbox, radio, or select.
    • options lists a select or radio field's choices, each with a label and a value.
    • validation holds the field's rules, such as required, min, max, and pattern.

    See Field object for every property a field can have.

  5. 5

    Start the payment

    When the customer confirms the method, call checkout.headless() with its paymentMethodType and the collected values, keyed by each field's id. checkout.headless() rejects when the payment fails, so wrap the call in try/catch.

    try {
      await checkout.headless('ideal', { bic: 'RABONL2U' })
    } catch (error) {
      // onFailure receives the error details too.
    }

    For a method without fields, pass only its type, such as checkout.headless('skrill').

    Pay.com.js opens the method in a window over your page or in a new tab, depending on the method. An entry's redirect is true when the method opens in a new tab on every device.

    • In a window over your page: if the customer closes it, Pay.com.js calls onFailure.
    • In a new tab: your page asks the customer to finish there. If they close the tab, Pay.com.js keeps waiting for the payment's outcome.

    When the customer finishes, Pay.com.js calls onSuccess if the payment succeeded, onPending if its result isn't final yet, or onFailure if it failed. Fulfill the order when you receive the charge.succeeded webhook event. See Listen for webhooks.

Test in the sandbox

Test with your sandbox API key (sk-pay-com-sandbox-...) and a payment session created against https://api.sandbox.pay.com/v1. Sandbox payments never move real funds. See Environments.

In the sandbox, checkout.getPaymentMethods() returns an entry for each method in payment_method_types that's available to the session. Some methods open a payment simulator in place of the method's page, where you choose the payment's outcome. Each method's Add to checkout page describes how to test it.

Next steps

Was this page helpful?