From ea1035e414dc18c388f41f4734680e00431e125f Mon Sep 17 00:00:00 2001 From: Agustin Kassis Date: Wed, 1 Apr 2020 22:58:24 -0300 Subject: [PATCH] Lookup invoice on lnd if not already cached as paid on redis --- class/User.js | 19 ++++++++++++------- controllers/api.js | 5 ++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/class/User.js b/class/User.js index c203b22..863a5bf 100644 --- a/class/User.js +++ b/class/User.js @@ -234,6 +234,17 @@ export class User { return await this._redis.get('ispaid_' + payment_hash); } + async syncInvoicePaid(payment_hash) { + const invoice = await this.lookupInvoice(payment_hash); + const ispaid = invoice.settled; // TODO: start using `state` instead as its future proof, and this one might get deprecated + if (ispaid) { + // so invoice was paid after all + await this.setPaymentHashPaid(payment_hash); + await this.clearBalanceCache(); + } + return ispaid; + } + async getUserInvoices(limit) { let range = await this._redis.lrange('userinvoices_for_' + this._userid, 0, -1); if (limit && !isNaN(parseInt(limit))) { @@ -261,13 +272,7 @@ export class User { if (!invoice.ispaid) { if (decoded && decoded.timestamp > +new Date() / 1000 - 3600 * 24 * 5) { // if invoice is not too old we query lnd to find out if its paid - let lookup_info = await this.lookupInvoice(invoice.payment_hash); - invoice.ispaid = lookup_info.settled; // TODO: start using `state` instead as its future proof, and this one might get deprecated - if (invoice.ispaid) { - // so invoice was paid after all - await this.setPaymentHashPaid(invoice.payment_hash); - await this.clearBalanceCache(); - } + invoice.ispaid = await this.syncInvoicePaid(invoice.payment_hash); } } else { _invoice_ispaid_cache[invoice.payment_hash] = true; diff --git a/controllers/api.js b/controllers/api.js index 90ddcff..0f0e818 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -266,7 +266,10 @@ router.get('/checkpayment/:payment_hash', async function(req, res) { return errorBadAuth(res); } - let paid = !!(await u.getPaymentHashPaid(req.params.payment_hash)); + let paid = true; + if (!(await u.getPaymentHashPaid(req.params.payment_hash))) { // Not found on cache + paid = await u.syncInvoicePaid(req.params.payment_hash); + } res.send({paid: paid}); });