| 1 | import type { |
| 2 | BillingPortalSession, |
| 3 | CreateBillingPortalSessionError, |
| 4 | CreateBillingPortalSessionRequest, |
| 5 | } from "@distilled.cloud/stripe/stripe"; |
| 6 | import type * as Effect from "effect/Effect"; |
| 7 | import * as Binding from "../Binding.ts"; |
| 8 | import type { RuntimeContext } from "../RuntimeContext.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Create a Stripe Billing Portal Session over HTTP. The portal is |
| 12 | * Stripe's hosted page where a customer updates their card, switches |
| 13 | * plans, downloads invoices, or cancels — so your app never has to build |
| 14 | * those screens. Create the session for the customer and redirect them to |
| 15 | * `session.url`. |
| 16 | * |
| 17 | * Account-scoped — binds the API key onto the host, not a specific |
| 18 | * resource. |
| 19 | * |
| 20 | * ### Letting a customer manage their subscription |
| 21 | * **Example:** Open the hosted portal |
| 22 | * ```typescript |
| 23 | * const createPortal = yield* Stripe.CreateBillingPortalSession(); |
| 24 | * |
| 25 | * // inside a Worker route |
| 26 | * const session = yield* createPortal({ |
| 27 | * customer: customerId, |
| 28 | * return_url: `${origin}/account`, |
| 29 | * }); |
| 30 | * // redirect to session.url |
| 31 | * ``` |
| 32 | * |
| 33 | * @binding |
| 34 | * @product Billing |
| 35 | */ |
| 36 | export interface CreateBillingPortalSession extends Binding.Service< |
| 37 | CreateBillingPortalSession, |
| 38 | "Stripe.CreateBillingPortalSession", |
| 39 | () => Effect.Effect< |
| 40 | ( |
| 41 | request: CreateBillingPortalSessionRequest, |
| 42 | ) => Effect.Effect< |
| 43 | BillingPortalSession, |
| 44 | CreateBillingPortalSessionError, |
| 45 | RuntimeContext |
| 46 | > |
| 47 | > |
| 48 | > {} |
| 49 | |
| 50 | export const CreateBillingPortalSession = |
| 51 | Binding.Service<CreateBillingPortalSession>( |
| 52 | "Stripe.CreateBillingPortalSession", |
| 53 | ); |