Add your own fields
You can add your own HTML fields to capture additional information about the customer or the transaction, for example, address information for the address verification service (AVS). After you capture the customer’s address information, you can send it in the payment request.
Important: If you add your own HTML fields, you are responsible for handling the additional information that you capture.
To help you understand how to add your own fields, we have created a worked example that you can follow. In our example, we capture billing details from the customer and then use the billing details in the payment request.
In our example, we complete the following actions:
- Add our own HTML fields.
- Validate the data in the fields.
- Handle the response.
- Retrieve the data from the fields.
- Create a payment request.
Step 1. Add your own HTML fields
In the following code block, we created a <div>
to capture the customer’s billing details and an error message for each required field.
Step 2. Validate the data in the fields
We use the onPreSubmit function to validate the data that the customer provides in our fields. If the field is empty or if the data doesn’t match the format that we specify, the JS library doesn’t submit the form to our gateway.
To use the onPreSubmit function in our worked example, we complete the following actions:
Step 2a. Add the onPreSubmit function.
Step 2b. Define the onPreSubmit function.
Step 2a. Add the onPreSubmit function
To delay the submission of the form until we validate the data the customer provides, we add the onPreSubmit function.
In the following code block, we add the onPreSubmit function.
Step 2b. Define the onPreSubmit function
We define the onPreSubmit function to specify the format and rules for the values that the customer must enter into the fields. If the field is missing a value, we call a displayMissingFieldsError function to display the error.
In the following code block, we check if the customer provides a value for each field.
displayMissingFieldsError function
If the customer doesn’t enter a value in a field, the displayMissingFieldsError function displays an error for five seconds.
Step 3. Handle the response
If the onPreSubmit function validates the values that the customer provides the JS library sends the request to our gateway, which returns a single-use token in the response.
In the following code block, we handle the response and close the Hosted Fields session.
Step 4. Retrieve the data from the fields
After we sent the request and received the single-use token, we use the information from the form to create a payment request.
In the following code block, we retrieve the billing details that the customer provides and organize them into a billingAddress object.
Step 5. Create payment request
We add the information that we received from the customer and the single-use token to a payment request, and then send the payment request to our gateway. For more information about how to create a payment request, go to Run a card sale.
In the following code block, we create a payment request using the billing details that the customer provides and the single-use token that we receive from the gateway.