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 thecollectContainer() method of the Payrails client.
There are 2 types of containers:
COMPOSABLE: you callmount()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
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.
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.
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 thecontainer.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, callcontainer.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 methodssetError 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: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.
Style
You can configure the style of the Secure Fields as you wish: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:
labelStyles are base and focus.
An example of a labelStyles object:
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’screateCollectElement(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 ofCOLLECT type.