class CoinazerPay { constructor(options) { this.iframe = document.createElement("iframe"); this.callback = options.callback || null; this.apiKey = options.apiKey || null; this.email = options.email || null; this.amount = options.amount || null; this.currency = options.currency || null; // Add an event listener to the window to listen for messages from the iframe window.addEventListener("message", this.handleIframeMessage.bind(this)); } openWindow() { // let body = document.getElementsByTagName('body'); this.url = `merchantToken=${this.apiKey}¤cy=${this.currency}&email=${this.email}&amount=${this.amount}`; // this.base = "http://localhost:4000/checkout/?"; this.base = 'https://coinazer.com/checkout/?'; this.iframe.id = "azer_holder"; this.iframe.style.backgroundColor = "rgba(0,0,0,0.6)"; this.iframe.style.allowTransparency = true; this.iframe.style.border = "0px none transparent"; this.iframe.style.overflow = "hidden"; this.iframe.style.position = "fixed"; this.iframe.style.top = "0px"; this.iframe.style.left = "0px"; this.iframe.style.width = "100%"; this.iframe.style.zIndex = 2147483647; this.iframe.style.padding = "0px"; this.iframe.style.margin = "0px"; this.iframe.style.height = "100%"; this.iframe.style.visibility = "visible"; this.iframe.src = this.base + this.url; document.body.appendChild(this.iframe); // i.src = 'https://coinazer.com/pay/?id=chmq2see'; // body.appendChild(this.iframe); } onLoad(e) { if (this.callback && typeof this.callback.onLoadEvent === "function") { this.callback.onLoadEvent(e); } } onCloseEvent() { if (this.callback && typeof this.callback.onCloseEvent === "function") { this.callback.onCloseEvent(); } } onSelect(p) { if (this.callback && typeof this.callback.onSelectedCrypto === "function") { this.callback.onSelectedCrypto(p); } } onSuccessMessage(e) { if ( this.callback && typeof this.callback.onTransactionSuccess === "function" ) { this.callback.onTransactionSuccess(e); } } onCancel() { if ( this.callback && typeof this.callback.onTransactionCancel === "function" ) { this.callback.onTransactionCancel(); } } OnTransactionRestart(e) { if ( this.callback && typeof this.callback.OnTransactionRestart === "function" ) { this.callback.OnTransactionRestart(e); } } handleIframeMessage(event) { // Check that the message came from the expected iframe if (event.source === this.iframe.contentWindow) { if (event.data.type === "OnLoad") { this.onLoad(event.data.data); } else if (event.data.type === "OnCancel") { this.onCancel(); } else if (event.data.type === "OnSelect") { this.onSelect(event.data.data); } else if (event.data === "onCloseEvent") { this.onCloseEvent(); } else if (event.data.type === "OnSuccessMessage") { this.onSuccessMessage(event.data.data); } else if (event.data.type === "OnTransactionRestart") { this.OnTransactionRestart(event.data.data); } } } }