lnbits-legend/lnbits/extensions/offlineshop/lnurl.py

87 lines
2.7 KiB
Python
Raw Normal View History

import hashlib
2021-09-16 19:42:05 +02:00
from fastapi.params import Query
from starlette.requests import Request
from lnbits.helpers import url_for
from lnurl import LnurlPayResponse, LnurlPayActionResponse, LnurlErrorResponse # type: ignore
from lnbits.core.services import create_invoice
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
from . import offlineshop_ext
from .crud import get_shop, get_item
2021-09-16 19:42:05 +02:00
@offlineshop_ext.get("/lnurl/{item_id}", name="offlineshop.lnurl_response")
async def lnurl_response(item_id: int = Query(...)):
item = await get_item(item_id)
if not item:
2021-08-21 00:34:48 +01:00
return {"status": "ERROR", "reason": "Item not found."}
if not item.enabled:
2021-08-21 00:34:48 +01:00
return {"status": "ERROR", "reason": "Item disabled."}
price_msat = (
await fiat_amount_as_satoshis(item.price, item.unit)
if item.unit != "sat"
else item.price
) * 1000
resp = LnurlPayResponse(
callback=url_for("offlineshop.lnurl_callback", item_id=item.id, _external=True),
min_sendable=price_msat,
max_sendable=price_msat,
metadata=await item.lnurlpay_metadata(),
)
2021-08-21 00:34:48 +01:00
return resp.dict()
2021-08-21 00:34:48 +01:00
@offlineshop_ext.get("/lnurl/cb/<item_id>")
2021-09-16 19:42:05 +02:00
async def lnurl_callback(request: Request, item_id: int):
item = await get_item(item_id)
if not item:
2021-08-21 00:34:48 +01:00
return {"status": "ERROR", "reason": "Couldn't find item."}
if item.unit == "sat":
min = item.price * 1000
max = item.price * 1000
else:
price = await fiat_amount_as_satoshis(item.price, item.unit)
# allow some fluctuation (the fiat price may have changed between the calls)
min = price * 995
max = price * 1010
amount_received = int(request.args.get("amount") or 0)
if amount_received < min:
2021-08-21 00:34:48 +01:00
return LnurlErrorResponse(
2021-09-16 19:42:05 +02:00
reason=f"Amount {amount_received} is smaller than minimum {min}."
).dict()
elif amount_received > max:
2021-08-21 00:34:48 +01:00
return LnurlErrorResponse(
2021-09-16 19:42:05 +02:00
reason=f"Amount {amount_received} is greater than maximum {max}."
).dict()
shop = await get_shop(item.shop)
try:
payment_hash, payment_request = await create_invoice(
wallet_id=shop.wallet,
amount=int(amount_received / 1000),
memo=item.name,
description_hash=hashlib.sha256(
(await item.lnurlpay_metadata()).encode("utf-8")
).digest(),
extra={"tag": "offlineshop", "item": item.id},
)
except Exception as exc:
2021-08-21 00:34:48 +01:00
return LnurlErrorResponse(reason=exc.message).dict()
resp = LnurlPayActionResponse(
pr=payment_request,
2021-09-16 19:42:05 +02:00
success_action=item.success_action(shop, payment_hash, request) if shop.method else None,
routes=[],
)
2021-08-21 00:34:48 +01:00
return resp.dict()