Refs #9 ## Summary - add a worker-side renewal invoice sweep that creates one invoice 72 hours before subscription expiry - expire elapsed pending invoices automatically and email users when an automatic renewal invoice is created - stop auto-recreating invoices for the same paid cycle once any invoice already exists for that cycle - document the current renewal-invoice and pending-invoice expiry behavior ## Testing - built `infra/docker/web.Dockerfile` - ran `pnpm --filter @nproxy/db test` inside the built container - verified `@nproxy/db build` and `@nproxy/web build` during the image build - built `infra/docker/worker.Dockerfile` Co-authored-by: sirily <sirily@git.shararam.party> Reviewed-on: #20
11 KiB
Payment System
Purpose
Describe how billing and payment processing currently work in nproxy.
This document is about the implemented system, not the desired future state.
Scope
The current payment system covers:
- default subscription plan bootstrap
- user subscription creation at registration time
- invoice creation through a provider adapter
- automatic renewal-invoice creation
72 hoursbeforecurrentPeriodEnd - one renewal-invoice creation attempt per paid cycle unless the user explicitly creates a new one manually
- worker-side polling reconciliation for
pendinginvoices - manual admin activation after an operator verifies that the provider reported a final successful payment status
- automatic expiry of elapsed subscription periods during account and generation access checks
- automatic expiry of elapsed pending invoices
- automatic provider-driven
expiredandcanceledtransitions for pending invoices - quota-cycle reset on successful activation
The current payment system does not yet cover:
- provider webhooks
- recurring billing
Main records
SubscriptionPlan
- one default active plan is bootstrapped by
packages/db/src/bootstrap.ts - current default seed:
code:monthlydisplayName:MonthlymonthlyRequestLimit:100monthlyPriceUsd:9.99billingCurrency:USDT
Subscription
- created for a new user during registration if the default plan exists
- starts in
pending_activation - becomes
activeonly after successful invoice activation - stores:
activatedAtcurrentPeriodStartcurrentPeriodEndrenewsManually
PaymentInvoice
- stores one provider-facing payment attempt for a subscription
pendingmeans the invoice exists, but the payment is not yet considered final or acceptedpaidmeans the payment is considered final and safe to activate against- important fields:
- local
id subscriptionIdproviderproviderInvoiceIdstatuscurrencyamountCryptoamountUsdpaymentAddressexpiresAtpaidAt
- local
UsageLedgerEntry
- records quota-affecting events
- on successful payment activation the system writes
cycle_reset - successful generations later write
generation_success
AdminAuditLog
- records state-changing admin actions
- the current admin
mark-paidflow writes:invoice_mark_paidinvoice_mark_paid_replayed
Provider boundary
Payment-provider code stays behind packages/providers/src/payments.ts.
Current provider contract:
createInvoice(input) -> providerInvoiceId, paymentAddress, amountCrypto, amountUsd, currency, expiresAtgetInvoiceStatus(providerInvoiceId) -> pending|paid|expired|canceled + paidAt? + expiresAt?
Current runtime note:
- the provider adapter is still a placeholder adapter
- worker polling is implemented, but provider-specific HTTP/status mapping is still placeholder logic
- provider callbacks and webhook signature verification are not implemented yet
- the rest of the payment flow is intentionally provider-agnostic
Registration flow
- User registers through
POST /api/auth/register. packages/db/src/auth-store.tscreates the user and session.- If the default plan exists, the store also creates a
Subscriptioninpending_activation. - At this point the user has an account and a plan assignment, but not an active paid cycle.
Invoice creation flow
- Authenticated user calls
POST /api/billing/invoices. packages/db/src/billing-store.tsloads the latest subscription for that user.- If there is already a non-expired
pendinginvoice for the same subscription, it is reused. - Otherwise the app calls the payment-provider adapter
createInvoice. - The returned provider invoice data is persisted as a new local
PaymentInvoiceinpending. - The API returns invoice details, including provider invoice id, amount, address, and expiry time.
Automatic renewal invoice flow
- The worker scans
activemanual-renewal subscriptions. - If
currentPeriodEndis within the next72 hours, the worker checks whether the current paid cycle already has an invoice. - If the current cycle has no invoice yet, the worker creates one renewal invoice through the payment-provider adapter.
- The worker sends the invoice details to the user by email.
- If any invoice already exists for the current cycle, the worker does not auto-create another one.
Current rule:
- after the first invoice exists for the current paid cycle, automatic re-creation stops for that cycle
- if that invoice later expires or is canceled, the next invoice is created only when the user explicitly goes to billing and creates one
Payment status semantics
pendingdoes not count as paid.pendingdoes not activate the subscription.pendingdoes not reset quota.- The system must treat an invoice as
paidonly after the payment provider reports a final successful status, meaning the funds are accepted strongly enough for access activation. - Worker reconciliation now polls provider status for stored
pendinginvoices. - If the provider reports final
paid, the worker activates access from providerpaidAtwhen that timestamp is available. - If the provider reports final
paidafter a local fallback expiry, providerpaidstill wins and access is activated. - If the provider reports final
expiredorcanceled, the worker finalizes the local invoice to the same terminal status.
Worker reconciliation flow
- The worker loads a batch of
pendinginvoices that haveproviderInvoiceId. - For each invoice, it calls
paymentProviderAdapter.getInvoiceStatus(providerInvoiceId). - If the provider still reports
pending, the worker leaves the invoice unchanged. - If the provider reports
paid, the worker calls the same idempotent activation path as adminmark-paid. - If the provider reports
expiredorcanceled, the worker atomically moves the local invoice frompendingto that terminal state. - After provider polling, the worker also expires any still-pending invoices whose local
expiresAthas elapsed. - If a manual admin action or another worker already finalized the invoice, reconciliation degrades to replay/no-op behavior instead of duplicating side effects.
Implementation note:
- invoice creation paths take a per-subscription database lock so manual invoice creation and worker renewal creation do not create duplicate invoices for the same subscription at the same time.
Invoice listing flow
GET /api/billing/invoicesreturns the user's invoices ordered by newest first.- This is a read-only view over persisted
PaymentInvoicerows. - The worker also marks
pendinginvoicesexpiredwhenexpiresAthas passed.
Current activation flow
The implemented activation paths are:
- automatic worker reconciliation after provider final status
- manual admin override after an operator verifies provider final status outside the app
Shared activation behavior:
markInvoicePaidruns inside one database transaction.- If the invoice is
pending, the store:- updates the invoice to
paid - sets
paidAt - updates the related subscription to
active - sets
activatedAtif it was not already set - sets
currentPeriodStart = paidAt - sets
currentPeriodEnd = paidAt + 30 days - clears
canceledAt - writes a
UsageLedgerEntrywithentryType = cycle_reset - writes an
AdminAuditLogentryinvoice_mark_paidwhen actor metadata is present
- updates the invoice to
Manual admin path:
- An authenticated admin calls
POST /api/admin/invoices/:id/mark-paid. - The web app resolves the admin session and passes actor metadata into the billing store.
- This endpoint is intended to be used only after the operator has already verified that the provider reached a final successful payment state.
- The API returns the updated invoice.
Important constraint:
mark-paidis not evidence by itself that apendinginvoice became payable.- It is only the current manual mechanism for recording that the provider has already given final confirmation outside the app.
Idempotency and transition rules
markInvoicePaid is replay-safe.
Allowed cases
pending -> paidpaid -> paidas a no-op replay
Replay behavior
If the invoice is already paid:
- the subscription is not mutated again
- no extra
cycle_resetentry is created - an audit event
invoice_mark_paid_replayedis written
Rejected cases
If the invoice is already terminal:
expiredcanceled
the store rejects the request with invoice_transition_not_allowed.
If the invoice does not exist, the store returns invoice_not_found.
How quota ties to payment
- A user can create generation requests only with an active subscription.
- Approximate quota shown to the user is derived from
generation_successentries since the current billing cycle start. - A successful payment activation starts a new cycle by writing
cycle_resetand moving the subscription window forward. - Failed generations do not consume quota.
- When a subscription period has elapsed, user-facing quota is no longer shown as an active-cycle quota.
Subscription period enforcement
currentPeriodEndis the hard end of paid access.- At or after
currentPeriodEnd, the runtime no longer treats the subscription as active. - During generation access checks, an elapsed
activesubscription is transitioned toexpiredbefore access is denied. - During account and billing reads, an elapsed
activeorpast_duesubscription is normalized toexpiredso the stored lifecycle is reflected consistently. - There is no grace period after
currentPeriodEnd.
HTTP surface
POST /api/billing/invoices- create or reuse the current pending invoice for the authenticated user
GET /api/billing/invoices- list the authenticated user's invoices
POST /api/admin/invoices/:id/mark-paid- admin-only manual payment activation path
Error behavior
Current payment-specific errors surfaced by the web app:
invoice_not_found->404invoice_transition_not_allowed->409
Current limitations
- The provider adapter still uses placeholder status lookups; real provider HTTP integration is not implemented yet.
- No provider callback or webhook signature verification path exists yet.
- Manual admin
mark-paidstill exists as an override, so operator judgment is still part of the system for exceptional cases. - The worker polls invoice status in batches; there is no provider push path yet.
Required future direction
- Replace placeholder provider status lookups with real provider integration.
- Add provider callbacks or webhook ingestion on top of polling where the chosen provider supports it.
- Reduce or remove the need for operator judgment in the normal payment-success path.
Code references
packages/db/src/bootstrap.tspackages/db/src/auth-store.tspackages/db/src/billing-store.tspackages/db/src/account-store.tspackages/providers/src/payments.tsapps/web/src/main.ts