Skip to main content

Tuna.js Reference

This is the reference for the Javascript tokenization library. You will find all details, objects and configurations needed to run the library from the browser using js. You can use our Plugin Javascript to to tokenize customer information, collect sensitive card data and accept payments on your website.

Get started: Add tuna.js

To start using tuna.js is easy as including tuna.js in your checkout page. To your own safety, do not store the js file at your server; you should always load it directly from https://js.tuna.uy

HTML
1<script src="https://js.tuna.uy/tuna.js"></script>

You can choose whether to use your own style components, or ours. To use our default style: you should include the .css classes to your checkout page. Add the file that is bellow

HTML
1<script src="https://js.tuna.uy/css/components.css"></script>

Accessing the Tuna tokenization service

Use the Tuna Tokenization Service throw the Tuna.js library to create PCI Compliant credit card tokens.

HTML
1const tuna = Tuna(sessionId);
2const tokenizator = tuna.tokenizator();
tokenizator.list ()
List saved card tokens to the given user session id.
object
Token service response object.
tokensarray
The array of tokens.
codenumber
The response code.
messagestring
Response message.
tokenizator.generate ()
Create a new credit card token entry
cardDataobjectOPTIONAL
Object containing the credit card data to be tokenized.
cardHolderNamestring
The credit card holder name.
cardNumberstring
The credit card number.
expirationMonthnumber
The credit card expiration month.
expirationYearnumber
The credit card expiration year.
singleUsebool
Flag to identify if the token needs to be stored or not.
object
Token service response object containing the generated token.
tokenstring
The generated card token.
brandstring
The credit card brand.
codenumber
The response code.
messagestring
Response message.

Using the default payment form

In Tuna we think that less is more, so if you are looking for a quick and easy way to integrate with Tuna using minimum configuration then the default payment form is the best option.

This method allows you to integrate with Tuna tokenization service using the default payment fields, and it also includes the input validations configured. Look below the entire configuration of this method.

Default payment form
Tuna ()
Call this method to initiate Tuna with the basic data to perform the tokenization action.
tokenizatorSessionKeystringREQUIRED
The tokenizatorSessionKey is the public key you got using your personal data through Tuna tokenizator API.
checkoutCallbackFunctionfunctionOPTIONAL
The checkoutCallbackFunction parameter is the callback function used to return the filled data with the generated token. This function is mandatory in case of using the forgeDefaultForm method, however it is optional when you create the payment data fields by you own using the piece manager. In this case, you can send the function by the tuna checkout method or use the tuna checkout async method.
object
The instance of Tuna object.
forgeDefaultForm ()
Call this method to create the default Tuna form to acquire the user's credit card data.
placestringREQUIRED
The selector that identify the container where the form will be placed.
optionsobjectREQUIRED
The options to customize the form as well all elements used to compose then. Except the document option, none of the other pieces needs to be mandatorily configured. They will use default values when the configuration was not given.
savedCardBoxTitlestring
The title used at the top of the saved card component.
savedCardButtonTitlestring
The title used in the form button used to call the saved card component.
newCardBoxTitlestring
The title used at the top of the new credit card form.
newCardButtonTitlestring
The title used in the form button used to call the new credit card form.
documentobject
The object used to customize the document field.
buttonobject
The object used to customize the button field.
cardHolderNameobject
The object used to customize the cardHolderName field.
cardValidityobject
The object used to customize the cardValidity field.
cardCvvobject
The object used to customize the cardCvv field.
saveCardobject
The object used to customize the document field.
cardListobject
The object used to customize the saved card component.
installmentobject
The object used to customize the installment field. If not present, the installment selector won't be rendered.
getBuyerDocumentFormatter ()
This method lookup by BuyerDocumentFormatter implementations related to the locale sent.
localestringOPTIONAL
The locale key related to the payment origin country.
object
An instance of BuyerDocumentFormatter object related to the locale sent or the default instance of BuyerDocumentFormatter.

Extending the BuyerDocumentFormatter class

In order to give flexibility to the creation of document fields, the BuyerDocumentFormatter class can be extended to suport any document format and validation rules. See bellow a example of extending this class tu support Brazilian CPF (Individual Registration)

BuyerDocumentFormatter
patternstringREQUIRED
The document pattern
validationFunctionfunctionREQUIRED
Function used to validate the inputed value
documentNamestringREQUIRED
The name of the document (used as field label)
javascript
1class BrazilianDocumentFormatter extends BuyerDocumentFormatter {
2    constructor() {
3        super("999.999.99999", cpfValidationFunction, "CPF");
4    }
5}
6pieceManager.forge("#DOCUMENT", "document", {
7    title: "Document", buyerDocumentFormatter: new BrazilianDocumentFormatter()
8});

Using the Piece Manager

The Piece Manager is the object responsible to create, treat and configure the UI elements at screen. You can use it to forge interface pieces as inputs, checkboxes and else elements that you need.

tuna.pieceManager ()
This method create the PieceManager instance which allow to create UI pieces.
object
The pieceManager instance.
javascript
1let pieceManager = tuna.pieceManager();
pieceManager.forge ()
This method is responsible to create the UI elements.
placestringREQUIRED
The selector that identify the cointainer element where the piece will be placed.
pieceNamestringREQUIRED
The name that identify the type of piece that will be generated. See the piece type details.
optionsobjectOPTIONAL
The specific piece options for the choosen piece. When null, a empty options object will be used.
groupstringOPTIONALDEFAULT:"default"
The component's group name.
propertyNamestringOPTIONAL

The name of the property at the returned object.

Each piece has its own default name. Use this parameter only when the intention was override the default name or when using the following piece types: formattedInput, input.
validationFunctionfunctionOPTIONAL
Is the function used to validate if the data typed on the element is valid. The following piece types are already validated and usually no more validation are needed: creditCard, month, year, saveCard, expiration, cardHolderName, cardCvv
int
The internal id of the generated piece.
pieceManager.destroy ()
Remove the UI element from the screen
pieceIDintREQUIRED
The id of the piece returned when the piece was forged.
pieceManager.changeGroupEnableStatus ()
Change the status (enabled/disabled) of all components at the given group. Once disabled, the UI elements will not be considered to the checkout proccess and the validation functions will not be triggered.
groupstringREQUIRED
The group name
enabledboolREQUIRED
The enable/disable status to be setted at each group element
pieceManager.destroyGroup ()
Destroy all elements of the given group.
groupstringREQUIRED
The group name.
preserveRootboolREQUIRED
Flag to choose if the root element should be preserved or not when destroing the element.

Piece Types

There are 10 different types of UI elements that should be used to create the payment form. Each element accept a set of properties that can be used to configure aspects as: field label, validation message, css classes and so on. You can see all pieces that Tuna implemented and their configurations bellow:

input
A simple input text.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
formattedInput
A customizable formatted input text.
patternstringREQUIRED
The pattern used to format the input: use "A" for letters and "9" for numbers. E.g. 999.999.999-99 for brazilian CPF (citizen identification number).
cleanMaskbool
A flag to set up if the inputted value will have the mask cleaned or not when returned.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
document
A customizable input text to receive and validate the buyer document data. The inputed value will be returned in the document property.
buyerDocumentFormatterobjectREQUIRED
An instance of the BuyerDocumentFormatter class
cleanMaskbool
A flag to set up if the inputted value will have the mask cleaned or not when returned.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
button
A simple button that can be used to trigger the checkout process.
titlestring
The title of the button.
buttonClassstring
The CSS class used in the button.
iconClassstring
The CSS class used in the button icon.
showIconbool
A flag to set if the button icon will be showed or not.
onclickfunction
A function that will be called when the button was clicked.
cardHolderName
An input text already setted to keep the card holder name data. The inputed value will be returned in the cardHolderName property.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
cardNumber
A formatted input text already setted to keep and treat the credit card number. The inputed value will be returned in the cardNumber property.
showCardBrandbool
A flag to set or not the display of the credit card brand icon.
cardBrandClassstring
The css class name to use in the card brand icon.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
cardCvv
A formatted input text already setted to keep and treat the credit card cvv. The inputed value will be returned in the cvv property.
showCVVImagebool
A flag to set or not the display of the cvv icon.
cvvImageClassstring
The css class name to use in the cvv icon.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
Flag that identify that the field is allowed to show validation messages.
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
cardValidity
A formatted input text already setted to keep and treat the credit card expiration containing the expiration month and year. The inputed value will be returned in the expirationMonth and expirationYear properties.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
placeholderstring
The input placeholder.
validationMessageClassstring
The CSS class used in the validation message element.
allowValidationMessagesboolDEFAULT: true
This flag identify if the field is allowed to show validation messages
validationMessagestringDEFAULT: "Please use a valid value"
The message that will be displayed when the field value is not valid.
month
A formatted select box already setted to keep the expiration month. The selected value will be returned in the expirationMonth property.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
year
A formatted select box already setted to keep the validity year. The selected value will be returned in the expirationYear property.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
installment
A formatted select box to render the installment options. The selected value will be returned in the selectedInstallmentKey property.
optionsarrayREQUIRED
An array of { key, value } object containing the allowed installment options.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
inputClassstring
The CSS class used in the input.
saveCard
A checkbox that allows the user to save the card data for future transactions.
titlestring
The title of the field.
titleClassstring
The CSS class used in the field title.
checkedboolDEFAULT: true
The default state of the checkbox.
inputClassstring
The CSS class used in the input.

Adding saved card selector

We strongly recomend the saveCard object to increase the conversion rate of your checkout page. By implementing it, you also need to include the Saved Card Selector, so your users can choose the card that have already been saved directly in your checkout page.

async useSavedCardSelector ()
Create the saved card selector component.
placestringREQUIRED
The selector that identify the cointainer element where the piece will be placed.
optionsobjectOPTIONAL
The options to customize the component
onSelectionBeginsfunction
A function that runs when the saved card selection begins
cardCvvobject
The options to customize the cvv input field inside the saved card selector component
groupstringOPTIONAL
The component's group name
int
The number of returned saved cards. If returns 0, then the component will not be rendered.
clearSavedCardSelector ()
Clean all data setted in any saved card selector component. Including the cvv and ID card inputs.

Managing checkout

Tuna has two ways to proceed the checkout.

checkout ()
This method triggers the field validations, and then compile the inputted data in order to generate the payment credit card token. This callback function could also be setted in the Tuna method, as described here
callbackFunctionfunctionOPTIONAL
The function the will be called after the field validation proccess returnig the tokenizator promisse and the inputed credit card data
tokenizatorPromissefunction
Promise to the tokenizator request response.
creditCardDataobject
The imputed data necessary to proceed with the finalization of the payment.
javascript
1const response = await tuna.checkout();
async checkoutAsync ()
Similar to checkout but awaitable.
object
The object containing the tokenizator response and the typed card data necessary to proceed with finalization of the payment.
cardDataobject
The imputed data necessary to proceed with the finalization of the payment.
tokenDataobject
The response object returned bay the tokenization proccess.
successbool
A success flag.
javascript
1await tuna.checkoutAsync();