The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Stripe from "@alchemy.run/stripe"; |
| 2 | import { Account } from "@bank/domain/Account/Account.aggregate"; |
| 3 | import { AccountId } from "@bank/domain/Account/Account.id"; |
| 4 | import { RecordSettlement } from "@bank/domain/Account/RecordSettlement.command"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | |
| 7 | /** |
| 8 | * Card top-ups settle through Stripe. Every `payment_intent.succeeded` whose |
| 9 | * metadata names an account becomes a `RecordSettlement`, a command that |
| 10 | * never rejects (money that settled on a closed account is refunded by |
| 11 | * `SettlementRefund`). |
| 12 | * |
| 13 | * `consumeEvents` provisions the Stripe webhook endpoint pointed at this |
| 14 | * Worker, binds its signing secret, and verifies every delivery. The payment |
| 15 | * intent id is the command id, so a redelivered event is a no-op. |
| 16 | */ |
| 17 | export const StripeSettlements = Effect.gen(function* () { |
| 18 | const accounts = yield* Account; |
| 19 | yield* Stripe.consumeEvents( |
| 20 | "Settlements", |
| 21 | { events: [Stripe.PaymentIntentSucceeded] }, |
| 22 | Effect.fn(function* (event) { |
| 23 | const intent = event.object; |
| 24 | const accountId = intent.metadata?.accountId; |
| 25 | if (!accountId) return; |
| 26 | yield* accounts.send( |
| 27 | AccountId.make(accountId), |
| 28 | new RecordSettlement({ settlementId: intent.id, amount: intent.amount_received }), |
| 29 | { commandId: `settlement:${intent.id}` }, |
| 30 | ); |
| 31 | }), |
| 32 | ); |
| 33 | }); |