import {z} from 'zod' export type CreateApiV1BillingPortalConfigurationsConfigurationArgs = {configuration: string, body: {active?: boolean | undefined, business_profile?: ({headline?: (string | '') | undefined, privacy_policy_url?: (string | '') | undefined, terms_of_service_url?: (string | '') | undefined}) | undefined, default_return_url?: (string | '') | undefined, expand?: Array | undefined, features?: ({customer_update?: ({allowed_updates?: (Array<'address' | 'email' | 'name' | 'phone' | 'shipping' | 'tax_id'> | '') | undefined, enabled?: boolean | undefined}) | undefined, invoice_history?: {enabled: boolean} | undefined, payment_method_update?: ({enabled: boolean, payment_method_configuration?: (string | '') | undefined}) | undefined, subscription_cancel?: ({cancellation_reason?: ({enabled: boolean, feedback_options?: (Array | '') | undefined, options?: (Array<'customer_service' | 'low_quality' | 'missing_features' | 'other' | 'switched_service' | 'too_complex' | 'too_expensive' | 'unused'> | '') | undefined}) | undefined, enabled?: boolean | undefined, mode?: ('at_period_end' | 'immediately') | undefined, proration_behavior?: ('always_invoice' | 'create_prorations' | 'none') | undefined}) | undefined, subscription_update?: ({billing_cycle_anchor?: ('now' | 'unchanged') | undefined, default_allowed_updates?: (Array<'price' | 'promotion_code' | 'quantity'> | '') | undefined, enabled?: boolean | undefined, products?: (Array<{adjustable_quantity?: ({enabled: boolean, maximum?: number | undefined, minimum?: number | undefined}) | undefined, prices: Array, product: string}> | '') | undefined, proration_behavior?: ('always_invoice' | 'create_prorations' | 'none') | undefined, schedule_at_period_end?: ({conditions?: (Array<{type: 'decreasing_item_amount' | 'shortening_interval'}> | '') | undefined}) | undefined, trial_update_behavior?: ('continue_trial' | 'end_trial') | undefined}) | undefined}) | undefined, login_page?: {enabled: boolean} | undefined, metadata?: ({[key: string]: string} | '') | undefined, name?: (string | '') | undefined}}; export type CreateApiV1BillingPortalConfigurationsConfigurationOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const application = z.object({id: z.string().max(5000), name: z.string().max(5000).nullable().optional(), object: z.literal("application")}); export const deletedApplication = z.object({deleted: z.literal(true), id: z.string().max(5000), name: z.string().max(5000).nullable().optional(), object: z.literal("application")}); export const portalBusinessProfile = z.object({headline: z.string().max(5000).nullable().optional(), privacy_policy_url: z.string().max(5000).nullable().optional(), terms_of_service_url: z.string().max(5000).nullable().optional()}); export const portalCustomerUpdate = z.object({allowed_updates: z.array(z.enum(["address", "email", "name", "phone", "shipping", "tax_id"])), enabled: z.boolean()}); export const portalInvoiceList = z.object({enabled: z.boolean()}); export const portalPaymentMethodUpdate = z.object({enabled: z.boolean(), payment_method_configuration: z.string().max(5000).nullable().optional()}); export const feedbackOptionsStatusTransitions = z.object({deactivated_at: z.number().int().nullable().optional()}); export const billingFeedbackOption = z.object({description: z.string().max(5000), id: z.string().max(5000), livemode: z.boolean(), object: z.literal("billing.feedback_option"), status: z.enum(["active", "inactive"]), status_transitions: feedbackOptionsStatusTransitions}); export const portalSubscriptionCancellationReason = z.object({enabled: z.boolean(), feedback_options: z.array(z.union([z.string().max(5000), billingFeedbackOption])).nullable().optional(), options: z.array(z.enum(["customer_service", "low_quality", "missing_features", "other", "switched_service", "too_complex", "too_expensive", "unused"]))}); export const portalSubscriptionCancel = z.object({cancellation_reason: portalSubscriptionCancellationReason, enabled: z.boolean(), mode: z.enum(["at_period_end", "immediately"]), proration_behavior: z.enum(["always_invoice", "create_prorations", "none"])}); export const portalSubscriptionUpdateProductAdjustableQuantity = z.object({enabled: z.boolean(), maximum: z.number().int().nullable().optional(), minimum: z.number().int()}); export const portalSubscriptionUpdateProduct = z.object({adjustable_quantity: portalSubscriptionUpdateProductAdjustableQuantity, prices: z.array(z.string().max(5000)), product: z.string().max(5000)}); export const portalResourceScheduleUpdateAtPeriodEndCondition = z.object({type: z.enum(["decreasing_item_amount", "shortening_interval"])}); export const portalResourceScheduleUpdateAtPeriodEnd = z.object({conditions: z.array(portalResourceScheduleUpdateAtPeriodEndCondition)}); export const portalSubscriptionUpdate = z.object({billing_cycle_anchor: z.enum(["now", "unchanged"]).nullable().optional(), default_allowed_updates: z.array(z.enum(["price", "promotion_code", "quantity"])), enabled: z.boolean(), products: z.array(portalSubscriptionUpdateProduct).nullable().optional(), proration_behavior: z.enum(["always_invoice", "create_prorations", "none"]), schedule_at_period_end: portalResourceScheduleUpdateAtPeriodEnd, trial_update_behavior: z.enum(["continue_trial", "end_trial"])}); export const portalFeatures = z.object({customer_update: portalCustomerUpdate, invoice_history: portalInvoiceList, payment_method_update: portalPaymentMethodUpdate, subscription_cancel: portalSubscriptionCancel, subscription_update: portalSubscriptionUpdate}); export const portalLoginPage = z.object({enabled: z.boolean(), url: z.string().max(5000).nullable().optional()}); export const billingPortalConfiguration = z.object({active: z.boolean(), application: z.union([z.string().max(5000), application, deletedApplication]).nullable().optional(), business_profile: portalBusinessProfile, created: z.number().int(), default_return_url: z.string().max(5000).nullable().optional(), features: portalFeatures, id: z.string().max(5000), is_default: z.boolean(), livemode: z.boolean(), login_page: portalLoginPage, metadata: z.record(z.string(), z.string().max(500)).nullable().optional(), name: z.string().max(5000).nullable().optional(), object: z.literal("billing_portal.configuration"), updated: z.number().int()}); 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 }; /** * Update a portal configuration * * POST /v1/billing_portal/configurations/{configuration} * * Also accepts auth: bearerAuth */ export const createApiV1BillingPortalConfigurationsConfiguration = async (args: CreateApiV1BillingPortalConfigurationsConfigurationArgs, options?: CreateApiV1BillingPortalConfigurationsConfigurationOptions) => { const {baseUrl = "https://api.stripe.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing_portal/configurations/${encodeURIComponent(String(args.configuration))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(billingPortalConfiguration, await res.json()) };