import Stripe from 'stripe'; import { ShopProduct, ShopCart } from '../types.js'; import { a as CartStore } from '../store-B6Oh5IPN.js'; /** * F136 Phase 1 — Stripe Checkout Session creator. * * Translates a CMS cart into a Stripe Checkout Session and returns the * redirect URL. The actual order document is created later by the * webhook handler when Stripe confirms payment — never trust the * client to tell us "the order is paid". * * Two operating modes: * 1. Single-merchant: the site owns the Stripe account. Just pass * `secretKey` (or set STRIPE_SECRET_KEY env). Money lands on that * account directly. * 2. Marketplace (Stripe Connect): the platform (e.g. webhouse.app) * owns the Stripe account, each merchant has a connected * `acct_xxx`. Pass `connect: { destinationAccountId, applicationFeeAmount }` * and money is transferred to the merchant minus the platform fee. * Patterns lifted from the proven sanneandersen-site implementation * (verified with real Stripe demo transactions, May 2026). * * Strategy: prefer existing `stripePriceIds` on each product (created * by syncProductToStripe). If a product hasn't been synced yet, fall * back to `price_data` inline — works but loses Stripe's reporting per * Price. */ type CheckoutSessionCreateParams = NonNullable[0]>; type CheckoutLocale = NonNullable; interface ConnectOptions { /** `acct_xxx` of the merchant's connected account. */ destinationAccountId: string; /** * Platform fee in minor units (øre/cents). Either supply this OR * `applicationFeePercent` — not both. Wins if both are set. */ applicationFeeAmount?: number; /** * Platform fee as a percentage of the cart total (0–100). Computed * server-side at checkout time. Convenient for sites that price by * percentage rather than fixed amount. */ applicationFeePercent?: number; } interface CreateCheckoutOptions { /** Stripe secret key (multi-tenant). Falls back to env. */ secretKey?: string; /** Where Stripe redirects on success — `{CHECKOUT_SESSION_ID}` placeholder is allowed. */ successUrl: string; /** Where Stripe redirects on cancel. */ cancelUrl: string; /** Resolve a product id → ShopProduct so we can pull Stripe price ids. */ loadProduct(id: string): Promise; /** Force Stripe to collect a shipping address (default: true if any cart line is physical). */ collectShippingAddress?: boolean; /** Stripe shipping rate ids to offer at checkout. */ shippingRateIds?: string[]; /** Locale shown in Stripe Checkout UI (e.g. 'da', 'en'). Default: cart.locale. */ locale?: CheckoutLocale; /** Allowed countries for shipping. ISO 3166-1 alpha-2. */ allowedShippingCountries?: string[]; /** Extra metadata to attach to the Stripe Session. */ metadata?: Record; /** Stripe Connect — turn this on for marketplace mode. */ connect?: ConnectOptions; /** * Force Stripe to create+attach a Customer object even for guest * checkouts. Without this the Customer column in the Stripe dashboard * is blank — which makes the merchant's life much harder. * Default: true. */ alwaysCreateCustomer?: boolean; /** * Short reference shown in the Stripe dashboard's payments table. * The webhook handler uses this to find the matching CMS document * by order/cart id without parsing metadata. Default: cart.id. */ clientReferenceId?: string; /** * Human-friendly description shown in Stripe dashboard's payments * table — what is this charge actually for? Default: short summary * built from the first line item title + total. */ description?: string; /** * Send Stripe-generated receipt email. Default: cart.email if present. * Set to false to disable (e.g. if the site sends its own receipts). */ receiptEmail?: string | false; /** * Discriminator written to `session.metadata.kind`. Lets the webhook * handler dispatch one event type to multiple flows (e.g. 'order', * 'subscription', 'donation'). Default: 'order'. */ kind?: string; } interface CheckoutResult { sessionId: string; url: string; } declare function createCheckoutSession(cart: ShopCart, opts: CreateCheckoutOptions): Promise; /** * Compute the platform fee in minor units. Exported so consumers (and * tests) can reason about the math without going through Stripe. */ declare function computeApplicationFee(cart: ShopCart, connect: ConnectOptions): number; interface CheckoutHandlerOptions extends Omit { store: CartStore; /** Build success_url from cart id — typically `${siteOrigin}/checkout/success?session_id={CHECKOUT_SESSION_ID}`. */ successUrl: string | ((cartId: string) => string); cancelUrl: string | ((cartId: string) => string); } declare function createCheckoutHandler(opts: CheckoutHandlerOptions): (req: Request) => Promise; export { type CheckoutHandlerOptions, type CheckoutResult, type ConnectOptions, type CreateCheckoutOptions, computeApplicationFee, createCheckoutHandler, createCheckoutSession };