Custom Store Backend
The Interface
interface Stripe402Store {
getClient(clientId: string): Promise<ClientRecord | null>
createClient(record: ClientRecord): Promise<void>
deductBalance(clientId: string, amount: number): Promise<number | null>
addBalance(clientId: string, amount: number): Promise<number>
recordTransaction?(transaction: TransactionRecord): Promise<void>
}Critical Requirement: Atomic deductBalance
deductBalance// DON'T DO THIS — not atomic
async deductBalance(clientId, amount) {
const client = await this.getClient(clientId)
if (client.balance >= amount) {
// Between this read and the write, another request could deduct too
await this.updateBalance(clientId, client.balance - amount)
return client.balance - amount
}
return null
}Example: In-Memory Store (for testing)
Example: MongoDB Store Skeleton
Checklist for Custom Stores
Last updated