From d44339b018ed9e1d5c014cb29ea01b3f3cf2ff18 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 14 Mar 2024 14:38:10 +0200 Subject: [PATCH] fix: pending property for PaymentStatus (#2324) * fix: pending property for PaymentStatus * fix: invoice status * fix: check pending status from the payment details * refactor: make condition more explicit --- lnbits/wallets/base.py | 2 +- lnbits/wallets/fake.py | 9 +++++++-- lnbits/wallets/lnbits.py | 12 ++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lnbits/wallets/base.py b/lnbits/wallets/base.py index 031cccc88..ce59f140b 100644 --- a/lnbits/wallets/base.py +++ b/lnbits/wallets/base.py @@ -35,7 +35,7 @@ class PaymentStatus(NamedTuple): @property def pending(self) -> bool: - return self.paid is not True + return self.paid is None @property def failed(self) -> bool: diff --git a/lnbits/wallets/fake.py b/lnbits/wallets/fake.py index 6d3b5772b..5ef407e54 100644 --- a/lnbits/wallets/fake.py +++ b/lnbits/wallets/fake.py @@ -19,9 +19,11 @@ from lnbits.settings import settings from .base import ( InvoiceResponse, + PaymentFailedStatus, PaymentPendingStatus, PaymentResponse, PaymentStatus, + PaymentSuccessStatus, StatusResponse, Wallet, ) @@ -118,8 +120,11 @@ class FakeWallet(Wallet): ) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: - paid = checking_id in self.paid_invoices - return PaymentStatus(paid) + if checking_id in self.paid_invoices: + return PaymentSuccessStatus() + if checking_id in list(self.payment_secrets.keys()): + return PaymentPendingStatus() + return PaymentFailedStatus() async def get_payment_status(self, _: str) -> PaymentStatus: return PaymentPendingStatus() diff --git a/lnbits/wallets/lnbits.py b/lnbits/wallets/lnbits.py index 1693397fa..e2674c6ef 100644 --- a/lnbits/wallets/lnbits.py +++ b/lnbits/wallets/lnbits.py @@ -9,6 +9,7 @@ from lnbits.settings import settings from .base import ( InvoiceResponse, + PaymentFailedStatus, PaymentPendingStatus, PaymentResponse, PaymentStatus, @@ -121,9 +122,16 @@ class LNbitsWallet(Wallet): r = await self.client.get( url=f"/api/v1/payments/{checking_id}", ) - if r.is_error: + r.raise_for_status() + + data = r.json() + details = data.get("details", None) + + if details and details.get("pending", False) is True: return PaymentPendingStatus() - return PaymentStatus(r.json()["paid"]) + if data.get("paid", False) is True: + return PaymentSuccessStatus() + return PaymentFailedStatus() except Exception: return PaymentPendingStatus()