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 customer retrieval input with a public resource ID. */ type CustomerIdInput = StripeInputValue & { customerId: string } /** Stripe customer mutation input with a public resource ID. */ type CustomerMutationInput = StripeMutationInput & { customerId: string } /** Creates a Stripe customer with billing, shipping, tax, and metadata fields. */ export const createStripeCustomer = stripeAction< StripeMutationInput, Stripe.Customer >( defineAction("Create Stripe customer") .describe("Creates a customer for payments, invoicing, or subscriptions.") .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ email: z.email().optional() }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).customers.create(params, options), ) }), ) /** Retrieves one Stripe customer, including deleted-customer tombstones. */ export const getStripeCustomer = stripeAction< CustomerIdInput, Stripe.Customer | Stripe.DeletedCustomer >( defineAction("Get Stripe customer") .describe("Retrieves a Stripe customer by ID with optional expansions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("customerId", "cus_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { customerId, ...params } = input return fromStripe( await getStripeApi(account).customers.retrieve( customerId, toStripeParams(params), ), ) }), ) /** Lists Stripe customers with cursor pagination and creation filters. */ export const listStripeCustomers = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe customers") .describe("Lists customers from newest to oldest with Stripe filters.") .account("stripe") .input( stripeInputSchema(stripeActionSchemas.list), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).customers.list(toStripeParams(input)), ), ), ) /** Searches Stripe customers with Stripe Search Query Language. */ export const searchStripeCustomers = stripeAction< StripeInputValue, Stripe.ApiSearchResult >( defineAction("Search Stripe customers") .describe("Searches customers with Stripe Search Query Language.") .account("stripe") .input( stripeInputSchema( stripeActionSchemas.search, ), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).customers.search(toStripeParams(input)), ), ), ) /** Updates selected fields on an existing Stripe customer. */ export const updateStripeCustomer = stripeAction< CustomerMutationInput, Stripe.Customer >( defineAction("Update Stripe customer") .describe("Updates billing, shipping, tax, payment, or metadata fields.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("customerId", "cus_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { customerId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).customers.update( customerId, params, options, ), ) }), ) /** Permanently deletes a Stripe customer and cancels active subscriptions. */ export const deleteStripeCustomer = stripeAction< CustomerMutationInput, Stripe.DeletedCustomer >( defineAction("Delete Stripe customer") .describe("Permanently deletes a customer and cancels its subscriptions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("customerId", "cus_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { customerId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).customers.del(customerId, params, options), ) }), )