feat: pass data through lnurl for webhook (#31)
* feat: pass through data with lnurl
This commit is contained in:
parent
f2e419e18d
commit
84179e8eea
3 changed files with 43 additions and 16 deletions
54
lnurl.py
54
lnurl.py
|
|
@ -22,9 +22,11 @@ from .crud import (
|
|||
name="lnurlp.api_lnurl_lnaddr_callback",
|
||||
)
|
||||
async def api_lnurl_lnaddr_callback(
|
||||
request: Request, link_id, amount: int = Query(...)
|
||||
request: Request, link_id, amount: int = Query(...), webhook_data: str = Query(None)
|
||||
):
|
||||
return await api_lnurl_callback(request, link_id, amount, lnaddress=True)
|
||||
return await api_lnurl_callback(
|
||||
request, link_id, amount, webhook_data, lnaddress=True
|
||||
)
|
||||
|
||||
|
||||
@lnurlp_ext.get(
|
||||
|
|
@ -33,7 +35,11 @@ async def api_lnurl_lnaddr_callback(
|
|||
name="lnurlp.api_lnurl_callback",
|
||||
)
|
||||
async def api_lnurl_callback(
|
||||
request: Request, link_id, amount: int = Query(...), lnaddress=False
|
||||
request: Request,
|
||||
link_id,
|
||||
amount: int = Query(...),
|
||||
webhook_data: str = Query(None),
|
||||
lnaddress=False,
|
||||
):
|
||||
link = await increment_pay_link(link_id, served_pr=1)
|
||||
if not link:
|
||||
|
|
@ -64,11 +70,15 @@ async def api_lnurl_callback(
|
|||
comment = request.query_params.get("comment")
|
||||
if len(comment or "") > link.comment_chars:
|
||||
return LnurlErrorResponse(
|
||||
reason=f"Got a comment with {len(comment)} characters, but can only accept {link.comment_chars}"
|
||||
reason=(
|
||||
f"Got a comment with {len(comment or '')} characters, "
|
||||
f"but can only accept {link.comment_chars}"
|
||||
)
|
||||
).dict()
|
||||
|
||||
if lnaddress:
|
||||
# for lnaddress, we have to set this otherwise the metadata won't have the identifier
|
||||
# for lnaddress, we have to set this otherwise
|
||||
# the metadata won't have the identifier
|
||||
link.domain = urlparse(str(request.url)).netloc
|
||||
|
||||
extra = {
|
||||
|
|
@ -80,6 +90,9 @@ async def api_lnurl_callback(
|
|||
if comment:
|
||||
extra["comment"] = (comment,)
|
||||
|
||||
if webhook_data:
|
||||
extra["webhook_data"] = webhook_data
|
||||
|
||||
# nip 57
|
||||
nostr = request.query_params.get("nostr")
|
||||
if nostr:
|
||||
|
|
@ -88,13 +101,14 @@ async def api_lnurl_callback(
|
|||
if lnaddress and link.username and link.domain:
|
||||
extra["lnaddress"] = f"{link.username}@{link.domain}"
|
||||
|
||||
# we take the zap request as the description instead of the metadata if present
|
||||
unhashed_description = nostr.encode() if nostr else link.lnurlpay_metadata.encode()
|
||||
|
||||
payment_hash, payment_request = await create_invoice(
|
||||
wallet_id=link.wallet,
|
||||
amount=int(amount / 1000),
|
||||
memo=link.description,
|
||||
unhashed_description=nostr.encode()
|
||||
if nostr # we take the zap request as the description instead of the LNURL metadata if present
|
||||
else link.lnurlpay_metadata.encode(),
|
||||
unhashed_description=unhashed_description,
|
||||
extra=extra,
|
||||
)
|
||||
|
||||
|
|
@ -110,7 +124,7 @@ async def api_lnurl_callback(
|
|||
|
||||
|
||||
@lnurlp_ext.get(
|
||||
"/api/v1/lnurl/{link_id}", # Backwards compatibility for old LNURLs / QR codes (with long URL)
|
||||
"/api/v1/lnurl/{link_id}", # Backwards compatibility for old LNURLs / QR codes
|
||||
status_code=HTTPStatus.OK,
|
||||
name="lnurlp.api_lnurl_response.deprecated",
|
||||
)
|
||||
|
|
@ -119,7 +133,9 @@ async def api_lnurl_callback(
|
|||
status_code=HTTPStatus.OK,
|
||||
name="lnurlp.api_lnurl_response",
|
||||
)
|
||||
async def api_lnurl_response(request: Request, link_id, lnaddress=False):
|
||||
async def api_lnurl_response(
|
||||
request: Request, link_id, webhook_data: str = Query(None), lnaddress=False
|
||||
):
|
||||
link = await increment_pay_link(link_id, served_meta=1)
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
|
|
@ -129,13 +145,25 @@ async def api_lnurl_response(request: Request, link_id, lnaddress=False):
|
|||
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
|
||||
|
||||
if lnaddress:
|
||||
# for lnaddress, we have to set this otherwise the metadata won't have the identifier
|
||||
# for lnaddress, we have to set this otherwise
|
||||
# the metadata won't have the identifier
|
||||
link.domain = urlparse(str(request.url)).netloc
|
||||
callback = str(
|
||||
request.url_for("lnurlp.api_lnurl_lnaddr_callback", link_id=link.id)
|
||||
request.url_for(
|
||||
"lnurlp.api_lnurl_lnaddr_callback",
|
||||
link_id=link.id,
|
||||
)
|
||||
)
|
||||
else:
|
||||
callback = str(request.url_for("lnurlp.api_lnurl_callback", link_id=link.id))
|
||||
callback = str(
|
||||
request.url_for(
|
||||
"lnurlp.api_lnurl_callback",
|
||||
link_id=link.id,
|
||||
)
|
||||
)
|
||||
|
||||
if webhook_data:
|
||||
callback += f"?webhook_data={webhook_data}"
|
||||
|
||||
resp = LnurlPayResponse(
|
||||
callback=callback,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue