HappyWan
API & Billing

Idempotent Job Ledger & Automatic Refund

Quick Spec Callout · 100% automatic credit refund on upstream failure or moderation halt

An atomic accounting architecture preventing duplicate charges on retries and automatically reversing reserved credits whenever an async render fails or gets filtered.

Engineering Explanation

Long-horizon generative video renders—especially 30-to-45-second 1080p, 2K, and 4K jobs—run asynchronously over tens of seconds to several minutes, creating costly financial edge cases for automated production pipelines. Without explicit `Idempotency-Key` headers, a transient mobile drop or gateway timeout during `POST /api/jobs` can trigger an automated worker retry that accidentally double-books an expensive 4K render. Equally frustrating, when an upstream GPU node times out or an automated content safety filter blocks final asset delivery, opaque API providers frequently force engineers to file manual billing tickets to recover lost balance. HappyWan Video prevents both failures through an append-only SQLite transactional credit ledger (`BEGIN IMMEDIATE`) synchronized with deterministic job states (`queued` -> `processing` -> `completed` | `failed` | `refunded`). Per-second credits are calculated upfront from engine, resolution, and duration before enqueueing; whenever status polling observes an upstream error, timeout, cancellation, or moderated empty asset, the ledger atomically appends a compensating positive entry restoring 100% of reserved credits alongside the original job reference ID. Paired with revocable `hwk_live_*` Bearer keys and HMAC-SHA512 verified NOWPayments checkout webhooks, agencies gain clean per-job financial auditability and zero stranded balance risk at scale.

Idempotent Ledger — Glossary | HappyWan