| 264 | }); |
| 265 | |
| 266 | const attachPlan = (customerId: string, planId: string) => |
| 267 | Effect.gen(function* () { |
| 268 | const response = yield* Effect.promise(() => |
| 269 | fetch(`${autumnUrl}/v1/billing.attach`, { |
| 270 | method: "POST", |
| 271 | headers: { "content-type": "application/json" }, |
| 272 | body: JSON.stringify({ customer_id: customerId, plan_id: planId }), |
| 273 | }), |
| 274 | ); |
| 275 | if (!response.ok) { |
| 276 | return yield* Effect.fail( |
| 277 | `autumn billing.attach responded ${response.status}: ${yield* Effect.promise(() => response.text())}`, |
| 278 | ); |
| 279 | } |
| 280 | // A plan that needs payment answers with a checkout URL instead of |
| 281 | // activating; that silently leaves the org on Free, and a scenario that |
| 282 | // asked for a paid org would then assert against the wrong plan. |
| 283 | const body = (yield* Effect.promise(() => response.json())) as { |
| 284 | readonly payment_url?: string | null; |
| 285 | }; |
| 286 | if (body.payment_url) { |
| 287 | return yield* Effect.fail( |
| 288 | `autumn billing.attach returned a checkout URL for '${planId}': it requires payment, so it cannot be attached directly`, |
| 289 | ); |
| 290 | } |
| 291 | }); |
| 292 | |
| 293 | const memberSeats = (customerId: string) => |
| 294 | Effect.gen(function* () { |