Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mint: LNbitsWallet add extra check for payment state #601

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions cashu/lightning/lnbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,13 @@ async def status(self) -> StatusResponse:
try:
r = await self.client.get(url=f"{self.endpoint}/api/v1/wallet", timeout=15)
r.raise_for_status()
data: dict = r.json()
except Exception as exc:
return StatusResponse(
error_message=f"Failed to connect to {self.endpoint} due to: {exc}",
balance=0,
)

try:
data: dict = r.json()
except Exception:
return StatusResponse(
error_message=(
f"Received invalid response from {self.endpoint}: {r.text}"
),
balance=0,
)
if "detail" in data:
if data.get("detail"):
return StatusResponse(
error_message=f"LNbits error: {data['detail']}", balance=0
)
Expand Down Expand Up @@ -85,13 +76,16 @@ async def create_invoice(
url=f"{self.endpoint}/api/v1/payments", json=data
)
r.raise_for_status()
except Exception:
data = r.json()
except httpx.HTTPStatusError:
return InvoiceResponse(
ok=False,
error_message=r.json()["detail"],
ok=False, error_message=f"HTTP status: {r.reason_phrase}"
)
except Exception as exc:
return InvoiceResponse(ok=False, error_message=str(exc))
if data.get("detail"):
return InvoiceResponse(ok=False, error_message=data["detail"])

data = r.json()
checking_id, payment_request = data["checking_id"], data["payment_request"]

return InvoiceResponse(
Expand All @@ -110,21 +104,27 @@ async def pay_invoice(
timeout=None,
)
r.raise_for_status()
except Exception:
return PaymentResponse(error_message=r.json()["detail"])
if r.status_code > 299:
data: dict = r.json()
except httpx.HTTPStatusError:
return PaymentResponse(error_message=(f"HTTP status: {r.reason_phrase}",))
if "detail" in r.json():
return PaymentResponse(error_message=(r.json()["detail"],))
except Exception as exc:
return PaymentResponse(error_message=str(exc))
if data.get("detail"):
return PaymentResponse(error_message=data["detail"])

data: dict = r.json()
checking_id = data["payment_hash"]

# we do this to get the fee and preimage
payment: PaymentStatus = await self.get_payment_status(checking_id)

if not payment.paid:
return PaymentResponse(
ok=False,
error_message="Payment failed.",
)

return PaymentResponse(
ok=True,
ok=payment.paid,
checking_id=checking_id,
fee=payment.fee,
preimage=payment.preimage,
Expand All @@ -136,22 +136,22 @@ async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
)
r.raise_for_status()
data: dict = r.json()
except Exception:
return PaymentStatus(paid=None)
data: dict = r.json()
if data.get("detail"):
return PaymentStatus(paid=None)
return PaymentStatus(paid=r.json()["paid"])
return PaymentStatus(paid=data["paid"])

async def get_payment_status(self, checking_id: str) -> PaymentStatus:
try:
r = await self.client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}"
)
r.raise_for_status()
data = r.json()
except Exception:
return PaymentStatus(paid=None)
data = r.json()
if "paid" not in data and "details" not in data:
return PaymentStatus(paid=None)

Expand Down
Loading