sailfork / t3code-github / .repos/ alchemy-effect/ packages/ stripe/ src/ CreateCheckoutSession.ts

import type {
  CheckoutSession,
  CreateCheckoutSessionError,
  CreateCheckoutSessionRequest,
} from "@distilled.cloud/stripe/stripe";
import * as Binding from "alchemy/Binding";
import type { RuntimeContext } from "alchemy/RuntimeContext";
import type * as Effect from "effect/Effect";

/**
 * Create a Stripe Checkout Session over HTTP. This is how a Worker starts
 * a purchase: create the session for a Price, then redirect the buyer to
 * `session.url`. Stripe hosts the payment page and sends
 * `checkout.session.completed` when it succeeds.
 *
 * Account-scoped — binds the API key onto the host, not a specific
 * resource.
 *
 * ### Starting a subscription checkout
 * **Example:** Redirect a buyer to hosted Checkout
 * ```typescript
 * const createCheckout = yield* Stripe.CreateCheckoutSession();
 *
 * // inside a Worker route
 * const session = yield* createCheckout({
 *   mode: "subscription",
 *   line_items: [{ price: price.id, quantity: 1 }],
 *   customer: customerId,
 *   success_url: `${origin}/welcome`,
 *   cancel_url: `${origin}/pricing`,
 * });
 * // redirect to session.url
 * ```
 *
 * @binding
 * @product Checkout
 */
export interface CreateCheckoutSession extends Binding.Service<
  CreateCheckoutSession,
  "Stripe.CreateCheckoutSession",
  () => Effect.Effect<
    (
      request: CreateCheckoutSessionRequest,
    ) => Effect.Effect<CheckoutSession, CreateCheckoutSessionError, RuntimeContext>
  >
> {}

export const CreateCheckoutSession = Binding.Service<CreateCheckoutSession>(
  "Stripe.CreateCheckoutSession",
);