feat(credits): /research/web → 1 credit + auto top-up opt-out via Stripe customer metadata

Merged
SW
5mo ago
+698-3

Summary

Implements the api slice of recoupable/chat#1861, fulfilling the contract in docs#270 (merge order: docs#270 → this → chat).

1. Web search repriced 5 → 1 credit

  • lib/research/ensureWebResearchCredits.ts — dedicated 1-credit gate for POST /api/research/web (the shared RESEARCH_CREDIT_COST = 5 is untouched: Songstats-backed endpoints cost us ~$0.09/call and stay at 5; Perplexity web search costs a flat $0.005/request).
  • postResearchWebHandler deducts 1 on success.

2. Auto top-up opt-out (consent on the Stripe Customer)

  • lib/stripe/getAutoRechargeOptOut.ts — fresh customers.retrieve (never the ~60s-stale search-index copy), presence semantics: any non-empty auto_recharge_opt_out metadata value = opted out.
  • lib/stripe/setAutoRechargeOptOut.ts — opt-out stamps "true"; opt-in writes "" (Stripe deletes the key). Never writes "false".
  • autoRechargeOrFail checks consent before chargeCustomerOffSession; opted-out accounts get the existing 402 + checkoutUrl Checkout fallback — manual top-ups (and card saving via Checkout) keep working, but the saved card is never charged silently.
  • New GET/PATCH /api/accounts/{id}/auto-recharge (mirrors the payment-method route: same param validation, auth via validateAuthContext, flat { account_id, enabled } response). GET is read-only (no Customer provisioning); PATCH resolves via resolveStripeCustomerForAccount — the same metadata.accountId lookup the charge path uses — so the flag always lands on the Customer the gate reads.

TDD evidence

Every unit was RED before GREEN (new modules failed collection; modified suites failed on the new assertions — e.g. creditsToDeduct: 5 vs expected 1). Final: 87 test files / 385 tests pass across lib/stripe, lib/billing, lib/credits, lib/research; tsc --noEmit reports zero errors in touched files (remaining tsc noise is pre-existing on origin/main, verified by stash-compare); pnpm lint clean.

Preview verification results will be posted as a comment once the deployment is Ready.

🤖 Generated with Claude Code


Summary by cubic

Repriced web search to 1 credit and added a Stripe-based auto top-up opt-out so off-session charges only happen with consent. This makes web research cheaper and gives users a clear way to avoid silent charges.

  • New Features

    • POST /api/research/web now costs 1 credit via ensureWebResearchCredits; other research endpoints stay at 5.
    • Auto top-up opt-out stored on the Stripe Customer (auto_recharge_opt_out metadata). autoRechargeOrFail does a fresh customers.retrieve, never charges off-session when opted out, and falls back to 402 with a checkoutUrl.
    • GET/PATCH /api/accounts/{id}/auto-recharge to read/update the setting. Default is enabled; GET is read-only; PATCH provisions the Customer and uses presence-based metadata (delete on opt-in; never write "false").
  • Refactors

    • Consolidated PATCH validation: validateUpdateAutoRechargeBody now handles path/auth checks and safe JSON parsing; response contract unchanged.