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 StripeInputValue, type StripeMutationInput, } from "../lib" /** Stripe SetupIntent retrieval input with a public resource ID. */ type SetupIntentIdInput = StripeInputValue & { setupIntentId: string } type SetupIntentMutationInput = StripeMutationInput & { setupIntentId: string } /** Stripe PaymentMethod retrieval input with a public resource ID. */ type PaymentMethodIdInput = StripeInputValue & { paymentMethodId: string } type PaymentMethodMutationInput = StripeMutationInput & { paymentMethodId: string } /** Creates a Stripe SetupIntent for a future payment method. */ export const createStripeSetupIntent = stripeAction< StripeMutationInput, Stripe.SetupIntent >( defineAction("Create Stripe SetupIntent") .describe( "Creates a SetupIntent to save and authenticate a payment method.", ) .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ customer: z.string().startsWith("cus_").optional(), paymentMethod: z.string().min(1).optional(), paymentMethodTypes: z.array(z.string().min(1)).min(1).optional(), returnUrl: z.url().optional(), usage: z.enum(["off_session", "on_session"]).optional(), }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).setupIntents.create(params, options), ) }), ) /** Retrieves a Stripe SetupIntent by ID. */ export const getStripeSetupIntent = stripeAction< SetupIntentIdInput, Stripe.SetupIntent >( defineAction("Get Stripe SetupIntent") .describe("Retrieves one SetupIntent with optional expansions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("setupIntentId", "seti_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { setupIntentId, ...params } = input return fromStripe( await getStripeApi(account).setupIntents.retrieve( setupIntentId, toStripeParams(params), ), ) }), ) /** Lists Stripe SetupIntents with customer and status filters. */ export const listStripeSetupIntents = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe SetupIntents") .describe("Lists SetupIntents with Stripe cursor pagination and filters.") .account("stripe") .input( stripeInputSchema(stripeActionSchemas.list), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).setupIntents.list(toStripeParams(input)), ), ), ) /** Updates mutable fields on a Stripe SetupIntent. */ export const updateStripeSetupIntent = stripeAction< SetupIntentMutationInput, Stripe.SetupIntent >( defineAction("Update Stripe SetupIntent") .describe("Updates the customer, payment method, usage, or metadata.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("setupIntentId", "seti_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { setupIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).setupIntents.update( setupIntentId, params, options, ), ) }), ) /** Confirms a Stripe SetupIntent. */ export const confirmStripeSetupIntent = stripeAction< SetupIntentMutationInput, Stripe.SetupIntent >( defineAction("Confirm Stripe SetupIntent") .describe("Confirms permission to reuse the selected payment method.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("setupIntentId", "seti_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { setupIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).setupIntents.confirm( setupIntentId, params, options, ), ) }), ) /** Cancels a cancelable Stripe SetupIntent. */ export const cancelStripeSetupIntent = stripeAction< SetupIntentMutationInput, Stripe.SetupIntent >( defineAction("Cancel Stripe SetupIntent") .describe( "Cancels a SetupIntent that no longer needs to collect permission.", ) .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("setupIntentId", "seti_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { setupIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).setupIntents.cancel( setupIntentId, params, options, ), ) }), ) /** Creates a Stripe PaymentMethod from provider-supported payment details. */ export const createStripePaymentMethod = stripeAction< StripeMutationInput, Stripe.PaymentMethod >( defineAction("Create Stripe PaymentMethod") .describe("Creates a PaymentMethod for a supported Stripe payment type.") .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ type: z.string().min(1).optional() }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).paymentMethods.create(params, options), ) }), ) /** Retrieves a Stripe PaymentMethod by ID. */ export const getStripePaymentMethod = stripeAction< PaymentMethodIdInput, Stripe.PaymentMethod >( defineAction("Get Stripe PaymentMethod") .describe("Retrieves one PaymentMethod with optional expansions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("paymentMethodId", "pm_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { paymentMethodId, ...params } = input return fromStripe( await getStripeApi(account).paymentMethods.retrieve( paymentMethodId, toStripeParams(params), ), ) }), ) /** Lists Stripe PaymentMethods for a customer or account. */ export const listStripePaymentMethods = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe PaymentMethods") .describe( "Lists PaymentMethods with customer, type, and pagination filters.", ) .account("stripe") .input( stripeInputSchema( stripeActionSchemas.list, ), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).paymentMethods.list(toStripeParams(input)), ), ), ) /** Updates billing details and metadata on a Stripe PaymentMethod. */ export const updateStripePaymentMethod = stripeAction< PaymentMethodMutationInput, Stripe.PaymentMethod >( defineAction("Update Stripe PaymentMethod") .describe( "Updates an attached PaymentMethod's billing details or metadata.", ) .account("stripe") .input( stripeSchema< PaymentMethodMutationInput >(stripeActionSchemas.id("paymentMethodId", "pm_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentMethodId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentMethods.update( paymentMethodId, params, options, ), ) }), ) /** Attaches a Stripe PaymentMethod to a customer. */ export const attachStripePaymentMethod = stripeAction< PaymentMethodMutationInput, Stripe.PaymentMethod >( defineAction("Attach Stripe PaymentMethod") .describe("Attaches a PaymentMethod to a Stripe customer.") .account("stripe") .input( stripeSchema< PaymentMethodMutationInput >( stripeActionSchemas.object({ customer: z.string().startsWith("cus_"), paymentMethodId: z.string().startsWith("pm_"), }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentMethodId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentMethods.attach( paymentMethodId, params, options, ), ) }), ) /** Detaches a Stripe PaymentMethod from its customer. */ export const detachStripePaymentMethod = stripeAction< PaymentMethodMutationInput, Stripe.PaymentMethod >( defineAction("Detach Stripe PaymentMethod") .describe("Permanently detaches a PaymentMethod from its customer.") .account("stripe") .input( stripeSchema< PaymentMethodMutationInput >(stripeActionSchemas.id("paymentMethodId", "pm_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentMethodId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentMethods.detach( paymentMethodId, params, options, ), ) }), )