completing listener, need to fetch pay lnurlpay

This commit is contained in:
benarc
2021-12-06 13:07:47 +00:00
parent 5f3bbd9695
commit 410689e04b

View File

@@ -20,38 +20,33 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
# Check its got a payout associated with it
lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1))
if payment.extra.get("wh_status"): if lnurlpayout_link:
# this webhook has already been sent # Check the wallet balance is more than the threshold
return
# check link # Getthe invoice from the LNURL to pay
pay_link = await get_lnurlpayout(payment.extra.get("link", -1))
if pay_link and pay_link.webhook_url:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
r = await client.post( url = await api_payments_decode({"data": data.lnurlpay})
pay_link.webhook_url, if str(url["domain"])[0:4] != "http":
json={ raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
"payment_hash": payment.payment_hash, try:
"payment_request": payment.bolt11, r = await client.post(
"amount": payment.amount, str(url["domain"]),
"comment": payment.extra.get("comment"), json={
"lnurlp": pay_link.id, "payment_hash": payment.payment_hash,
}, "payment_request": payment.bolt11,
timeout=40, "amount": payment.amount,
) "comment": payment.extra.get("comment"),
await mark_webhook_sent(payment, r.status_code) "lnurlp": pay_link.id,
except (httpx.ConnectError, httpx.RequestError): },
await mark_webhook_sent(payment, -1) timeout=40,
)
await mark_webhook_sent(payment, r.status_code)
except (httpx.ConnectError, httpx.RequestError):
await mark_webhook_sent(payment, -1)
except Exception:
async def mark_webhook_sent(payment: Payment, status: int) -> None: raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
payment.extra["wh_status"] = status
await core_db.execute(
"""
UPDATE apipayments SET extra = ?
WHERE hash = ?
""",
(json.dumps(payment.extra), payment.payment_hash),
)