Secure Fields (Payment)
Below, we outline the step-by-step process to enable the Secure Fields checkout functionalities in your system:
Step 1: Include the library in your project.
The first step is to make sure to include the Yuno SDK file in your webpage before closing the <body>
tag. See the example below:
<script src="https://sdk-web.y.uno/v1/static/js/main.min.js"></script>
Step 2: Initialize secure fields with the public key
In your JavaScript application, create an instance of the Yuno
class by providing a valid PUBLIC_API_KEY. Check the Get your API credentials guide.
Like the example below, use the initialized class that is attributed to the yuno
constant.
const yuno = Yuno.initialize(PUBLIC_API_KEY)
Step 3: Start the checkout process
You will start the checkout process. To do it, use the secureFields
function and provide the necessary configuration parameters.
The essential parameters are the countryCode
, which determines the country for which the payment process is configured, and checkoutSession
, which refers to the current payment's checkout session. The next code block presents an example of the parameter configuration.
The following table lists all required parameters and their descriptions.
Parameter | Description |
---|---|
countryCode | This parameter determines the country for which the payment process is being configured. It should be set to one of the following country codes: CO, BR, CL, PE, EC, UR, or MX. |
checkoutSession | Refers to the current payment's checkout session.Example: '438413b7-4921-41e4-b8f3-28a5a0141638' |
const secureFields = yuno.secureFields({
/**
* country can be one of CO, BR, CL, PE, EC, UR, MX
*/
countryCode: country,
checkoutSession,
})
Step 4: Mount the Secure Fields
After defining the parameters, you will define, configure, and mount the Secure Fields. For each Secure Field, you need to define the name
and options
when creating it with the secureFields.create
function.
The table below presents all configurations available:
Parameters | Description |
---|---|
name | The available names for field names are cvv, pan, or expiration. |
options.placeholder | Default placeholder for the field. |
options.styles | Additional CSS styles for the current field. |
options.label | Field visible label. |
options.showError | Defines if errors will be shown. Available options are true and false . |
options.onChange | An auxiliary function that can be configured and will run when the field content changes. Indicates if the fields have errors. |
options.onBlur | An auxiliary function that can be configured and will run when blurring from the input. |
options.onFocus | An auxiliary function that can be configured and will run when focussing on the input. |
Once you have set the parameter, you will render the created Secure Field with the render
function by selecting an HTML element using a valid CSS selector (#
, .
, [data-*]
).
The next code block presents an example of the parameter configuration for three Secure Fields, and as they are mounted, the fields are presented to the user.
const secureNumber = secureFields.create({
name: 'pan',
options: {
placeholder: '0000 0000 0000 0000',
/**
* you can edit card form styles
* only you should write css then it will be injected into the iframe
* example
* `@import url('https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap');
* .Yuno-text-field__content.focus label.Yuno-text-field__label {
* color: red;
* font-family: 'Luckiest Guy' !important;
* }`
*/
styles: ``,
label: 'Card Number',
showError: true,
onChange: ({ error }) => {
if (error) {
console.log('error_pan')
} else {
console.log('not_error_pan')
}
},
onBlur() {
console.log('blur_pan')
},
onFocus: () => {
console.log('focus_pan')
}
},
})
secureNumber.render('#pan')
const secureExpiration = secureFields.create({
/**
* Fields name, can be 'cvv' | 'pan' | 'expiration'
*/
name: 'expiration',
// All options are optional
options: {
placeholder: 'MM / YY',
/**
* you can edit card form styles
* only you should write css then it will be injected into the iframe
* example
* `@import url('https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap');
* .Yuno-text-field__content.focus label.Yuno-text-field__label {
* color: red;
* font-family: 'Luckiest Guy' !important;
* }`
*/
styles: ``,
label: 'Card Expiration',
showError: true,
// Indicates if the fields has error
onChange: ({ error }) => {
if (error) {
console.log('error_expiration')
} else {
console.log('not_error_expiration')
}
},
// Trigger when blurring from input
onBlur() {
console.log('blur_expiration')
},
// Trigger when focussing on input
onFocus: () => {
console.log('focus_expiration')
}
},
})
secureExpiration.render('#expiration')
const secureCvv = secureFields.create({
name: 'cvv',
options: {
placeholder: 'CVV',
/**
* you can edit card form styles
* only you should write css then it will be injected into the iframe
* example
* `@import url('https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap');
* .Yuno-text-field__content.focus label.Yuno-text-field__label {
* color: red;
* font-family: 'Luckiest Guy' !important;
* }`
*/
styles: ``,
label: 'CVV',
showError: true,
onChange: ({ error }) => {
if (error) {
console.log('error_cvv')
} else {
console.log('not_error_cvv')
}
},
onBlur() {
console.log('blur_cvv')
},
onFocus: () => {
console.log('focus_cvv')
}
},
})
secureCvv.render('#cvv')
- Below you find a GIF showing how you can configure the Secure Fields:
Step 5: Generate an OTT (one-time token)
With all user information in hand, you can start the payment. First, you need to create a One-Time Token using the function secureFields.generateToken
. As it is an asynchronous function, you can use try/catch to ensure you will correctly handle triggered errors. Below, you will find an example of creating a One-Time Token:
// Create One Time Token
// This will trigger an error if there are missing data
// You can catch it using a try/catch
const oneTimeToken = await secureFields.generateToken({
// Required: You can create an input to get this formation
cardHolderName: 'John Deer',
// Optional: You can create an input to get this formation
saveCard: true,
// Check your card processor to know if you need to send
// customer information
// full object here https://docs.y.uno/reference/the-customer-object
customer: {
document: {
document_number: '1090209924',
document_type: 'CC',
},
},
})
You can also use secureFields.generateTokenWithInformation
to receive any additional info given by the customer in the checkout such as installments or document type/number.
// Create One Time Token
// This will trigger an error if there are missing data
// You can catch it using a try/catch
const oneTimeTokenWithInformation = await secureFields.generateTokenWithInformation({
// Required: You can create an input to get this formation
cardHolderName: 'John Deer',
// Optional: You can create an input to get this formation
saveCard: true,
// Check your card processor to know if you need to send
// customer information
// full object here https://docs.y.uno/reference/the-customer-object
customer: {
document: {
document_number: '1090209924',
document_type: 'CC',
},
},
})
Step 6: Create the Payment
After receiving the One Time Token, you can create the payment using the createPayment
function, providing the oneTimeToken
and the checkoutSession
. The back-to-back payment creation must be carried out using the Create Payment endpoint.
- Status Payment: As the process may raise errors, we recommend using a try/catch function here. Then, you can check the payment status with the
yuno.mountStatusPayment
function, as shown in the next code example:
// Create your payment, you should implement this function
await createPayment({ oneTimeToken, checkoutSession })
// Check payment status
yuno.mountStatusPayment({
checkoutSession: checkoutSession,
/**
* Country can be one of CO, BR, CL, PE, EC, UR, MX
*/
countryCode: 'CO',
/**
* Language can be one of es, en, pt
*/
language: 'en',
/**
* @param {'READY_TO_PAY' | 'CREATED' | 'SUCCEEDED' | 'REJECTED' | 'CANCELLED' | 'ERROR' | 'DECLINED' | 'PENDING' | 'EXPIRED' | 'VERIFIED' | 'REFUNDED'} data
*/
yunoPaymentResult(data) {
console.log('yunoPaymentResult', data)
},
})
Additional steps
In addition to the code examples provided, you can access the Demo App for a complete implementation of Yuno SDKs or go directly to the HTML and JavaScript Secure Fields checkout demos available on GitHub.
Updated about 1 year ago