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 PaymentIntent retrieval input with a public resource ID. */ type PaymentIntentIdInput = StripeInputValue & { paymentIntentId: string } /** Stripe PaymentIntent mutation input with a public resource ID. */ type PaymentIntentMutationInput = StripeMutationInput & { paymentIntentId: string } /** Stripe refund retrieval input with a public resource ID. */ type RefundIdInput = StripeInputValue & { refundId: string } /** Stripe refund mutation input with a public resource ID. */ type RefundMutationInput = StripeMutationInput & { refundId: string } /** Creates a Stripe PaymentIntent for a precise custom payment flow. */ export const createStripePaymentIntent = stripeAction< StripeMutationInput, Stripe.PaymentIntent >( defineAction("Create Stripe PaymentIntent") .describe( "Creates a PaymentIntent with amount, currency, and payment options.", ) .account("stripe") .input( stripeMutationSchema( stripeActionSchemas.mutation({ amount: z.int().positive(), captureMethod: z .enum(["automatic", "automatic_async", "manual"]) .optional(), confirmationMethod: z.enum(["automatic", "manual"]).optional(), currency: z.string().regex(/^[a-z]{3}$/), customer: z.string().startsWith("cus_").optional(), paymentMethod: z.string().min(1).optional(), setupFutureUsage: 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).paymentIntents.create(params, options), ) }), ) /** Retrieves a Stripe PaymentIntent by ID. */ export const getStripePaymentIntent = stripeAction< PaymentIntentIdInput, Stripe.PaymentIntent >( defineAction("Get Stripe PaymentIntent") .describe("Retrieves one PaymentIntent with optional expansions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("paymentIntentId", "pi_"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { paymentIntentId, ...params } = input return fromStripe( await getStripeApi(account).paymentIntents.retrieve( paymentIntentId, toStripeParams(params), ), ) }), ) /** Lists PaymentIntents with customer and creation filters. */ export const listStripePaymentIntents = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe PaymentIntents") .describe("Lists PaymentIntents with Stripe cursor pagination.") .account("stripe") .input( stripeInputSchema( stripeActionSchemas.list, ), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).paymentIntents.list(toStripeParams(input)), ), ), ) /** Searches PaymentIntents with Stripe Search Query Language. */ export const searchStripePaymentIntents = stripeAction< StripeInputValue, Stripe.ApiSearchResult >( defineAction("Search Stripe PaymentIntents") .describe("Searches PaymentIntents with Stripe Search Query Language.") .account("stripe") .input( stripeInputSchema( stripeActionSchemas.search, ), ) .output(stripeObjectSchema>()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => fromStripe( await getStripeApi(account).paymentIntents.search( toStripeParams(input), ), ), ), ) /** Updates mutable fields on a Stripe PaymentIntent. */ export const updateStripePaymentIntent = stripeAction< PaymentIntentMutationInput, Stripe.PaymentIntent >( defineAction("Update Stripe PaymentIntent") .describe("Updates payment, customer, shipping, or metadata fields.") .account("stripe") .input( stripeSchema< PaymentIntentMutationInput >(stripeActionSchemas.id("paymentIntentId", "pi_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentIntents.update( paymentIntentId, params, options, ), ) }), ) /** Confirms a Stripe PaymentIntent and attempts to complete payment. */ export const confirmStripePaymentIntent = stripeAction< PaymentIntentMutationInput, Stripe.PaymentIntent >( defineAction("Confirm Stripe PaymentIntent") .describe( "Confirms a PaymentIntent with payment method and return settings.", ) .account("stripe") .input( stripeSchema< PaymentIntentMutationInput >(stripeActionSchemas.id("paymentIntentId", "pi_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentIntents.confirm( paymentIntentId, params, options, ), ) }), ) /** Cancels a cancelable Stripe PaymentIntent. */ export const cancelStripePaymentIntent = stripeAction< PaymentIntentMutationInput, Stripe.PaymentIntent >( defineAction("Cancel Stripe PaymentIntent") .describe( "Cancels a PaymentIntent and releases remaining capturable funds.", ) .account("stripe") .input( stripeSchema< PaymentIntentMutationInput >(stripeActionSchemas.id("paymentIntentId", "pi_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentIntents.cancel( paymentIntentId, params, options, ), ) }), ) /** Captures funds from a manually captured Stripe PaymentIntent. */ export const captureStripePaymentIntent = stripeAction< PaymentIntentMutationInput, Stripe.PaymentIntent >( defineAction("Capture Stripe PaymentIntent") .describe("Captures all or part of an authorized PaymentIntent.") .account("stripe") .input( stripeSchema< PaymentIntentMutationInput >(stripeActionSchemas.id("paymentIntentId", "pi_")), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { paymentIntentId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).paymentIntents.capture( paymentIntentId, params, options, ), ) }), ) /** * Creates a full or partial refund from a charge, PaymentIntent, or customer * balance. */ export const createStripeRefund = stripeAction< StripeMutationInput, Stripe.Refund >( defineAction("Create Stripe refund") .describe("Creates a full or partial refund with reason and metadata.") .account("stripe") .input( stripeMutationSchema( stripeActionSchemas .mutation({ amount: z.int().positive().optional(), charge: z.string().startsWith("ch_").optional(), currency: z .string() .regex(/^[a-z]{3}$/) .optional(), customer: z.string().startsWith("cus_").optional(), origin: z.literal("customer_balance").optional(), paymentIntent: z.string().startsWith("pi_").optional(), reason: z .enum(["duplicate", "fraudulent", "requested_by_customer"]) .optional(), }) .superRefine((input, context) => { const hasCharge = input.charge !== undefined const hasPaymentIntent = input.paymentIntent !== undefined const hasCustomerBalanceField = input.origin !== undefined || input.customer !== undefined || input.currency !== undefined const hasCompleteCustomerBalance = input.origin === "customer_balance" && input.customer !== undefined && input.currency !== undefined if ( !( (hasCharge && !hasPaymentIntent && !hasCustomerBalanceField) || (hasPaymentIntent && !hasCharge && !hasCustomerBalanceField) || (hasCompleteCustomerBalance && !hasCharge && !hasPaymentIntent) ) ) { context.addIssue({ code: "custom", message: "Provide exactly one charge, PaymentIntent, or customer-balance source.", path: ["paymentIntent"], }) } }), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const [params, options] = stripeMutationArguments(input) return fromStripe( await getStripeApi(account).refunds.create(params, options), ) }), ) /** Retrieves a Stripe refund by ID. */ export const getStripeRefund = stripeAction< RefundIdInput, Stripe.Refund >( defineAction("Get Stripe refund") .describe("Retrieves one Stripe refund with optional expansions.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("refundId"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { refundId, ...params } = input return fromStripe( await getStripeApi(account).refunds.retrieve( refundId, toStripeParams(params), ), ) }), ) /** Lists Stripe refunds by charge, PaymentIntent, or creation time. */ export const listStripeRefunds = stripeAction< StripeInputValue, Stripe.ApiList >( defineAction("List Stripe refunds") .describe("Lists refunds 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).refunds.list(toStripeParams(input)), ), ), ) /** Updates the metadata on a Stripe refund. */ export const updateStripeRefund = stripeAction< RefundMutationInput, Stripe.Refund >( defineAction("Update Stripe refund") .describe("Updates mutable metadata on one Stripe refund.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("refundId"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { refundId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).refunds.update(refundId, params, options), ) }), ) /** Cancels a pending Stripe refund when its payment method allows it. */ export const cancelStripeRefund = stripeAction< RefundMutationInput, Stripe.Refund >( defineAction("Cancel Stripe refund") .describe("Cancels a pending refund for a supported payment method.") .account("stripe") .input( stripeSchema>( stripeActionSchemas.id("refundId"), ), ) .output(stripeObjectSchema()) .retry({ replaySafety: stripeMutationReplaySafety }) .handler(async ({ account, input }) => { const { refundId, ...mutation } = input const [params, options] = stripeMutationArguments(mutation) return fromStripe( await getStripeApi(account).refunds.cancel(refundId, params, options), ) }), )