mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-19 12:01:12 +02:00
lnurl withdraw, better errorhandling for callback endpoint (#1107)
This commit is contained in:
@@ -9,7 +9,7 @@ from fastapi import HTTPException
|
|||||||
from fastapi.param_functions import Query
|
from fastapi.param_functions import Query
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import HTMLResponse # type: ignore
|
from starlette.responses import HTMLResponse
|
||||||
|
|
||||||
from lnbits.core.services import pay_invoice
|
from lnbits.core.services import pay_invoice
|
||||||
|
|
||||||
@@ -51,10 +51,24 @@ async def api_lnurl_response(request: Request, unique_hash):
|
|||||||
# CALLBACK
|
# CALLBACK
|
||||||
|
|
||||||
|
|
||||||
@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", name="withdraw.api_lnurl_callback")
|
@withdraw_ext.get(
|
||||||
|
"/api/v1/lnurl/cb/{unique_hash}",
|
||||||
|
name="withdraw.api_lnurl_callback",
|
||||||
|
summary="lnurl withdraw callback",
|
||||||
|
description="""
|
||||||
|
This enpoints allows you to put unique_hash, k1
|
||||||
|
and a payment_request to get your payment_request paid.
|
||||||
|
""",
|
||||||
|
response_description="JSON with status",
|
||||||
|
responses={
|
||||||
|
200: {"description": "status: OK"},
|
||||||
|
400: {"description": "k1 is wrong or link open time or withdraw not working."},
|
||||||
|
404: {"description": "withdraw link not found."},
|
||||||
|
405: {"description": "withdraw link is spent."},
|
||||||
|
},
|
||||||
|
)
|
||||||
async def api_lnurl_callback(
|
async def api_lnurl_callback(
|
||||||
unique_hash,
|
unique_hash,
|
||||||
request: Request,
|
|
||||||
k1: str = Query(...),
|
k1: str = Query(...),
|
||||||
pr: str = Query(...),
|
pr: str = Query(...),
|
||||||
id_unique_hash=None,
|
id_unique_hash=None,
|
||||||
@@ -63,19 +77,22 @@ async def api_lnurl_callback(
|
|||||||
now = int(datetime.now().timestamp())
|
now = int(datetime.now().timestamp())
|
||||||
if not link:
|
if not link:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found"
|
status_code=HTTPStatus.NOT_FOUND, detail="withdraw not found."
|
||||||
)
|
)
|
||||||
|
|
||||||
if link.is_spent:
|
if link.is_spent:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent."
|
status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail="withdraw is spent."
|
||||||
)
|
)
|
||||||
|
|
||||||
if link.k1 != k1:
|
if link.k1 != k1:
|
||||||
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Bad request.")
|
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="k1 is wrong.")
|
||||||
|
|
||||||
if now < link.open_time:
|
if now < link.open_time:
|
||||||
return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=f"wait link open_time {link.open_time - now} seconds.",
|
||||||
|
)
|
||||||
|
|
||||||
usescsv = ""
|
usescsv = ""
|
||||||
|
|
||||||
@@ -95,7 +112,7 @@ async def api_lnurl_callback(
|
|||||||
usescsv = ",".join(useslist)
|
usescsv = ",".join(useslist)
|
||||||
if not found:
|
if not found:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
status_code=HTTPStatus.NOT_FOUND, detail="withdraw not found."
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
usescsv = usescsv[1:]
|
usescsv = usescsv[1:]
|
||||||
@@ -144,7 +161,9 @@ async def api_lnurl_callback(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
await update_withdraw_link(link.id, **changesback)
|
await update_withdraw_link(link.id, **changesback)
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
return {"status": "ERROR", "reason": "Link not working"}
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST, detail=f"withdraw not working. {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# FOR LNURLs WHICH ARE UNIQUE
|
# FOR LNURLs WHICH ARE UNIQUE
|
||||||
|
Reference in New Issue
Block a user