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",
|
name="lnurlp.api_lnurl_lnaddr_callback",
|
||||||
)
|
)
|
||||||
async def 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(
|
@lnurlp_ext.get(
|
||||||
|
|
@ -33,7 +35,11 @@ async def api_lnurl_lnaddr_callback(
|
||||||
name="lnurlp.api_lnurl_callback",
|
name="lnurlp.api_lnurl_callback",
|
||||||
)
|
)
|
||||||
async def 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)
|
link = await increment_pay_link(link_id, served_pr=1)
|
||||||
if not link:
|
if not link:
|
||||||
|
|
@ -64,11 +70,15 @@ async def api_lnurl_callback(
|
||||||
comment = request.query_params.get("comment")
|
comment = request.query_params.get("comment")
|
||||||
if len(comment or "") > link.comment_chars:
|
if len(comment or "") > link.comment_chars:
|
||||||
return LnurlErrorResponse(
|
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()
|
).dict()
|
||||||
|
|
||||||
if lnaddress:
|
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
|
link.domain = urlparse(str(request.url)).netloc
|
||||||
|
|
||||||
extra = {
|
extra = {
|
||||||
|
|
@ -80,6 +90,9 @@ async def api_lnurl_callback(
|
||||||
if comment:
|
if comment:
|
||||||
extra["comment"] = (comment,)
|
extra["comment"] = (comment,)
|
||||||
|
|
||||||
|
if webhook_data:
|
||||||
|
extra["webhook_data"] = webhook_data
|
||||||
|
|
||||||
# nip 57
|
# nip 57
|
||||||
nostr = request.query_params.get("nostr")
|
nostr = request.query_params.get("nostr")
|
||||||
if nostr:
|
if nostr:
|
||||||
|
|
@ -88,13 +101,14 @@ async def api_lnurl_callback(
|
||||||
if lnaddress and link.username and link.domain:
|
if lnaddress and link.username and link.domain:
|
||||||
extra["lnaddress"] = f"{link.username}@{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(
|
payment_hash, payment_request = await create_invoice(
|
||||||
wallet_id=link.wallet,
|
wallet_id=link.wallet,
|
||||||
amount=int(amount / 1000),
|
amount=int(amount / 1000),
|
||||||
memo=link.description,
|
memo=link.description,
|
||||||
unhashed_description=nostr.encode()
|
unhashed_description=unhashed_description,
|
||||||
if nostr # we take the zap request as the description instead of the LNURL metadata if present
|
|
||||||
else link.lnurlpay_metadata.encode(),
|
|
||||||
extra=extra,
|
extra=extra,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -110,7 +124,7 @@ async def api_lnurl_callback(
|
||||||
|
|
||||||
|
|
||||||
@lnurlp_ext.get(
|
@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,
|
status_code=HTTPStatus.OK,
|
||||||
name="lnurlp.api_lnurl_response.deprecated",
|
name="lnurlp.api_lnurl_response.deprecated",
|
||||||
)
|
)
|
||||||
|
|
@ -119,7 +133,9 @@ async def api_lnurl_callback(
|
||||||
status_code=HTTPStatus.OK,
|
status_code=HTTPStatus.OK,
|
||||||
name="lnurlp.api_lnurl_response",
|
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)
|
link = await increment_pay_link(link_id, served_meta=1)
|
||||||
if not link:
|
if not link:
|
||||||
raise HTTPException(
|
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
|
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
|
||||||
|
|
||||||
if lnaddress:
|
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
|
link.domain = urlparse(str(request.url)).netloc
|
||||||
callback = str(
|
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:
|
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(
|
resp = LnurlPayResponse(
|
||||||
callback=callback,
|
callback=callback,
|
||||||
|
|
|
||||||
2
tasks.py
2
tasks.py
|
|
@ -40,6 +40,7 @@ async def on_invoice_paid(payment: Payment):
|
||||||
logger.error("Invoice paid. But no pay link id found.")
|
logger.error("Invoice paid. But no pay link id found.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
pay_link = await get_pay_link(pay_link_id)
|
pay_link = await get_pay_link(pay_link_id)
|
||||||
if not pay_link:
|
if not pay_link:
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
@ -66,6 +67,7 @@ async def send_webhook(payment: Payment, pay_link: PayLink):
|
||||||
"payment_request": payment.bolt11,
|
"payment_request": payment.bolt11,
|
||||||
"amount": payment.amount,
|
"amount": payment.amount,
|
||||||
"comment": payment.extra.get("comment"),
|
"comment": payment.extra.get("comment"),
|
||||||
|
"webhook_data": payment.extra.get("webhook_data") or "",
|
||||||
"lnurlp": pay_link.id,
|
"lnurlp": pay_link.id,
|
||||||
"body": json.loads(pay_link.webhook_body)
|
"body": json.loads(pay_link.webhook_body)
|
||||||
if pay_link.webhook_body
|
if pay_link.webhook_body
|
||||||
|
|
|
||||||
|
|
@ -175,9 +175,6 @@
|
||||||
label="Lightning Address"
|
label="Lightning Address"
|
||||||
@input="formDialog.data.username = formDialog.data.username.toLowerCase()"
|
@input="formDialog.data.username = formDialog.data.username.toLowerCase()"
|
||||||
/>
|
/>
|
||||||
<span class="label">
|
|
||||||
@ {% raw %} {{ domain }} {% endraw %}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="col" style="margin-top: 10px">
|
<div class="col" style="margin-top: 10px">
|
||||||
<span class="label">
|
<span class="label">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue