cloud-sdk-js
Version:
X-PAYMENTS CLOUD API
2 lines (1 loc) • 3 kB
JavaScript
!function(){"use strict";function e(e,t,n){this.jsApiVersion="2.0",this.serverDomain="xpayments.com",this.messageNamespace="xpayments.connect.",this.previousHeight=-1,this.config={debug:!1,account:"",container:"",topElement:"",referrerUrl:document.location.href,applePayOnly:!1,quickAccessKey:""},this.handlers={},this.bindedListener=!1}e.prototype.init=function(e){for(var t in e)void 0!==this.config[t]&&(this.config[t]=e[t]);return this.on("alert",(function(e){window.alert(e.message)})).on("config",(function(){console.error("X-Payments Widget is not configured properly!")})),this.bindedListener=this.messageListener.bind(this),window.addEventListener("message",this.bindedListener),this},e.prototype.getContainerElm=function(){return this.safeQuerySelector(this.config.container)},e.prototype.getIframeId=function(){return"xpayments-connect"},e.prototype.getIframeElm=function(){return document.getElementById(this.getIframeId())},e.prototype.safeQuerySelector=function(e){var t=!1;return e&&(t=document.querySelector(e)),t},e.prototype.resize=function(e){var t=this.getIframeElm();t&&(this.previousHeight=t.style.height,t.style.height=e+"px")},e.prototype.load=function(){var e=this.getContainerElm();if(!e)return this;var t=document.createElement("iframe");t.id=this.getIframeId(),t.style.width="100%",t.style.height="0",t.style.overflow="hidden",t.setAttribute("scrolling","no"),e.appendChild(t),t.src=this.getRedirectUrl()},e.prototype.getRedirectUrl=function(){return"https://"+this.getServerHost()+"/?ref="+encodeURIComponent(this.config.referrerUrl)+"&account="+encodeURIComponent(this.config.account)+"&api_version="+encodeURIComponent(this.jsApiVersion)+"&quickaccess="+encodeURIComponent(this.config.quickAccessKey)},e.prototype.on=function(e,t){return this.handlers[e]=t.bind(this),this},e.prototype.trigger=function(e,t){return"function"==typeof this.handlers[e]&&this.handlers[e](t),this},e.prototype.getServerHost=function(){return"connect."+this.serverDomain},e.prototype.messageListener=function(e){if(window.JSON){var t=!1;try{t=window.JSON.parse(e.data)}catch(e){}if(t&&t.event&&0===t.event.indexOf(this.messageNamespace)){this.log("X-Payments Event: "+t.event+"\n"+window.JSON.stringify(t.params));var n=t.event.substr(this.messageNamespace.length);if("loaded"===n){if(-1!==this.previousHeight){var i=this.config.topElement?this.safeQuerySelector(this.config.topElement):this.getContainerElm();i&&i.scrollIntoView(!0)}this.resize(t.params.height)}else"resize"===n?this.resize(t.params.height):"alert"===n&&(t.params.message=t.params.message.replace(/<\/?[^>]+>/gi,""));this.trigger(n,t.params)}}},e.prototype.postMessage=function(e){var t=this.getIframeElm();window.postMessage&&window.JSON&&t&&t.contentWindow?(this.log("Sent to X-Payments: "+e.event+"\n"+window.JSON.stringify(e.params)),t.contentWindow.postMessage(window.JSON.stringify(e),"*")):this.log("Error sending message - iframe wasn't initialized!")},e.prototype.log=function(e){this.config.debug&&console.log(e)}}();