4.8 KiB
4.8 KiB
Billing + Credits Architecture v1
1. System boundary
Separate billing orchestration from plugin runtime.
Payment System(e.g. Paymenter):- plans, checkout, invoices, payments
Management Cloud / Credits Service:- authoritative credit ledger
- entitlement state
- grant workflows
- treasury + funding automation
NuQloud plugin + broker:- checks credits/entitlements before write operations
- records consumption/refunds via management APIs
Nextcloud app should not integrate directly with payment webhooks.
2. Data model (minimum)
2.1 Ledger entries
ledger_entries
idinstance_idtype(grant|purchase|consume|refund|adjustment)credits_delta(signed integer)qort_amount(nullable decimal, reference only)ref(idempotency key)metadata(JSON)created_at
Constraint:
- unique index on
(instance_id, type, ref)
2.2 Balances
instance_balances
instance_id(PK)credits_balanceupdated_at
2.3 Entitlements
instance_entitlements
instance_id(PK)planfollower_countcapacity_gbstatus(active|past_due|suspended|cancelled)period_endupdated_at
2.4 Grants
intro_grants
instance_id(unique)status(pending|approved|rejected|completed)requested_atapproved_byapproved_atgrant_creditsref(request id / tx id)
3. API contract (minimum)
3.1 Billing webhook ingest
POST /billing/webhook/paymenter
- Verify signature/HMAC.
- Extract
instance_idfrom trusted metadata. - Write idempotent purchase ledger entry.
- Update entitlement state.
- Trigger async funding/reconciliation job.
3.2 Balance + entitlement read
GET /instances/{id}/billing/summary- Returns:
- balance
- plan
- capacity
- follower targets
- status
3.3 Consumption
POST /instances/{id}/credits/consume
- Input:
cost_creditsref(publish operation id)metadata
- Behavior:
- atomic check and decrement
- idempotent by
ref
3.4 Refund
POST /instances/{id}/credits/refund
- Input:
creditsref(failed operation id)
- Behavior:
- idempotent compensation entry
3.5 Intro grant request/approval
POST /instances/{id}/grants/intro/requestPOST /instances/{id}/grants/intro/approvePOST /instances/{id}/grants/intro/reject
Guardrails:
- One grant ever per instance.
- Approval role required.
- Fixed max grant size.
3.6 Product catalog discovery
POST /api/sovereign/v1/billing/catalog
- Accepts instance-scoped request plus optional
categoryId. - Returns canonical catalog contract
sc.billing.catalog.v1. - Includes normalized
subscriptions[]andaddons[]for UI rendering. - Enables adding/changing purchasable products without plugin updates.
4. Publish-time flow
- Plugin/broker receives write request.
- Determine cost in credits.
- Call consume endpoint with operation ref.
- Attempt write/publish.
- On success: done.
- On failure: call refund endpoint.
Requirements:
- operation ref must be globally unique.
- no duplicate charges on retries.
5. Funding and reconciliation
Optional treasury automation keeps publishing identity funded against outstanding credit liabilities.
Reconciliation job:
- compute required funding from credit liability + safety buffer.
- compare to instance publishing-wallet balance.
- queue transfer if below threshold.
- store tx hash and reconciliation ref.
6. Idempotency and concurrency
Non-negotiable:
- webhook ingest idempotent.
- consume/refund idempotent.
- atomic balance updates (transaction/row lock).
- no negative balance race conditions.
7. Failure modes + handling
- Duplicate webhook:
- ignored by unique
(instance_id,type,ref)
- ignored by unique
- Payment metadata missing instance_id:
- reject and quarantine event
- Publish fails after consume:
- refund by same operation ref
- Entitlement expired:
- block guaranteed-replication features; keep clear error state
8. Operational controls
- Admin override endpoint for adjustments with audit trail.
- Export ledger CSV/JSON for audit.
- Alerting for:
- low balance
- funding failures
- degraded replication below plan target
9. Security controls
- Verify payment webhooks cryptographically.
- Restrict webhook endpoint exposure.
- Separate service credentials for:
- payment ingest
- ledger mutation
- read-only dashboard queries
- Redact sensitive fields in logs.
10. Implementation phases
- Phase 1:
- ledger + balances + entitlements schema
- summary/read API
- Phase 2:
- consume/refund idempotent endpoints
- broker integration
- Phase 3:
- payment webhook ingest + entitlement update
- Phase 4:
- intro grant workflow
- reconciliation/funding jobs
- Phase 5:
- dashboards, exports, alerts