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

94 lines
2.9 KiB
Python
Raw Normal View History

import hashlib
2021-09-19 13:34:31 +02:00
from lnbits.extensions.offlineshop.models import Item
2021-09-16 19:42:05 +02:00
from fastapi.params import Query
from starlette.requests import Request
from lnbits.helpers import url_for
2021-10-17 18:33:29 +01:00
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")
2021-09-19 13:34:31 +02:00
async def lnurl_response(req: Request, item_id: int = Query(...)):
2021-10-17 18:33:29 +01:00
item = await get_item(item_id) # type: Item
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(
2021-09-19 13:34:31 +02:00
callback=req.url_for("offlineshop.lnurl_callback", item_id=item.id),
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-09-19 13:34:31 +02:00
@offlineshop_ext.get("/lnurl/cb/{item_id}", name="offlineshop.lnurl_callback")
2021-09-16 19:42:05 +02:00
async def lnurl_callback(request: Request, item_id: int):
2021-10-17 18:33:29 +01:00
item = await get_item(item_id) # type: Item
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
2021-09-19 13:34:31 +02:00
amount_received = int(request.query_params.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-10-17 18:33:29 +01: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()