diff --git a/lnbits/extensions/satspay/__init__.py b/lnbits/extensions/satspay/__init__.py index 4bdaa2b6..7b7f0bde 100644 --- a/lnbits/extensions/satspay/__init__.py +++ b/lnbits/extensions/satspay/__init__.py @@ -1,13 +1,25 @@ -from quart import Blueprint +import asyncio + +from fastapi import APIRouter + from lnbits.db import Database +from lnbits.helpers import template_renderer db = Database("ext_satspay") -satspay_ext: Blueprint = Blueprint( - "satspay", __name__, static_folder="static", template_folder="templates" +satspay_ext: APIRouter = APIRouter( + prefix="/satspay", + tags=["satspay"] ) +def satspay_renderer(): + return template_renderer( + [ + "lnbits/extensions/satspay/templates", + ] + ) + from .views_api import * # noqa from .views import * # noqa diff --git a/lnbits/extensions/satspay/crud.py b/lnbits/extensions/satspay/crud.py index 56cabdbe..fab0406f 100644 --- a/lnbits/extensions/satspay/crud.py +++ b/lnbits/extensions/satspay/crud.py @@ -2,11 +2,10 @@ from typing import List, Optional, Union # from lnbits.db import open_ext_db from . import db -from .models import Charges +from .models import Charges, CreateCharge from lnbits.helpers import urlsafe_short_hash -from quart import jsonify import httpx from lnbits.core.services import create_invoice, check_invoice_status from ..watchonly.crud import get_watch_wallet, get_fresh_address, get_mempool @@ -17,25 +16,27 @@ from ..watchonly.crud import get_watch_wallet, get_fresh_address, get_mempool async def create_charge( user: str, - description: str = None, - onchainwallet: Optional[str] = None, - lnbitswallet: Optional[str] = None, - webhook: Optional[str] = None, - completelink: Optional[str] = None, - completelinktext: Optional[str] = "Back to Merchant", - time: Optional[int] = None, - amount: Optional[int] = None, + data: CreateCharge + # user: str, + # description: str = None, + # onchainwallet: Optional[str] = None, + # lnbitswallet: Optional[str] = None, + # webhook: Optional[str] = None, + # completelink: Optional[str] = None, + # completelinktext: Optional[str] = "Back to Merchant", + # time: Optional[int] = None, + # amount: Optional[int] = None, ) -> Charges: charge_id = urlsafe_short_hash() - if onchainwallet: - wallet = await get_watch_wallet(onchainwallet) - onchain = await get_fresh_address(onchainwallet) + if data.onchainwallet: + wallet = await get_watch_wallet(data.onchainwallet) + onchain = await get_fresh_address(data.onchainwallet) onchainaddress = onchain.address else: onchainaddress = None - if lnbitswallet: + if data.lnbitswallet: payment_hash, payment_request = await create_invoice( - wallet_id=lnbitswallet, amount=amount, memo=charge_id + wallet_id=data.lnbitswallet, amount=data.amount, memo=charge_id ) else: payment_hash = None @@ -63,17 +64,17 @@ async def create_charge( ( charge_id, user, - description, - onchainwallet, + data.description, + data.onchainwallet, onchainaddress, - lnbitswallet, + data.lnbitswallet, payment_request, payment_hash, - webhook, - completelink, - completelinktext, - time, - amount, + data.webhook, + data.completelink, + data.completelinktext, + data.time, + data.amount, 0, ), ) diff --git a/lnbits/extensions/satspay/models.py b/lnbits/extensions/satspay/models.py index a7bfa14f..8730809b 100644 --- a/lnbits/extensions/satspay/models.py +++ b/lnbits/extensions/satspay/models.py @@ -1,9 +1,19 @@ from sqlite3 import Row -from typing import NamedTuple +from fastapi.param_functions import Query +from pydantic import BaseModel import time +class CreateCharge(BaseModel): + onchainwallet: str = Query(None) + lnbitswallet: str = Query(None) + description: str = Query(...) + webhook: str = Query(None) + completelink: str = Query(None) + completelinktext: str = Query(None) + time: int = Query(..., ge=1) + amount: int = Query(..., ge=1) -class Charges(NamedTuple): +class Charges(BaseModel): id: str user: str description: str diff --git a/lnbits/extensions/satspay/templates/satspay/_api_docs.html b/lnbits/extensions/satspay/templates/satspay/_api_docs.html index 526af7f3..1a7ba5e4 100644 --- a/lnbits/extensions/satspay/templates/satspay/_api_docs.html +++ b/lnbits/extensions/satspay/templates/satspay/_api_docs.html @@ -90,7 +90,7 @@
curl -X GET {{ request.url_root }}api/v1/charge/<charge_id>
- -H "X-Api-Key: {{ g.user.wallets[0].inkey }}"
+ -H "X-Api-Key: {{ user.wallets[0].inkey }}"
@@ -113,7 +113,7 @@
curl -X GET {{ request.url_root }}api/v1/charges -H "X-Api-Key: {{
- g.user.wallets[0].inkey }}"
+ user.wallets[0].inkey }}"
@@ -139,7 +139,7 @@
curl -X DELETE {{ request.url_root
}}api/v1/charge/<charge_id> -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
+ user.wallets[0].adminkey }}"
@@ -162,7 +162,7 @@
curl -X GET {{ request.url_root
}}api/v1/charges/balance/<charge_id> -H "X-Api-Key: {{
- g.user.wallets[0].inkey }}"
+ user.wallets[0].inkey }}"
diff --git a/lnbits/extensions/satspay/views.py b/lnbits/extensions/satspay/views.py
index 2c99a925..c1be381c 100644
--- a/lnbits/extensions/satspay/views.py
+++ b/lnbits/extensions/satspay/views.py
@@ -1,22 +1,29 @@
-from quart import g, abort, render_template, jsonify
+from fastapi.param_functions import Depends
+from starlette.exceptions import HTTPException
+from starlette.responses import HTMLResponse
+from lnbits.core.models import User
+from lnbits.core.crud import get_wallet
+from lnbits.decorators import check_user_exists
from http import HTTPStatus
-from lnbits.decorators import check_user_exists, validate_uuids
+from fastapi.templating import Jinja2Templates
-from . import satspay_ext
+from . import satspay_ext, satspay_renderer
from .crud import get_charge
+templates = Jinja2Templates(directory="templates")
-@satspay_ext.route("/")
-@validate_uuids(["usr"], required=True)
-@check_user_exists()
-async def index():
- return await render_template("satspay/index.html", user=g.user)
+@satspay_ext.get("/", response_class=HTMLResponse)
+async def index(request: Request, user: User = Depends(check_user_exists)):
+ return satspay_renderer().TemplateResponse("satspay/index.html", {"request": request,"user": user.dict()})
-@satspay_ext.route("/