Iframve v1
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/dfc904128d1d431681c32eb9e42c556c"></iframe>
<br/>
<button type="button" id="bthTokenize">Tokenize</button>
<button type="button" id="bthValidate">Validate</button>
<button type="button" id="btnReset">Reset</button>
JavaScript
document.getElementById('bthValidate').onclick = validate;
document.getElementById('bthTokenize').onclick = tokenize;
document.getElementById('btnReset').onclick = reset;
//add event listener for postmessage
if (window.addEventListener) {
addEventListener("message", listener, false)
} else {
attachEvent("onmessage", listener)
}
//send the tokenize command to the iframe
function tokenize() {
var iframe = document.getElementById("tokenExIframe");
iframe.contentWindow.postMessage('tokenize', 'https://htp.tokenex.com');
}
//send the validate command to the iframe
function validate() {
var iframe = document.getElementById("tokenExIframe");
iframe.contentWindow.postMessage('validate', 'https://htp.tokenex.com');
}
//send the reset command to the iframe
function reset() {
var iframe = document.getElementById("tokenExIframe");
iframe.contentWindow.postMessage('reset', 'https://htp.tokenex.com');
}
//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) {
document.getElementById("data").innerHTML += '<em>'+ message+ '</em>' + "<br /><br />";
}