Iframve v2 - Prod
HTML
<p id="data"></p>
<!--update the src of this iframe to the HTPURL you obtained from the https://test-htp.tokenex.com/api/v1 call -->
<iframe id="tokenExIframe" width="30%" height="20%" scrolling="no" frameborder="0" src="https://htp.tokenex.com/iframe/v2/79775f4cdabb4efc96c97596b9f5d80f"></iframe>
<br/>
<button type="button" id="bthTokenize">Tokenize</button>
<button type="button" id="bthValidate">Validate</button>
<button type="button" id="btnReset">Reset</button>
<button type="button" id="btnPretty">Enable Pretty</button>
<button type="button" id="btnFocus">focus</button>
JavaScript
document.getElementById('bthValidate').onclick = validate;
document.getElementById('bthTokenize').onclick = tokenize;
document.getElementById('btnReset').onclick = reset;
document.getElementById('btnPretty').onclick = pretty;
document.getElementById('btnFocus').onclick = focus;
var iframe = document.getElementById("tokenExIframe");
//add event listener for postmessage
if (window.addEventListener) {
addEventListener("message", listener, false)
} else {
attachEvent("onmessage", listener)
}
//send the tokenize command to the iframe
function pretty() {
iframe.contentWindow.postMessage('enablePrettyFormat', '*');
}
function focus() {
iframe.contentWindow.postMessage('focus', '*');
}
//enable pretty formated pan
function tokenize() {
iframe.contentWindow.postMessage('tokenize', '*');
}
//send the validate command to the iframe
function validate() {
iframe.contentWindow.postMessage('validate', '*');
}
//send the reset command to the iframe
function reset() {
var iframe = document.getElementById("tokenExIframe");
iframe.contentWindow.postMessage('reset', '*');
}
//handle the message from the iframe
function listener(event) {
if (event.origin === 'https://htp.tokenex.com') {
var message = JSON.parse(event.data);
switch (message.event) {
case 'focus':
if (message.data.value) {
//field has focus
} else {
//field does not have focus(blur)
}
case 'change':
// field changed
break;
case 'validation':
if (!message.data.isValid) {
//field failed validation
} else {
//validation valid!
}
break;
case 'post':
if (!message.data.success) {
// use message.data.error
} else {
//get token! message.data.token
}
break;
}
log("iframe message:" + event.data)
} else {
log("Oops! Origin: " + event.origin + " isn't allowed")
}
}
function log(message) {
...