Secure card fields
Place each card input in its own Pay.com iframe, list payment methods for your own UI, and pay with any listed method.
Secure fields let you lay out the card inputs yourself while each input stays a Pay.com iframe. The card number, expiration date, CVV, and name are typed into Pay.com frames, so the card never exists on your page. You style the inputs through an allowlist, and your page receives field events that never carry a value.
To build the rest of your own checkout UI, checkout.getPaymentMethods() returns more about each method, and checkout.pay({ method }) pays with any method it lists.
Mount the fields
Set up the checkout as usual (see Install and initialize), then call checkout.fields() with mode: 'secure':
<label>Card number <div id="card-number"></div></label>
<label>Expiry <div id="card-expiry"></div></label>
<label>CVC <div id="card-cvv"></div></label>
<input id="cardholder" placeholder="Name on card" />
<button id="pay">Pay</button>const checkout = pay.checkout({ clientSecret, onSuccess, onFailure })
const fields = checkout.fields({
mode: 'secure',
inputs: {
number: { container: '#card-number' },
expiry: { container: '#card-expiry' },
cvv: { container: '#card-cvv', placeholder: 'CVC' },
},
inheritStyleFrom: '#cardholder',
style: {
base: { fontSize: 16, padding: '10px 12px' },
invalid: { color: '#c62828' },
},
})
document.querySelector<HTMLButtonElement>('#pay')!.addEventListener('click', () => {
checkout.submit({ cardholderName: document.querySelector<HTMLInputElement>('#cardholder')!.value })
})inputs.number and inputs.expiry are required. Leave out name to collect the cardholder name yourself and pass it to checkout.submit({ cardholderName }).
Only the inputs are Pay.com frames. The labels, borders, focus ring, error text, and Pay button belong to your page.
To check the fields without paying, call checkout.validate(). It resolves with { valid, invalidFields }.
Each input renders as an iframe that fills its container's width. It starts 40px high, then sizes itself to its content. The frame sets the input's padding to 0, so without a padding or lineHeight style a field is one line of text high (about 19px). Calling fields() again replaces the fields already mounted.
For every option, and the handle fields() returns to restyle, focus, clear, or disable the fields, see checkout.fields().
Style the fields
Secure fields accept only allowlisted styles, so no CSS of your choosing runs inside the frames. Anything outside the allowlist, and any value that doesn't match its property's format, is dropped. The SDK logs one console warning naming what it dropped (for example style.base.background), never the value.
SecureFieldStyle has one set of rules per state:
| State | Applies when |
|---|---|
base | Always. |
focus | The input has focus. |
invalid | The input shows an error. That happens after the customer types in it and leaves it, or after a submit or validation. |
complete | The input's value is complete. |
disabled | The input is disabled. |
When states overlap, invalid wins over focus, and disabled wins over both.
Each state takes these properties:
| Property | Accepted values |
|---|---|
color | A hex color, rgb(), rgba(), hsl(), hsla(), or a color name. |
fontFamily | Family names, quoted or not, separated by commas. |
fontSize | A length, or small, medium, large, or x-large. |
fontStyle | normal, italic, or oblique. |
fontWeight | normal, bold, bolder, lighter, or a multiple of 100 from 100 to 900. Other numbers, such as 450, are dropped. |
letterSpacing | A length or normal. |
lineHeight | A length, a number, or normal. |
padding | One to four lengths. |
textAlign | left, right, center, start, or end. |
textTransform | none, uppercase, lowercase, or capitalize. |
'::placeholder' | { color } only. |
A length is a number with px, em, rem, %, or pt. A JavaScript number means pixels, except for fontWeight and lineHeight, where it stays unitless. Values are limited to 100 characters (200 for fontFamily).
Styles layer from lowest to highest: inheritStyleFrom, then style, then each input's own style.
Listen for events
Listen with checkout.on(). Field events carry the field's name and state, never what was typed.
| Event | Payload | When |
|---|---|---|
fields_ready | The fields are ready for input. | |
field_change | { field, empty, valid, error } | A field's state changes. error is the message the field shows, or an empty string. |
field_focus | { field } | A field gains focus. |
field_blur | { field, valid } | A field loses focus. |
form_validation_update | { validationResult } | The form as a whole becomes valid or invalid. |
session_expired | The session's client_secret expired, so the fields can't take a payment anymore. It's emitted once, and never after a payment succeeded or went pending. |
After a decline, the inputs stay editable so the customer can try again, even with toggles.displayFailureMessages on. When the session ends, because it expired or has no attempts left, the inputs lock and the fields show no message. Show your own.
Your page draws the message under the field from the field_change event's error.
checkout.on(checkout.EVENT_TYPES.FORM_VALIDATION_UPDATED, (...params) => {
const { validationResult } = params[0] as { validationResult: boolean }
document.querySelector<HTMLButtonElement>('#pay')!.disabled = !validationResult
})If the frames haven't loaded after 30 seconds, the SDK logs a console warning. Check that nothing on your page blocks frames from js.pay.com.
List payment methods
checkout.getPaymentMethods() returns more about each method, so you can draw your own list:
const methods = await checkout.getPaymentMethods({ includeWalletAvailability: true })| Option | Type | Description |
|---|---|---|
includeWalletAvailability | boolean | Also check whether Apple Pay and Google Pay can pay in this browser, as checkout.canMakePayments() does. Off by default, because it loads the wallets' scripts. |
The methods come in the order set in your dashboard. Each has:
| Field | Type | Description |
|---|---|---|
paymentMethodType | string | The method's identifier. Pass it to checkout.pay({ method }). |
name | string | The display name. |
redirectMode | 'none' | 'new_tab' | 'top' | Where paying takes the customer on this device. none: nowhere. The method completes on this page. new_tab: the provider opens in a new tab while this page waits. top: this page goes to the provider and comes back to the session's return URL. |
redirect | true | The method's configured redirect flag, the same on every device. |
icon | string | The method's icon name in Pay.com's icon set. |
color | string | The method's brand color. |
fields | array | Extra fields the customer must fill, as described in List payment methods. |
supportedCardBrands | string[] | For card, the accepted brands, in lowercase. |
available | boolean | For applepay and googlepay, when you set includeWalletAvailability, whether this browser can pay with it now. |
Pay with a listed method
checkout.pay() also takes a method from the list:
await checkout.pay({ method: 'paypal' })| Option | Type | Description |
|---|---|---|
method | string | A paymentMethodType from getPaymentMethods(). |
data | object | The values for the method's fields. |
billingDetails | object | The customer's billing details, as checkout.headless() takes them. |
redirect | 'top' | Send the whole page to the provider instead of the method's configured redirectMode. |
method: 'card'with nodatapays with the mounted card fields, ascheckout.submit()does.- Any other method runs the same flow as
checkout.headless(method, data, billingDetails). What happens next depends on the method'sredirectMode. Withtop, the page leaves for the provider and comes back to the session's return URL, where your server checks the payment's status. Withnew_tab, the provider opens in a new tab while your page stays, and the promise settles there. A method without a redirect opens in a Pay.com modal.
checkout.pay({ source, cvv }) still pays with a saved payment method, as described in Pay with a saved payment method.