Lookup invoice on lnd if not already cached as paid on redis

This commit is contained in:
Agustin Kassis 2020-04-01 22:58:24 -03:00
parent 7afb56398a
commit ea1035e414
2 changed files with 16 additions and 8 deletions

View File

@ -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;

View File

@ -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});
});