Iframve v2 - localhost - non PCI

by TokenEx Support

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="http://localhost:31036/iframe/v2NonPCI/16f8159dafe14bcb9c1a6d19e0b1fa8c"></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="btnFocus">focus</button>

JavaScript

document.getElementById('bthValidate').onclick = validate;
document.getElementById('bthTokenize').onclick = tokenize;
document.getElementById('btnReset').onclick = reset;
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)
}


//set the focos
function focus() {
  iframe.contentWindow.postMessage('focus', '*');
}
//send the tokenize command to the iframe
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 === 'http://localhost:31036') {
    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 />";
}