diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index bd92e379b..8eaf02e9a 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -159,6 +159,17 @@ def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) -> return [Payment(**row) for row in rows] +def delete_wallet_payments_expired(wallet_id: str, *, seconds: int = 86400) -> None: + with open_db() as db: + db.execute( + """ + DELETE + FROM apipayments WHERE wallet = ? AND pending = 1 AND time < strftime('%s', 'now') - ? + """, + (wallet_id, seconds), + ) + + # payments # -------- diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 9761f5f03..a84430030 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -5,14 +5,17 @@ def m001_initial(db): """ Initial LNbits tables. """ - db.execute(""" + db.execute( + """ CREATE TABLE IF NOT EXISTS accounts ( id TEXT PRIMARY KEY, email TEXT, pass TEXT ); - """) - db.execute(""" + """ + ) + db.execute( + """ CREATE TABLE IF NOT EXISTS extensions ( user TEXT NOT NULL, extension TEXT NOT NULL, @@ -20,8 +23,10 @@ def m001_initial(db): UNIQUE (user, extension) ); - """) - db.execute(""" + """ + ) + db.execute( + """ CREATE TABLE IF NOT EXISTS wallets ( id TEXT PRIMARY KEY, name TEXT NOT NULL, @@ -29,8 +34,10 @@ def m001_initial(db): adminkey TEXT NOT NULL, inkey TEXT ); - """) - db.execute(""" + """ + ) + db.execute( + """ CREATE TABLE IF NOT EXISTS apipayments ( payhash TEXT NOT NULL, amount INTEGER NOT NULL, @@ -42,8 +49,10 @@ def m001_initial(db): UNIQUE (wallet, payhash) ); - """) - db.execute(""" + """ + ) + db.execute( + """ CREATE VIEW IF NOT EXISTS balances AS SELECT wallet, COALESCE(SUM(s), 0) AS balance FROM ( SELECT wallet, SUM(amount) AS s -- incoming @@ -57,7 +66,8 @@ def m001_initial(db): GROUP BY wallet ) GROUP BY wallet; - """) + """ + ) def migrate(): diff --git a/lnbits/core/models.py b/lnbits/core/models.py index 14a9429da..c57c28130 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -39,6 +39,11 @@ class Wallet(NamedTuple): return get_wallet_payments(self.id, include_all_pending=include_all_pending) + def delete_expired_payments(self, seconds: int = 86400) -> None: + from .crud import delete_wallet_payments_expired + + delete_wallet_payments_expired(self.id, seconds=seconds) + class Payment(NamedTuple): checking_id: str diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index ed3b7f0d2..aa778cb5a 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -12,6 +12,8 @@ from ..services import create_invoice, pay_invoice @api_check_wallet_key("invoice") def api_payments(): if "check_pending" in request.args: + g.wallet.delete_expired_payments() + for payment in g.wallet.get_payments(include_all_pending=True): if payment.is_out: payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending) @@ -74,13 +76,13 @@ def api_payment(checking_id): try: if payment.is_out: - is_paid = WALLET.get_payment_status(checking_id).paid + is_paid = not WALLET.get_payment_status(checking_id).pending elif payment.is_in: - is_paid = WALLET.get_invoice_status(checking_id).paid + is_paid = not WALLET.get_invoice_status(checking_id).pending except Exception: return jsonify({"paid": False}), Status.OK - if is_paid is True: + if is_paid: payment.set_pending(False) return jsonify({"paid": True}), Status.OK