# Marketplace credits — deduction (follow-up)

**Context:** Issue #306 implemented prepaid credits: balance, purchase ($10 / $100 / $1000), and Stripe success flow to add credits to `User.marketplaceCreditsCents`. Deduction on marketplace (remote) usage is **not** implemented in the MVP.

## Implemented (MVP)

- **Schema:** `User.marketplaceCreditsCents` (Int, default 0).
- **API:** GET `/api/dashboard/marketplace/credits` (balance), POST `/api/marketplace/credits/purchase` (Stripe Checkout for 1000 | 10000 | 100000 cents).
- **Stripe:** Checkout one-time payment; webhook `checkout.session.completed` (mode=payment, metadata.type=marketplace_credits) increments user balance.
- **Dashboard:** Devices page shows "Credits balance: $X.XX" and "Add credits" ($10 / $100 / $1000).

## Follow-up: deducting credits on marketplace use

When a buyer uses a **remote** (marketplace) device, credits must be decremented. The Python proxy does not have direct access to Prisma. Options:

1. **Proxy → Next.js internal API (recommended when feasible):** Python proxy calls Next.js `POST /api/internal/marketplace/deduct` with `api_key_id` (or user id) and `amount_cents`. Next.js validates and decrements `User.marketplaceCreditsCents`. Requires internal auth (shared secret or network restriction).
2. **Batch from BillingStore:** Record usage in a store (e.g. BillingStore events); a batch job or webhook consumer decrements user balance from aggregated usage.
3. **Proxy-side ledger:** Proxy maintains a local ledger and syncs to Next.js periodically; more complex, eventual consistency.

**MVP:** Only balance and purchase are implemented. Implement option (a) when the proxy can call back to Next.js; document the internal API contract and secure it (e.g. `INTERNAL_API_KEY`, or allow only from proxy origin).
