Skip to Content
Docs

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 }).

Secure card fields laid out in a merchant's own form

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:

StateApplies when
baseAlways.
focusThe input has focus.
invalidThe input shows an error. That happens after the customer types in it and leaves it, or after a submit or validation.
completeThe input's value is complete.
disabledThe input is disabled.

When states overlap, invalid wins over focus, and disabled wins over both.

Secure card fields styled for a dark page

Each state takes these properties:

PropertyAccepted values
colorA hex color, rgb(), rgba(), hsl(), hsla(), or a color name.
fontFamilyFamily names, quoted or not, separated by commas.
fontSizeA length, or small, medium, large, or x-large.
fontStylenormal, italic, or oblique.
fontWeightnormal, bold, bolder, lighter, or a multiple of 100 from 100 to 900. Other numbers, such as 450, are dropped.
letterSpacingA length or normal.
lineHeightA length, a number, or normal.
paddingOne to four lengths.
textAlignleft, right, center, start, or end.
textTransformnone, 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.

EventPayloadWhen
fields_readyThe 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_expiredThe 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.

A secure card number field with an invalid-number message

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 })
OptionTypeDescription
includeWalletAvailabilitybooleanAlso 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:

FieldTypeDescription
paymentMethodTypestringThe method's identifier. Pass it to checkout.pay({ method }).
namestringThe 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.
redirecttrueThe method's configured redirect flag, the same on every device.
iconstringThe method's icon name in Pay.com's icon set.
colorstringThe method's brand color.
fieldsarrayExtra fields the customer must fill, as described in List payment methods.
supportedCardBrandsstring[]For card, the accepted brands, in lowercase.
availablebooleanFor 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' })
OptionTypeDescription
methodstringA paymentMethodType from getPaymentMethods().
dataobjectThe values for the method's fields.
billingDetailsobjectThe 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 no data pays with the mounted card fields, as checkout.submit() does.
  • Any other method runs the same flow as checkout.headless(method, data, billingDetails). What happens next depends on the method's redirectMode. With top, the page leaves for the provider and comes back to the session's return URL, where your server checks the payment's status. With new_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.

Was this page helpful?