Skip to content

Commit fcc94d5

Browse files
committed
Fix topup
1 parent 60735c8 commit fcc94d5

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎models.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class PaymentsPaymentRequest(BaseModel):
101101
payment_request: str | None = None
102102

103103

104+
class PublicTopupRequest(BaseModel):
105+
days: int = Field(gt=0)
106+
107+
104108

105109

106110
class PaymentsFilters(FilterModel):

‎services.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
CreateTunnelRequest,
3535
CreateTunnelResponse,
3636
ExtensionSettings,
37+
PublicTopupRequest,
3738
)
3839

3940
async def get_settings(user_id: str) -> ExtensionSettings:
@@ -559,18 +560,31 @@ def _remove_authorized_key(settings: ExtensionSettings, proxy: Proxies) -> None:
559560

560561
async def payment_request_for_payments(
561562
proxies_id: str,
562-
data: CreatePayments,
563+
data: CreatePayments | PublicTopupRequest,
563564
) -> PaymentsPaymentRequest:
564565
proxies = await get_proxies_by_id(proxies_id)
565566
if not proxies:
566567
raise ValueError("Invalid proxies ID.")
567568

568-
payments = await create_payments(proxies_id, data)
569+
settings = await get_settings(proxies.user_id)
569570

570-
amount = getattr(payments, "amount", 0)
571+
# derive amount from days when provided
572+
amount = getattr(data, "amount", 0)
573+
if hasattr(data, "days"):
574+
amount = getattr(data, "days", 0) * settings.price_per_day_sats
571575
if not amount or amount <= 0:
572576
raise ValueError("The amount must be greater than zero to create a payment request.")
573577

578+
payments = await create_payments(
579+
proxies_id,
580+
CreatePayments(
581+
tunnel_id=proxies_id,
582+
invoice_id="",
583+
amount=amount,
584+
paid=False,
585+
),
586+
)
587+
574588
proxies_name = getattr(proxies, "", "Unknown")
575589
payment: Payment = await create_invoice(
576590
wallet_id=getattr(proxies, "wallet", "no_wallet_id"),

‎views_api.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Payments,
3030
PaymentsFilters,
3131
PaymentsPaymentRequest,
32+
PublicTopupRequest,
3233
CreatePayments,
3334
CreateProxies,
3435
Proxies,
@@ -200,11 +201,11 @@ async def api_create_payments(
200201
)
201202
async def api_submit_public_payments(
202203
proxies_id: str,
203-
data: CreatePayments,
204+
data: PublicTopupRequest,
204205
) -> PaymentsPaymentRequest | None:
205206
"""
206207
PUBLIC: clients can use this to top up / extend an existing proxy.
207-
`data.amount` is in sats and is converted to an invoice.
208+
Uses days to compute the amount server-side.
208209
"""
209210
return await payment_request_for_payments(proxies_id, data)
210211

0 commit comments

Comments
 (0)