Skip to content

Commit

Permalink
feat: add fiat amount to create charge endpoint
Browse files Browse the repository at this point in the history
+ frontend
  • Loading branch information
dni committed Aug 16, 2024
1 parent 03ced2c commit f4d8fb8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
27 changes: 26 additions & 1 deletion templates/satspay/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,37 @@ <h6 class="text-subtitle1 q-my-none">
label="*Description"
></q-input>

<div class="row q-col-gutter-sm">
<div class="col">
<q-select
dense
:options="currencies"
v-model="formDialogCharge.data.currency"
:display-value="formDialogCharge.data.currency || 'satoshis'"
label="Currency"
:hint="'Converted to satoshis at each payment. ' + (formDialogCharge.data.currency && fiatRates[formDialogCharge.data.currency] ? `Currently 1 ${formDialogCharge.data.currency} = ${fiatRates[formDialogCharge.data.currency]} sat` : '')"
@input="updateFiatRate"
/>
</div>
</div>

<q-input
filled
dense
v-model.trim="formDialogCharge.data.amount"
type="number"
label="*Amount (sats)"
v-if="formDialogCharge.data.currency === 'satoshis'"
></q-input>

<q-input
filled
dense
v-model.trim="formDialogCharge.data.currency_amount"
type="number"
:label="'*Amount (' + formDialogCharge.data.currency + ')'"
v-if="formDialogCharge.data.currency !== 'satoshis'"
step="0.01"
></q-input>

<q-input
Expand Down Expand Up @@ -513,7 +538,7 @@ <h6 class="text-subtitle1 q-my-none">
color="primary"
:disable="
formDialogCharge.data.time == null ||
formDialogCharge.data.amount == null"
(formDialogCharge.data.amount == null && formDialogCharge.data.currency_amount == null)"
type="submit"
>Create Charge</q-btn
>
Expand Down
11 changes: 11 additions & 0 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f4d8fb8

Please sign in to comment.