import {z} from 'zod' export type GetApiV1BillingPortalConfigurationsConfigurationArgs = {configuration: string, expand?: Array | undefined, body: Record}; export type GetApiV1BillingPortalConfigurationsConfigurationOptions = {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 }; /** * Retrieve a portal configuration * * GET /v1/billing_portal/configurations/{configuration} * * Also accepts auth: bearerAuth */ export const getApiV1BillingPortalConfigurationsConfiguration = async (args: GetApiV1BillingPortalConfigurationsConfigurationArgs, options?: GetApiV1BillingPortalConfigurationsConfigurationOptions) => { 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") for (const item of args.expand ?? []) query.append("expand", String(item)) 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: "GET", 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()) };