Skip to main content

Get started

Below is a four-step process to start using Secure Fields in your client.

Create a container

First, create a container for the form fields using the collectContainer() method of the Payrails client. There are 2 types of containers:
  • COMPOSABLE: you call mount() directly on the container, you must define the form’s layout, and the number of elements must match the sum of the numbers in the layout.
  • COLLECT: layout is not necessary. Secure fields are mounted separately.

Create a collectible field

A Payrails CollectField is defined as shown below:

Mount fields to the DOM

Collect container

To specify where the fields will be rendered on your page, create a placeholder <div> fields with unique id tags. For instance, the form below has 4 empty divs with unique IDs as placeholders for four Payrails fields.
Now, when the mount(domField) method of the Field is called, the Field will be inserted in the specified div. For instance, the call below will insert the Field into the div with the id #cardNumber.
You can use the unmount method to reset any collect field to its initial state.

Composable container

A composable container works similarly to a collect container but is mounted as a whole.
HTML

Extract data from secure fields

Collect raw data

When the form is ready to be submitted, call the container.collect() method on the container object. This will collect the data from all the fields belonging to the container and save it to the vault.

Tokenize the card and create a payment instrument

Alternatively, you can create a payment instrument. Note: you don’t need to use this method in most cases as it might also break for some flows (e.g. when no holder reference is passed during client init). Instead, call container.collect() and use raw card data to authorize the payment. Call the container.tokenize({storeInstrument: true}) method on the container object. This will collect the data from all the fields belonging to the container and save it to the vault. It will also create a payment instrument and return the payment instrument details.

Errors and validation

Payrails Web provides two types of validation for collecting fields.

Default validations

Every Collect Field has a set of default validations listed below:

UI errors

Helps to display custom error messages on the Payrails Fields through the methods setError and resetError on the fields.
setError(error: string) method is used to set the error text for the field. When this method is triggered, all the current errors in the field will be overridden with the custom error message passed. This error will be displayed on the field until resetError() is triggered on the same field. resetError() method is used to clear the custom error message that is set using setError.

Event Listener

Helps to communicate with Payrails fields by listening to an event:
There are 4 events in EventName: The handler function(state) => void is a callback function you provide that will be called when the event is fired with the state object, as shown below.
Here’s a sample code snippet for using listeners:

Style

You can configure the style of the Secure Fields as you wish:
The inputStyles field accepts a style object that consists of CSS properties that should be applied to the form field in the following states: Styles are specified with JSS. An example of a inputStyles object:
The states that are available for labelStyles are base and focus. An example of a labelStyles object:
The state that is available for errorTextStyles is only the base state, it shows up when there is some error in the collect field. An example of an errorTextStyles object:

Translations

For the Secure Fields, translations are part of the container’s createCollectElement(collectField, options) signature, inside the collectField object:

End-to-end example

Here’s an end-to-end example of card collection with a secure field for a container of COLLECT type.
Last modified on September 23, 2026