feat: update_payment_extra with webhook response
This commit is contained in:
parent
0ab99bef41
commit
60cd40d5e5
1 changed files with 32 additions and 6 deletions
|
|
@ -11,6 +11,7 @@ from loguru import logger
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import HTMLResponse
|
from starlette.responses import HTMLResponse
|
||||||
|
|
||||||
|
from lnbits.core.crud import update_payment_extra
|
||||||
from lnbits.core.services import pay_invoice
|
from lnbits.core.services import pay_invoice
|
||||||
|
|
||||||
from . import withdraw_ext
|
from . import withdraw_ext
|
||||||
|
|
@ -44,7 +45,11 @@ async def api_lnurl_response(request: Request, unique_hash):
|
||||||
"minWithdrawable": link.min_withdrawable * 1000,
|
"minWithdrawable": link.min_withdrawable * 1000,
|
||||||
"maxWithdrawable": link.max_withdrawable * 1000,
|
"maxWithdrawable": link.max_withdrawable * 1000,
|
||||||
"defaultDescription": link.title,
|
"defaultDescription": link.title,
|
||||||
|
"webhook_url": link.webhook_url,
|
||||||
|
"webhook_headers": link.webhook_headers,
|
||||||
|
"webhook_body": link.webhook_body,
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.dumps(withdrawResponse)
|
return json.dumps(withdrawResponse)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -56,7 +61,7 @@ async def api_lnurl_response(request: Request, unique_hash):
|
||||||
name="withdraw.api_lnurl_callback",
|
name="withdraw.api_lnurl_callback",
|
||||||
summary="lnurl withdraw callback",
|
summary="lnurl withdraw callback",
|
||||||
description="""
|
description="""
|
||||||
This enpoints allows you to put unique_hash, k1
|
This endpoints allows you to put unique_hash, k1
|
||||||
and a payment_request to get your payment_request paid.
|
and a payment_request to get your payment_request paid.
|
||||||
""",
|
""",
|
||||||
response_description="JSON with status",
|
response_description="JSON with status",
|
||||||
|
|
@ -143,18 +148,39 @@ async def api_lnurl_callback(
|
||||||
if link.webhook_url:
|
if link.webhook_url:
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
try:
|
try:
|
||||||
r = await client.post(
|
kwargs = {
|
||||||
link.webhook_url,
|
"json": {
|
||||||
json={
|
|
||||||
"payment_hash": payment_hash,
|
"payment_hash": payment_hash,
|
||||||
"payment_request": payment_request,
|
"payment_request": payment_request,
|
||||||
"lnurlw": link.id,
|
"lnurlw": link.id,
|
||||||
},
|
},
|
||||||
timeout=40,
|
"timeout": 40,
|
||||||
|
}
|
||||||
|
if link.webhook_body:
|
||||||
|
kwargs["json"]["body"] = json.loads(link.webhook_body)
|
||||||
|
if link.webhook_headers:
|
||||||
|
kwargs["headers"] = json.loads(link.webhook_headers)
|
||||||
|
|
||||||
|
r: httpx.Response = await client.post(link.webhook_url, **kwargs)
|
||||||
|
await update_payment_extra(
|
||||||
|
payment_hash,
|
||||||
|
dict(
|
||||||
|
{
|
||||||
|
"wh_success": r.is_success,
|
||||||
|
"wh_message": r.reason_phrase,
|
||||||
|
"wh_response": r.text,
|
||||||
|
}
|
||||||
|
),
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
# webhook fails shouldn't cause the lnurlw to fail since invoice is already paid
|
# webhook fails shouldn't cause the lnurlw to fail since invoice is already paid
|
||||||
logger.error("Caught exception when dispatching webhook url:", exc)
|
logger.error(
|
||||||
|
"Caught exception when dispatching webhook url: " + str(exc)
|
||||||
|
)
|
||||||
|
await update_payment_extra(
|
||||||
|
payment_hash,
|
||||||
|
dict({"wh_success": False, "wh_message": str(exc)}),
|
||||||
|
)
|
||||||
|
|
||||||
return {"status": "OK"}
|
return {"status": "OK"}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue