import {z} from 'zod' export type CreateApiV1BillingCreateScheduleArgs = {'x-api-version': string, body: {customer_id: string, entity_id?: string | undefined, invoice_mode?: ({enabled: boolean, enable_plan_immediately?: boolean | undefined, finalize?: boolean | undefined, invoice_template_id?: string | undefined, net_terms_days?: number | undefined}) | undefined, discounts?: (Array<{reward_id?: string | undefined, promotion_code?: string | undefined}>) | undefined, success_url?: string | undefined, checkout_session_params?: {[key: string]: unknown} | undefined, redirect_mode?: ('always' | 'if_required' | 'never') | undefined, billing_behavior?: ('prorate_immediately' | 'none') | undefined, billing_cycle_anchor?: 'now' | undefined, enable_plan_immediately?: boolean | undefined, phases: Array<{starts_at?: (number | 'now') | undefined, starting_after?: ({duration_type: 'month' | 'year', duration_count: number}) | undefined, plans: Array<{plan_id: string, feature_quantities?: (Array<{feature_id: string, quantity?: number | undefined, adjustable?: boolean | undefined}>) | undefined, version?: number | undefined, customize?: ({price?: ({amount: number, interval: 'one_off' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year', interval_count?: number | undefined, additional_currencies?: Array<{currency: string, amount: number}> | undefined} | null) | undefined, items?: (Array<{feature_id: string, included?: number | undefined, unlimited?: boolean | undefined, reset?: ({interval: 'one_off' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year', interval_count?: number | undefined}) | undefined, price?: ({amount?: number | undefined, additional_currencies?: Array<{currency: unknown, amount: unknown}> | undefined, tiers?: Array<{to: unknown, amount: unknown, flat_amount: unknown, additional_currencies: unknown}> | undefined, tier_behavior?: ('graduated' | 'volume') | undefined, interval: 'one_off' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year', interval_count?: number | undefined, billing_units?: number | undefined, billing_method: 'prepaid' | 'usage_based', max_purchase?: (number | null) | undefined}) | undefined, proration?: ({on_increase: 'bill_immediately' | 'prorate_immediately' | 'prorate_next_cycle' | 'bill_next_cycle', on_decrease: 'prorate' | 'prorate_immediately' | 'prorate_next_cycle' | 'none' | 'no_prorations'}) | undefined, rollover?: ({max?: number | undefined, max_percentage?: number | undefined, expiry_duration_type: 'month' | 'forever', expiry_duration_length?: number | undefined}) | undefined}>) | undefined, add_items?: (Array<{feature_id: string, included?: number | undefined, unlimited?: boolean | undefined, reset?: ({interval: 'one_off' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year', interval_count?: number | undefined}) | undefined, price?: ({amount?: number | undefined, additional_currencies?: Array<{currency: unknown, amount: unknown}> | undefined, tiers?: Array<{to: unknown, amount: unknown, flat_amount: unknown, additional_currencies: unknown}> | undefined, tier_behavior?: ('graduated' | 'volume') | undefined, interval: 'one_off' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year', interval_count?: number | undefined, billing_units?: number | undefined, billing_method: 'prepaid' | 'usage_based', max_purchase?: (number | null) | undefined}) | undefined, proration?: ({on_increase: 'bill_immediately' | 'prorate_immediately' | 'prorate_next_cycle' | 'bill_next_cycle', on_decrease: 'prorate' | 'prorate_immediately' | 'prorate_next_cycle' | 'none' | 'no_prorations'}) | undefined, rollover?: ({max?: number | undefined, max_percentage?: number | undefined, expiry_duration_type: 'month' | 'forever', expiry_duration_length?: number | undefined}) | undefined}>) | undefined, remove_items?: (Array<{feature_id?: string | undefined, billing_method?: ('prepaid' | 'usage_based') | undefined, interval?: ('one_off' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year' | 'one_off' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'semi_annual' | 'year') | undefined, interval_count?: number | undefined}>) | undefined, billing_controls?: ({auto_topups?: (Array<{feature_id: string, enabled?: boolean | undefined, threshold: number, quantity: number, purchase_limit?: ({interval: 'hour' | 'day' | 'week' | 'month', interval_count?: number | undefined, limit: number, count?: number | undefined}) | undefined, invoice_mode?: boolean | undefined}>) | undefined, spend_limits?: (Array<{feature_id?: string | undefined, enabled?: boolean | undefined, limit_type?: ('absolute' | 'usage_percentage') | undefined, overage_limit?: number | undefined, skip_overage_billing?: boolean | undefined}>) | undefined, usage_limits?: (Array<{feature_id: string, enabled?: boolean | undefined, limit: number, interval: 'day' | 'week' | 'month' | 'year', filter?: {properties: {[key: string]: unknown}} | undefined}>) | undefined, usage_alerts?: (Array<{feature_id?: string | undefined, enabled?: boolean | undefined, threshold: number, threshold_type: 'usage' | 'usage_percentage' | 'remaining' | 'remaining_percentage', name?: string | undefined}>) | undefined, overage_allowed?: (Array<{feature_id: string, enabled?: boolean | undefined}>) | undefined}) | undefined}) | undefined, subscription_id?: string | undefined}>, billing_cycle_anchor?: 'phase_start' | undefined}>}}; export type CreateApiV1BillingCreateScheduleOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1BillingCreateScheduleResponse = z.object({customer_id: z.string(), entity_id: z.string().nullable(), status: z.enum(["created", "pending_payment"]), schedule_id: z.string().nullable(), phases: z.array(z.object({phase_id: z.string(), starts_at: z.number(), customer_product_ids: z.array(z.string())})), invoice: z.object({status: z.string().nullable(), stripe_id: z.string(), total: z.number(), currency: z.string(), hosted_invoice_url: z.string().nullable()}).optional(), payment_url: z.string().nullable(), required_action: z.object({code: z.enum(["3ds_required", "payment_method_required", "payment_failed", "payment_processing"]), reason: z.string()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Creates a multi-phase subscription schedule for a customer. The first phase starts immediately and subsequent phases automatically transition at their scheduled start times. * * POST /v1/billing.create_schedule */ export const createApiV1BillingCreateSchedule = async (args: CreateApiV1BillingCreateScheduleArgs, options?: CreateApiV1BillingCreateScheduleOptions) => { const {baseUrl = "https://api.useautumn.com", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing.create_schedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1BillingCreateScheduleResponse, await res.json()) };