TokenEx Iframe
The TokenEx Iframe repo is an open source library used for loading and interacting with the hosted TokenEx Iframe
Table of Contents
- Quick Start
- Configuring the Iframe
- Interacting with the Iframe
- Listening and Responding to Events
- License
Quick Start
Install the iframe npm library
npm install tokenex-iframe
Import the library into your front end application
import { TokenizeIframe } from "tokenex-iframe";
Configure the iframe
TokenizeIframe.configure({
auth: {
tokenExId: "TOKENEX_ID_GOES_HERE",
origin: window.location.origin,
tokenScheme: "TOKEN_SCHEME_GOES_HERE",
hmac: "BASE64_ENCODED_HMACSHA256_GOES_HERE",
timestamp: "UTC_TIMESTAMP_OF_GENERATED_HMAC_GOES_HERE_YYYYMMDDHHMMSS",
},
env: "test",
initialSettings: {
container: "ID_OF_THE_HTML_CONTAINER_TO_APPEND_IFRAME",
},
});
Load the iframe
TokenizeIframe.load();
Configuring the Iframe
Before you can use the iframe you must set the configuration object.
TokenizeIframe.configure({
auth: {
tokenExId: "TOKENEX_ID_GOES_HERE",
origin: window.location.origin,
tokenScheme: "TOKEN_SCHEME_GOES_HERE",
hmac: "BASE64_ENCODED_HMACSHA256_GOES_HERE",
timestamp: "UTC_TIMESTAMP_OF_GENERATED_HMAC_GOES_HERE_YYYYMMDDHHMMSS",
},
env: "test",
initialSettings: {
container: "ID_OF_THE_HTML_CONTAINER_TO_APPEND_IFRAME",
},
});
auth
Authentication properties that must be set with a valid hmac
and timestamp
OR with a valid clientSecret
NOTE: It is recommended that your hmac
is generated in a secure backend environment and your clientSecret
isn't exposed to the front end.
Properties for auth
tokenExId: string;
origin: string;
tokenScheme: number | string;
Properties for hmac
authentication
For more information on generating an HMAC, see: https://docs.tokenex.com/docs/generating-the-authentication-key
hmac: string;
timestamp: string;
Properties for clientSecret
authentication
NOTE: It is recommended that you utilize an hmac that is generated in a secure backend environment and not your clientSecret so that it isn't exposed to the front end.
clientSecret: string;
env
TokenEx environment from which to load the iframe
env: "test" | "production" | "eu"
NOTE: This can be extended for local internal testing through a .env
file with values for VITE_APP_ENVIRONMENTS_LOCAL
, VITE_APP_ENVIRONMENTS_DEV
, and VITE_APP_ENVIRONMENTS_STAGING
env: "test" | "production" | "eu";
initialSettings
These are settings that are sent in the GET
request to initialize the iframe.
cardMaxLengths?: CardMaxLengths;
container: string;
customDataLabel?: string;
customCvvDataLabel?: string;
cvv?: boolean;
cvvContainer?: string;
cvvOnly?: boolean;
cvvTitle?: string;
enforceLuhnCompliance?: boolean;
expiresInSeconds?: number;
inlineIframeJavaScript?: boolean;
inputMaxLength?: number;
kountMerchantId?: string;
pci?: boolean;
returnAutoCompleteValues?: boolean;
returnKHash?: boolean;
returnWHash?: boolean;
threeDSMethodNotificationUrl?: string;
title?: string;
token?: string;
use3DS?: boolean;
useExtendedBin?: boolean;
useKount?: boolean;
additionalSettings
These are settings that are sent via postMessage
after the iframe is rendered.
allowUnknownCardTypes?: boolean;
cardType?: string;
customDataTypes?: CustomDataType[];
customRegEx?: string;
cvvInputType?: "number" | "tel" | "text";
cvvPlaceholder?: string;
debug?: boolean;
enableAriaRequired?: boolean;
enableAutoComplete?: boolean;
enablePrettyFormat?: boolean;
enableValidateOnBlur?: boolean;
enableValidateOnCvvKeyUp?: boolean;
enableValidateOnKeyUp?: boolean;
font?: string;
forterSiteId?: string;
forterUsername?: string;
inputMode?: string;
inputType?: "number" | "tel" | "text";
maskInput?: boolean;
placeholder?: string;
styles?: {
base?: string;
focus?: string;
error?: string;
placeholder?: string;
cvv?: {
base?: string;
focus?: string;
error?: string;
placeholder?: string;
}
};
Interacting with the iframe
configure(configuration)
Sets the configuration object settings for the iframe see the configuration section for more details.
TokenizeIframe.configure({
auth: {
tokenExId: "TOKENEX_ID_GOES_HERE",
origin: window.location.origin,
tokenScheme: "TOKEN_SCHEME_GOES_HERE",
hmac: "BASE64_ENCODED_HMACSHA256_GOES_HERE",
timestamp: "UTC_TIMESTAMP_OF_GENERATED_HMAC_GOES_HERE_YYYYMMDDHHMMSS",
},
env: "test",
initialSettings: {
container: "ID_OF_THE_HTML_CONTAINER_TO_APPEND_IFRAME",
},
});
validateConfig()
Validates the current configuration of the iframe and returns { isValid: boolean, details: string[] }
which can be useful for testing and troubleshooting your configuration object. This method is also ran on TokenizeIframe.load()
so the iframe won't load if the configuration is invalid.
const { isValid, details } = TokenizeIframe.validateConfig();
if (!isValid) {
console.log(details);
}
onEvent(messageEvent, callback)
Sets a listener that returns a callback whenever an event is triggered. See events section for more details.
TokenizeIframe.onEvent("load", () => {
hideLoadingSpinner();
});
load()
Loads the iframe and appends the iframe to the container element provided in the configuration
TokenizeIframe.load();
remove()
Removes the iframe from the container element provided in the configuration
TokenizeIframe.remove();
setFraudServicesRequestDetails(data)
Sets the fraud services request with the specified details
TokenizeIframe.setFraudServicesRequestDetails(data);
tokenize()
Validates and tokenizes the contents of the iFrame (if validations succeed)
TokenizeIframe.tokenize();
validate()
Invokes validation routines on the contents of the iFrame
TokenizeIframe.validate();
reset()
Restore iFrame contents to initial state
TokenizeIframe.reset();
blur()
Remove focus from the input element within the iFrame
TokenizeIframe.blur();
cvvBlur()
Remove focus from the CVV input element within the iFrame
TokenizeIframe.cvvBlur();
focus()
Set the focus of the page to the input element within the iFrame
TokenizeIframe.focus();
cvvFocus()
Set the focus of the page to the CVV input element within the iFrame
TokenizeIframe.cvvFocus();
toggleMask()
Toggles the masking of the input element within the iframe
TokenizeIframe.toggleMask();
toggleCvvMask()
Toggles the masking of the cvv input element within the iframe
TokenizeIframe.toggleCvvMask();
binLookup()
Send a Bin Lookup request to the Iframe (subscribers only)
TokenizeIframe.toggleCvvMask();
sendCommandToIframe(command, data)
(Advanced use cases) Sends a post message command to the iframe
TokenizeIframe.sendCommandToIframe(command, data);
Listening and Responding to Events
Each event object returned from the iFrame will contain properties relevant to the type of message or event. Wiring up one or more of these events to your instance of the iFrame is done on the iFrame object generated by the call to the on() function and supplying a callback function.
Event | Raise Condition(s) |
---|
load | The iFrame has loaded. |
focus | The input in the iFrame gains focus. |
blur | The input in the iFrame loses focus. |
change | The input value has changed. Raised only if the data has changed between the most recent focus and blur events. |
cardTypeChange | (PCI only) The possible card type entered by the user has changed. |
error | An error occurred during the invocation of a command. |
validate | The validate command was invoked, either directly by the user or as part of the Tokenize command (see Validate event fields below). |
tokenize | The tokenize command was invoked (see Tokenize event fields below). |
cvvFocus | The input in the cvv iFrame gains focus. |
cvvBlur | The input in the cvv iFrame loses focus. |
notice | Raised when the iFrame is loaded by providing an expiration date time stamp as yyyyMMddHHmmss in UTC (current time + 20 mins) and a second notice one minute prior to expiration." |
expired | Raised when the IFrame has expired. |
toggleMask | The toggleMask command was invoked (see ToggleMask and ToggleCvvMask event fields below) |
toggleCvvMask | The toggleCvvMask command was invoked (see ToggleMask and ToggleCvvMask event fields below) |
binLookupResponse | The Bin Lookup command has returned a response |
onEvent(event, callback)
TokenizeIframe.onEvent("load", () => { console.log("CC iFrame Loaded") });
TokenizeIframe.onEvent("focus", () => { console.log("CC iFrame focus") });
TokenizeIframe.onEvent("blur", () => { console.log("CC iFrame blur") });
TokenizeIframe.onEvent("change", () => { console.log("CC iFrame Change:") });
TokenizeIframe.onEvent("validate", (data) => { console.log("CC iFrame validate:" + JSON.stringify(data)) });
TokenizeIframe.onEvent("cardTypeChange", (data) => { console.log("CC iFrame cardTypeChange:" + JSON.stringify(data)) });
TokenizeIframe.onEvent("tokenize", (data) => { console.log("CC iFrame Tokenize:" + JSON.stringify(data)) });
TokenizeIframe.onEvent("error", (data) => { console.log("CC iFrame error:" + JSON.stringify(data)) });
TokenizeIframe.onEvent("cvvFocus", () => { console.log("CVV iFrame focus") });
TokenizeIframe.onEvent("cvvBlur", () => { console.log("CVV iFrame blur") });
TokenizeIframe.onEvent("autoCompleteValues", (data) => { console.log("CC iFrame Autocomplete Values:" + JSON.stringify(data)) });
TokenizeIframe.onEvent(“notice”, function(data) { console.log(“CC iFrame notice:” + JSON.stringify(data) });
License
MIT License
Copyright (c) TokenEx
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.