UNPKG

@bitcoin-suisse/pay

Version:

Bitcoin Suisse Pay plugin for crypto payments

123 lines 4.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CryptoPayments = void 0; class CryptoPayments { constructor(config) { this.endpointUrl = 'https://terminal.bitcoinsuisse.com'; this.validateConfig(config); this.createIframe(); if (config.target) { config.target.appendChild(this.frame); } else { document.body.appendChild(this.frame); } window.addEventListener('message', this.onMessageFromSandbox.bind(this), false); } validateConfig(config) { if (!config.terminalId) throw new Error('Crypto Payments: terminalId is required.'); if (!config.auth) throw new Error('Crypto Payments: auth is required.'); if (!config.paymentId) { if (!config.amount) throw new Error('Crypto Payments: amount is required.'); if (!config.fromCurrency) throw new Error('Crypto Payments: fromCurrency is required.'); } this.config = config; } createIframe() { this.frame = document.createElement('iframe'); this.frame.id = 'iframe-container'; this.frame.src = this.createUrl(); this.frame.setAttribute('style', CryptoPayments.style); } createUrl() { var _a; const url = new URL(this.endpointUrl); if (this.config.paymentId) { url.pathname = `${this.config.terminalId}/${this.config.paymentId}`; } else { url.pathname = this.config.terminalId; url.searchParams.append('amount', this.config.amount.toString()); url.searchParams.append('request', this.config.fromCurrency); } url.searchParams.append('auth', this.config.auth); if (this.config.paymentCallbackUrl) url.searchParams.append('callbackUrl', this.config.paymentCallbackUrl); url.searchParams.append('language', (_a = this.config.language) !== null && _a !== void 0 ? _a : 'en'); if (this.config.referenceId) url.searchParams.append('referenceId', this.config.referenceId); return url.toString(); } create() { this.promise = new Promise((resolve, reject) => { this.resolver = resolve; this.rejecter = reject; }); return this.promise; } resolve() { this.frame.setAttribute('style', CryptoPayments.styleDisabled + CryptoPayments.style); setTimeout(() => { this.resolver(this.data); this.frame.parentNode.removeChild(this.frame); }, 5000); } reject(quick) { this.frame.setAttribute('style', CryptoPayments.styleDisabled + CryptoPayments.style); setTimeout(() => { this.rejecter(this.data); this.frame.parentNode.removeChild(this.frame); }, quick ? 0 : 5000); } onMessageFromSandbox(event) { try { this.data = JSON.parse(event.data); } catch (e) { console.error('Faulty JSON when parsing'); } if (!this.data) { return; } if (this.data.closeRequest === true) { this.reject(true); } else { switch (this.data.state) { case 'crypto-payments-status-CANCELED': case 'crypto-payments-status-EXPIRED': this.reject(); break; case 'crypto-payments-status-PAID': case 'crypto-payments-status-PAIDOVER': case 'crypto-payments-status-PAIDLATE': this.resolve(); break; } } } } exports.CryptoPayments = CryptoPayments; CryptoPayments.style = ` position: absolute; width: 400px; height: 660px; border: 0px; border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.07) 4px 4px 24px 0px; top: 0px; bottom: 0px; left: 0px; right: 0px; margin: auto; z-index: 1000; `; CryptoPayments.styleDisabled = ` pointer-events: none; opacity: 0.7; `; //# sourceMappingURL=index.js.map