import type Stripe from "stripe" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { stripeAction, fromStripe, getStripeApi, stripeActionSchemas, stripeInputSchema, stripeMutationArguments, stripeMutationReplaySafety, stripeMutationSchema, stripeObjectSchema, stripeSchema, toStripeParams, type StripeMutationInput, type StripeInputValue, } from "../lib" /** Stripe Checkout Session retrieval input with a public resource ID. */ type CheckoutSessionIdInput = StripeInputValue & { checkoutSessionId: string } /** Stripe Checkout Session mutation input with a public resource ID. */ type CheckoutSessionMutationInput = StripeMutationInput & { checkoutSessionId: string } /** Stripe Payment Link retrieval input with a public resource ID. */ type PaymentLinkIdInput = StripeInputValue & { paymentLinkId: string } /** Stripe Payment Link mutation input with a public resource ID. */ type PaymentLinkMutationInput = StripeMutationInput & { paymentLinkId: string } /** * Named input keeps Stripe Checkout namespace types portable during declaration * emit. */ type CheckoutSessionCreateInput = StripeMutationInput /** * Named input keeps Stripe Checkout namespace types portable during declaration * emit. */ type CheckoutSessionRetrieveInput = CheckoutSessionIdInput /** * Named input keeps Stripe Checkout namespace types portable during declaration * emit. */ type CheckoutSessionListInput = StripeInputValue /** * Named input keeps Stripe Checkout namespace types portable during declaration * emit. */ type CheckoutSessionExpireInput = CheckoutSessionMutationInput type CheckoutSessionOutput = Stripe.Checkout.Session /** * Named output keeps Stripe Checkout namespace types portable during * declaration emit. */ type CheckoutSessionListOutput = Stripe.ApiList /** * Named input keeps Stripe Billing Portal namespace types portable during * declaration emit. */ type BillingPortalSessionCreateInput = StripeMutationInput /** * Named output keeps Stripe Billing Portal namespace types portable during * declaration emit. */ type BillingPortalSessionOutput = Stripe.BillingPortal.Session /** Creates a Stripe-hosted, embedded, Elements, or form Checkout Session. */ export const createStripeCheckoutSession = stripeAction< CheckoutSessionCreateInput, CheckoutSessionOutput >( defineAction("Create Stripe Checkout Session") .describe( "Creates a Checkout Session for payments, setup, or subscriptions.", ) .account("stripe") .input( stripeMutationSchema( stripeActionSchemas .mutation({ lineItems: z .array( z .object({ price: z.string().startsWith("price_").optional(), priceData: z.object({}).loose().optional(), quantity: z.int().positive().optional(), }) .loose() .refine( ({ price, priceData }) => Boolean(price) !== Boolean(priceData), { message: "Provide exactly one of price or priceData.", }, ), ) .min(1) .max(100) .optional(), mode: z.enum(["payment", "setup", "subscription"]), successUrl: z.url().optional(), uiMode: z .enum(["elements", "embedded_page", "form", "hosted_page"]) .optional(), }) .superRefine(({ lineItems, mode, successUrl, uiMode }, context) => { if (mode !== "setup" && lineItems === undefined) { context.addIssue({ code: "custom", message: "Payment and subscription sessions require lineItems.", path: ["lineItems"], }) } if ( uiMode !== undefined && uiMode !== "hosted_page" && successUrl ) { context.addIssue({ code: "custom", message: "Only hosted-page Checkout Sessions can use successUrl.", path: ["successUrl"], }) } }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).checkout.sessions.create(params, options), ) }), ) /** Retrieves a Checkout Session and optional expanded relationships. */ export const getStripeCheckoutSession = stripeAction< CheckoutSessionRetrieveInput, CheckoutSessionOutput >( defineAction("Get Stripe Checkout Session") .describe("Retrieves one Stripe Checkout Session by ID.") .account("stripe") .input( stripeSchema< CheckoutSessionIdInput >(stripeActionSchemas.id("checkoutSessionId", "cs_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { checkoutSessionId, ...params } = input return fromStripe( await getStripeApi(account).checkout.sessions.retrieve( checkoutSessionId, toStripeParams(params), ), ) }), ) /** Lists Checkout Sessions with customer, payment, and creation filters. */ export const listStripeCheckoutSessions = stripeAction< CheckoutSessionListInput, CheckoutSessionListOutput >( defineAction("List Stripe Checkout Sessions") .describe("Lists Checkout Sessions with Stripe cursor pagination.") .account("stripe") .input( stripeInputSchema( stripeActionSchemas.list, ), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).checkout.sessions.list( toStripeParams(input), ), ), ), ) /** Expires an open Checkout Session immediately. */ export const expireStripeCheckoutSession = stripeAction< CheckoutSessionExpireInput, CheckoutSessionOutput >( defineAction("Expire Stripe Checkout Session") .describe("Expires an open Checkout Session so it can no longer be used.") .account("stripe") .input( stripeSchema< CheckoutSessionMutationInput >(stripeActionSchemas.id("checkoutSessionId", "cs_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { checkoutSessionId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).checkout.sessions.expire( checkoutSessionId, params, options, ), ) }), ) /** Creates a reusable Stripe Payment Link. */ export const createStripePaymentLink = stripeAction< StripeMutationInput, Stripe.PaymentLink >( defineAction("Create Stripe Payment Link") .describe("Creates a reusable hosted payment URL for products and prices.") .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ lineItems: z .array( z .object({ price: z.string().startsWith("price_"), quantity: z.int().positive().optional(), }) .loose(), ) .min(1) .max(20), }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).paymentLinks.create(params, options), ) }), ) /** Retrieves a Stripe Payment Link by ID. */ export const getStripePaymentLink = stripeAction< PaymentLinkIdInput, Stripe.PaymentLink >( defineAction("Get Stripe Payment Link") .describe("Retrieves one active or inactive Stripe Payment Link.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("paymentLinkId", "plink_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { paymentLinkId, ...params } = input return fromStripe( await getStripeApi(account).paymentLinks.retrieve( paymentLinkId, toStripeParams(params), ), ) }), ) /** Lists Stripe Payment Links with activity and pagination filters. */ export const listStripePaymentLinks = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe Payment Links") .describe("Lists reusable Stripe Payment Links with cursor pagination.") .account("stripe") .input( stripeInputSchema(stripeActionSchemas.list), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).paymentLinks.list(toStripeParams(input)), ), ), ) /** Updates settings or activity state for a Stripe Payment Link. */ export const updateStripePaymentLink = stripeAction< PaymentLinkMutationInput, Stripe.PaymentLink >( defineAction("Update Stripe Payment Link") .describe("Updates purchase settings, metadata, or active state.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("paymentLinkId", "plink_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentLinkId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentLinks.update( paymentLinkId, params, options, ), ) }), ) /** Creates a Stripe-hosted customer billing portal session. */ export const createStripeBillingPortalSession = stripeAction< BillingPortalSessionCreateInput, BillingPortalSessionOutput >( defineAction("Create Stripe billing portal session") .describe( "Creates a short-lived portal URL for customer billing management.", ) .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ customer: z.string().startsWith("cus_"), returnUrl: z.url().optional(), }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).billingPortal.sessions.create( params, options, ), ) }), )