| 212 | }); |
| 213 | |
| 214 | const paymentMethod = (customerId: string) => |
| 215 | Effect.gen(function* () { |
| 216 | const response = yield* Effect.promise(() => |
| 217 | fetch(`${autumnUrl}/v1/customers.get_or_create`, { |
| 218 | method: "POST", |
| 219 | headers: { "content-type": "application/json" }, |
| 220 | body: JSON.stringify({ customer_id: customerId, expand: ["payment_method"] }), |
| 221 | }), |
| 222 | ); |
| 223 | if (!response.ok) { |
| 224 | return yield* Effect.fail( |
| 225 | `autumn customers.get_or_create responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 226 | ); |
| 227 | } |
| 228 | const body = (yield* Effect.promise(() => response.json())) as { |
| 229 | readonly payment_method?: { |
| 230 | readonly card?: { |
| 231 | readonly brand: string; |
| 232 | readonly last4: string; |
| 233 | readonly exp_month: number; |
| 234 | readonly exp_year: number; |
| 235 | }; |
| 236 | } | null; |
| 237 | }; |
| 238 | const card = body.payment_method?.card; |
| 239 | return card |
| 240 | ? { brand: card.brand, last4: card.last4, expMonth: card.exp_month, expYear: card.exp_year } |
| 241 | : null; |
| 242 | }); |
| 243 | |
| 244 | // Track exactly the plan's included allotment in one event, driving |
| 245 | // `remaining` (included - usage) to zero so the next check reports blocked. |