diff --git a/assets/lntip.js b/assets/lntip.js index a851eab..61feb7b 100644 --- a/assets/lntip.js +++ b/assets/lntip.js @@ -6,8 +6,7 @@ LnTip = function (options) { this.host = options.host || host; this.amount = options.amount; this.memo = options.memo || ''; - this.loadStylesheet(); - this.getInvoice(); + this.loadStylesheet(); // load it early that styles are ready when the popup is opened } LnTip.prototype.loadStylesheet = function () { @@ -50,16 +49,19 @@ LnTip.prototype.thanks = function () { LnTip.prototype.watchPayment = function () { if (this.paymentWatcher) { window.clearInterval(this.paymentWatcher) } - this.paymentWatcher = window.setInterval(() => { - this._request(`${this.host}/settled/${this.invoice.ImplDepID}`) - .then((settled) => { - if (settled) { - this.invoice.settled = true; - this.thanks(); - this.stopWatchingPayment(); - } - }) - }, 2000); + + return new Promise((resolve, reject) => { + this.paymentWatcher = window.setInterval(() => { + this._fetch(`${this.host}/settled/${this.invoice.ImplDepID}`) + .then((settled) => { + if (settled) { + this.invoice.settled = true; + this.stopWatchingPayment(); + resolve(this.invoice); + } + }); + }, 2000); + }); } LnTip.prototype.stopWatchingPayment = function () { @@ -72,14 +74,14 @@ LnTip.prototype.payWithWebln = function () { webln.enable().then((weblnResponse) => { return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest }) }).catch((e) => { - this.requestPayment(); + return this.requestPayment(); }) } else { return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest }) } } -LnTip.prototype.requestPayment = function () { +LnTip.prototype.showPaymentRequest = function () { var content = `