import {z} from 'zod' export type GetApiSubscriptionsSubscriptionIdArgs = {'Finix-Version'?: string | undefined}; export type GetApiSubscriptionsSubscriptionIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const amount = z.number().int(); export const billingCycleDay = z.number().int().gte(1).lte(31).nullable(); export const billingInterval = z.enum(["BIMONTHLY", "BIWEEKLY", "BIYEARLY", "DAILY", "MONTHLY", "QUARTERLY", "SEMIYEARLY", "TRIYEARLY", "WEEKLY", "YEARLY"]); export const receiptRequestedDeliveryMethods = z.array(z.object({type: z.enum(["EMAIL", "SMS", "PRINT"]), destinations: z.array(z.string())})); export const country = z.enum(["ABW", "AFG", "AGO", "AIA", "ALA", "ALB", "AND", "ARE", "ARG", "ARM", "ASM", "ATA", "ATF", "ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BES", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLM", "BLR", "BLZ", "BMU", "BOL", "BRA", "BRB", "BRN", "BTN", "BVT", "BWA", "CAF", "CAN", "CCK", "CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPV", "CRI", "CUB", "CUW", "CXR", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", "ESH", "ESP", "EST", "ETH", "FIN", "FJI", "FLK", "FRA", "FRO", "FSM", "GAB", "GBR", "GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNB", "GNQ", "GRC", "GRD", "GRL", "GTM", "GUF", "GUM", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN", "IDN", "IMN", "IND", "IOT", "IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBR", "LBY", "LCA", "LIE", "LKA", "LSO", "LTU", "LUX", "LVA", "MAC", "MAF", "MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", "MLT", "MMR", "MNE", "MNG", "MNP", "MRT", "MSR", "MTQ", "MUS", "MWI", "MYS", "MYT", "NAM", "NCL", "NER", "NFK", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL", "NRU", "NZL", "OMN", "PAK", "PAN", "PCN", "PER", "PHL", "PLW", "PNG", "POL", "PRI", "PRK", "PRT", "PRY", "PSE", "PYF", "QAT", "REU", "ROU", "RUS", "RWA", "SAU", "SDN", "SEN", "SGP", "SGS", "SHN", "SJM", "SLB", "SLE", "SLV", "SMR", "SOM", "SPM", "SRB", "SSD", "STP", "SUR", "SVK", "SVN", "SWE", "SWZ", "SXM", "SYC", "SYR", "TCA", "TCD", "TGO", "THA", "TJK", "TKL", "TKM", "TLS", "TON", "TTO", "TUN", "TUR", "TUV", "TWN", "TZA", "UGA", "UKR", "UMI", "URY", "USA", "UZB", "VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "XKX", "YEM", "ZAF", "ZMB", "ZWE"]).nullable(); export const address = z.object({line1: z.string().min(1).nullable(), line2: z.string().min(1).nullable().optional(), city: z.string().min(1).nullable(), region: z.string().min(1).nullable(), postal_code: z.string().min(1).nullable(), country: country}); export const nickname = z.string().nullable(); export const tags = z.record(z.string(), z.unknown()).nullable(); export const self = z.object({href: z.string().min(1).optional()}); export const subscription = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), amount: amount.optional(), application_id: z.string().optional(), billing_cycle_day: billingCycleDay.optional(), billing_interval: billingInterval.optional(), buyer_details: z.object({identity_id: z.string().optional(), instrument_id: z.unknown(), requested_delivery_methods: receiptRequestedDeliveryMethods.optional(), shipping_address: address.nullable().optional()}).optional(), canceled_via: z.enum(["MERCHANT", "AUTOMATED_OVERDUE", "SUPPORT"]).nullable().optional(), currency: z.enum(["USD", "CAD"]).optional(), end_subscription_at: z.string().nullable().optional(), expires_at: z.string().nullable().optional(), first_charge_at: z.string().optional(), linked_to: z.string().optional(), linked_type: z.literal("MERCHANT").optional(), next_billing_date: z.object({year: z.number().int().optional(), month: z.number().int().optional(), day: z.number().int().optional()}).optional(), nickname: nickname.optional(), start_subscription_at: z.string().optional(), state: z.enum(["ACTIVE", "CANCELED", "EXPIRED", "NOT_STARTED", "PAST_DUE"]).optional(), subscription_details: z.object({collection_method: z.literal("BILL_AUTOMATICALLY"), send_invoice: z.boolean().optional(), send_receipt: z.boolean().optional(), trial_details: z.object({interval_type: z.enum(["DAY", "WEEK", "MONTH", "YEAR"]).optional(), interval_count: z.number().int().optional(), trial_started_at: z.string().nullable().optional(), trial_expected_start_at: z.string().optional(), trial_expected_end_at: z.string().optional()}).nullable().optional(), discount_phase_details: z.object({amount: z.number().int().optional(), billing_interval_count: z.number().int().optional(), discount_phase_started_at: z.string().nullable().optional(), discount_phase_expected_start_at: z.string().optional(), discount_phase_expected_end_at: z.string().optional()}).nullable().optional(), notification_preferences: z.object({send_confirmation: z.boolean().optional()}).optional()}).optional(), subscription_link_id: z.string().nullable().optional(), subscription_phase: z.enum(["EVERGREEN", "DISCOUNT", "FIXED", "NONE", "TRIAL"]).optional(), subscription_plan_id: z.string().nullable().optional(), has_usage_based_plan: z.boolean().nullable().optional(), tags: tags.optional(), total_billing_intervals: z.number().int().nullable().optional(), _links: z.object({self: self.optional()}).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 }; /** * Fetch a Subscription * * GET /subscriptions/{subscription_id} */ export const getApiSubscriptionsSubscriptionId = async (args: GetApiSubscriptionsSubscriptionIdArgs, options?: GetApiSubscriptionsSubscriptionIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/${encodeURIComponent(String(args.subscription_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(subscription, await res.json()) };