1. Create a collect container
Create a container for the secure fields with thecollectContainer() method of the Payrails client:
- mount each element individually wherever you want on the page (
element.mount(selector)) — use this when you control the markup around every field, or - mount the container once with
container.mount(selector)to render all created elements into a single wrapper.
containerId and fonts — see How to customize the checkout’s appearance for font loading. Styling happens per element via
appearance (step 2).
2. Create collect elements
Create one element per field withcreateCollectElement(options):
type field takes a Payrails ElementType. Each type applies the
appropriate formatting and validations to the field:
CARD_NUMBERCARDHOLDER_NAMECVVEXPIRATION_MONTHEXPIRATION_YEAREXPIRATION_DATE
format values accepted for EXPIRATION_DATE are:
MM/YY(default)MM/YYYYYY/MMYYYY/MM
format values accepted for EXPIRATION_YEAR are:
YY(default)YYYY
format is not specified, or an invalid value is passed, the default is used.
For the appearance rules syntax, class names, and state modifiers (.payrails-input--invalid, …) see How to customize the checkout’s appearance.
3. Mount the elements to the DOM
Create placeholder<div> elements with unique id attributes where the fields should render:
unmount() to remove an element from the DOM:
4. React to element events (optional)
Every element exposes the typedon(eventName, handler) method shared by all SDK elements; secure fields emit 'change', 'focus', 'blur', and 'ready' . on() returns an unsubscribe function:
5. Validate and collect the data
When the form is ready to be submitted, you can first validate all fields:collect() on the container. All created elements must be mounted. The card data is encrypted inside the secure iframes and returned as an opaque payload - the raw values are never exposed to your code:
collect() rejects with a validation error when a field is incomplete or invalid.
6. Save the card as a payment instrument (optional)
To store the collected card for later payments, calltokenize() instead of collect(). It collects and encrypts the data, then saves it to Payrails as a payment instrument for the customer (holder) the SDK session was initialized for:
id, status, and card metadata such as data.bin, data.network, and data.suffix.
End-to-end example
Default validations
Every element type has a set of built-in validations:CARD_NUMBER: card number validation with checksum (Luhn algorithm) and card-scheme-aware length and formatting. A valid 16 digit card number is formatted asXXXX XXXX XXXX XXXX.CARDHOLDER_NAME: name should be 2 or more symbols; valid characters match the pattern^([a-zA-Z\\ \\,\\.\\-\\']{2,})$.CVV: 3–4 digits.EXPIRATION_DATE: any date starting from the current month, in the configuredformat(defaultMM/YY).EXPIRATION_MONTH: a valid month (01–12).EXPIRATION_YEAR: the current year or later, in the configuredformat(defaultYY).
UI errors for collect elements
You can display custom error messages on the elements withsetError and resetError.
setError(error: string) overrides any current error on the element with the custom message. The message stays visible until resetError() is called on the same element.
resetError() clears the custom error message set by setError.