From f4d8fb89fc85a7917a22c29fa94bcc9536ea940a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Fri, 16 Aug 2024 08:02:43 +0200 Subject: [PATCH] feat: add fiat amount to create charge endpoint + frontend --- crud.py | 1 + migrations.py | 11 +++++++++++ models.py | 6 +++++- templates/satspay/index.html | 27 ++++++++++++++++++++++++++- views_api.py | 11 +++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crud.py b/crud.py index 084a45c..e584d5d 100644 --- a/crud.py +++ b/crud.py @@ -32,6 +32,7 @@ async def create_charge( {"mempool_endpoint": config.mempool_endpoint, "network": config.network} ) + assert data.amount, "Amount is required" if data.lnbitswallet: payment_hash, payment_request = await create_invoice( wallet_id=data.lnbitswallet, diff --git a/migrations.py b/migrations.py index 234b015..14e4d03 100644 --- a/migrations.py +++ b/migrations.py @@ -145,3 +145,14 @@ async def m009_settings(db): ) except OperationalError: pass + + +async def m009_add_fiat(db): + """ + Add 'currency' and 'currency_amount' columns for storing the fiat amount + """ + try: + await db.execute("ALTER TABLE satspay.charges ADD COLUMN currency TEXT") + await db.execute("ALTER TABLE satspay.charges ADD COLUMN currency_amount FLOAT") + except OperationalError: + pass diff --git a/models.py b/models.py index 975f317..469b11e 100644 --- a/models.py +++ b/models.py @@ -25,10 +25,12 @@ class CreateCharge(BaseModel): completelink: str = Query(None) completelinktext: str = Query("Back to Merchant") time: int = Query(..., ge=1) - amount: int = Query(..., ge=1) + amount: Optional[int] = Query(None, ge=1) zeroconf: bool = Query(False) extra: str = DEFAULT_MEMPOOL_CONFIG custom_css: Optional[str] = Query(None) + currency: str = Query(None) + currency_amount: Optional[float] = Query(None) class ChargeConfig(BaseModel): @@ -60,6 +62,8 @@ class Charge(BaseModel): pending: Optional[int] = 0 timestamp: int last_accessed_at: Optional[int] = 0 + currency: Optional[str] = None + currency_amount: Optional[float] = None @property def paid(self): diff --git a/templates/satspay/index.html b/templates/satspay/index.html index ff58992..12733d2 100644 --- a/templates/satspay/index.html +++ b/templates/satspay/index.html @@ -382,12 +382,37 @@
label="*Description" > +
+
+ +
+
+ + + color="primary" :disable=" formDialogCharge.data.time == null || - formDialogCharge.data.amount == null" + (formDialogCharge.data.amount == null && formDialogCharge.data.currency_amount == null)" type="submit" >Create Charge diff --git a/views_api.py b/views_api.py index f0dbcb0..1143617 100644 --- a/views_api.py +++ b/views_api.py @@ -7,6 +7,7 @@ require_admin_key, require_invoice_key, ) +from lnbits.utils.exchange_rates import get_fiat_rate_satoshis from loguru import logger from .crud import ( @@ -40,6 +41,16 @@ async def api_charge_create( except Exception as exc: logger.error(f"Error fetching onchain config: {exc}") + if not data.amount and not data.currency_amount: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="either amount or currency_amount are required.", + ) + + if data.currency and data.currency_amount: + rate = await get_fiat_rate_satoshis(data.currency) + data.amount = round(rate * data.currency_amount) + return await create_charge( user=wallet.wallet.user, data=data,