diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 91d53ca13..b5c3d9fa2 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -140,11 +140,12 @@ async def pay_invoice( else: # actually pay the external invoice payment: PaymentResponse = await WALLET.pay_invoice(payment_request) - if payment.ok and payment.checking_id: + if payment.checking_id: await create_payment( checking_id=payment.checking_id, fee=payment.fee_msat, preimage=payment.preimage, + pending=payment.ok == None, **payment_kwargs, ) await delete_payment(temp_id) diff --git a/lnbits/wallets/base.py b/lnbits/wallets/base.py index 469f7ec07..3f131c1e8 100644 --- a/lnbits/wallets/base.py +++ b/lnbits/wallets/base.py @@ -15,9 +15,8 @@ class InvoiceResponse(NamedTuple): class PaymentResponse(NamedTuple): - ok: Optional[ - bool - ] = None # when ok is None it means we don't know if this succeeded + # when ok is None it means we don't know if this succeeded + ok: Optional[bool] = None checking_id: Optional[str] = None # payment_hash, rcp_id fee_msat: int = 0 preimage: Optional[str] = None diff --git a/lnbits/wallets/spark.py b/lnbits/wallets/spark.py index 1586bbb0d..c97dd33f3 100644 --- a/lnbits/wallets/spark.py +++ b/lnbits/wallets/spark.py @@ -109,7 +109,34 @@ class SparkWallet(Wallet): try: r = await self.pay(bolt11) except (SparkError, UnknownError) as exc: - return PaymentResponse(False, None, 0, None, str(exc)) + listpays = await self.listpays(bolt11) + if listpays: + pays = listpays["pays"] + + if len(pays) == 0: + return PaymentResponse(False, None, 0, None, str(exc)) + + pay = pays[0] + payment_hash = pay["payment_hash"] + + if len(pays) > 1: + raise Exception( + f"listpays({payment_hash}) returned an unexpected response: {listpays}" + ) + + if pay["status"] == "failed": + return PaymentResponse(False, None, 0, None, str(exc)) + elif pay["status"] == "pending": + return PaymentResponse(None, listpays["pays"], 0, None, None) + elif pay["status"] == "complete": + r = pay + r["payment_preimage"] = pay["preimage"] + r["msatoshi"] = int(pay["amount_msat"][0:-4]) + r["msatoshi_sent"] = int(pay["amount_sent_msat"][0:-4]) + # this may result in an error if it was paid previously + # our database won't allow the same payment_hash to be added twice + # this is good + pass fee_msat = r["msatoshi_sent"] - r["msatoshi"] preimage = r["payment_preimage"]