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:
- The methods enabled on your account and turned on in the Pay.com dashboard.
- A Headless checkout integration, with a checkout instance created with
pay.checkout(). - A webhook endpoint that receives charge events. See Webhooks.
Accept an APM payment
An APM payment with Headless checkout takes five steps.
- 1
Create a payment session
On your server, create a payment session with
POST /v1/sessions/payment, and add each method's type topayment_method_typesnext 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
10000is €100.00.customer_reference_idis your own ID for the customer, and it's required. Return the session'sclient_secretto your page, and initialize Pay.com.js with it. - 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 usecheckout.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 tocheckout.headless(), and an optionalnameto display. Card entries also havesupportedCardBrands, and a method that asks the customer for details has afieldsarray. iDEAL's entry, trimmed to itsfields, 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
Render the payment method selector
Create a button or tab for each entry, in the array's order. Use
nameas 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,
supportedCardBrandslists the card brands to show icons for. - 4
Collect the method's fields
When the selected method has a
fieldsarray, render an input for each field before you start the payment. Use each field'slabelas the input's label, and itsidas the key when you start the payment.typeis the input to render:input,date,checkbox,radio, orselect.optionslists aselectorradiofield's choices, each with alabeland avalue.validationholds the field's rules, such asrequired,min,max, andpattern.
See Field object for every property a field can have.
- 5
Start the payment
When the customer confirms the method, call
checkout.headless()with itspaymentMethodTypeand the collected values, keyed by each field'sid.checkout.headless()rejects when the payment fails, so wrap the call intry/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
redirectistruewhen 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
onSuccessif the payment succeeded,onPendingif its result isn't final yet, oronFailureif it failed. Fulfill the order when you receive thecharge.succeededwebhook event. See Listen for webhooks. - In a window over your page: if the customer closes it, Pay.com.js calls
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
- Headless checkout: Set up Pay.com.js, the checkout instance, and its callbacks.
checkout.getPaymentMethods(): Review the response, its fields, and their validation rules.checkout.headless(): Review the method's parameters and errors.- Payment methods: Find each method's type and fields.