Skip to Content
Bryfo Docsv0.2.1

Commercial Ledger & Dual-Currency Economics

Integer minor-unit USD/CNY pricing, append-only dual-pool credit_ledger, fail-closed webhooks, and on-demand IMAP caching.

Exact Integer Accounting & Dual-Pool Credits

  • Zero Floating-Point Money: Currency minor units (USD cents, CNY fen) and AI credits use int64 across PostgreSQL, Go, and JSON, paired with shopspring/decimal ceiling rounding and min_charge_credits for token-to-credit conversion.
  • Append-Only credit_ledger & Concurrency Slots:
    • Historical ledger rows are never updated or deleted; refunds write compensating entries guarded by unique idempotency_key values (ai:<requestID>, order:<orderNo>, redeem:<code>:<userID>).
    • AI streams pre-check available >= min_charge_credits (402 INSUFFICIENT_CREDITS), acquire a bounded Valkey slot (lock:ai:slot:<uid>, max 2 concurrent streams), and settle inside a single SELECT ... FOR UPDATE transaction deducting expiring credit_buckets (expires_at ASC) before permanent paid_credits.

Fail-Closed Webhooks & Storage Economics

  • Signed Asynchronous Fulfillment Only: Browser return_url redirects never grant entitlements. Only verified webhooks (/api/v1/webhooks/stripe, /api/v1/payments/callback/{provider}) or Step-Up audited admin fulfillment transition pending → paid under FOR UPDATE, asserting exact amount, currency, and proration preconditions (409 PRORATION_STATE_CHANGED).
  • On-Demand Large Attachments (cloud): Attachments above STORAGE_ONDEMAND_THRESHOLD_BYTES (2 MiB) record metadata + imap_uid/imap_part_spec (stored_remotely = true) and stream from upstream IMAP on demand (<= 35 MiB) into a 7-day ephemeral cache.

On this page