import {z} from 'zod' export type GetApiRecurringAccountIdArgs = {accountId: number}; export type GetApiRecurringAccountIdOptions = {baseUrl?: string | undefined, Passcode?: string | undefined, fetch?: typeof fetch | undefined}; export const recurringPaymentDetails = z.object({amount: z.number().optional(), disable_tax1: z.boolean().optional(), disable_tax2: z.boolean().optional(), process_back_payments: z.boolean().optional()}); export const address = z.object({name: z.string().max(64).optional(), address_line1: z.string().max(64).optional(), address_line2: z.string().max(64).optional(), city: z.string().max(32).optional(), province: z.string().optional(), country: z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW"]).max(2).optional(), postal_code: z.string().max(16).optional(), phone_number: z.string().optional(), phone_country_code: z.string().max(3).optional(), email_address: z.string().max(64).optional()}); export const recurringPaymentCard = z.object({card_type: z.enum(["AM", "DI", "JB", "MC", "NN", "VI", "PV", "MD"]).max(2).optional(), name: z.string().min(1).max(64).optional(), expiry_year: z.string().max(2).optional(), expiry_month: z.string().max(2).optional()}); export const recurringPaymentCustom = z.object({order_number: z.string().max(30).optional(), comments: z.string().max(2000).optional(), ref1: z.string().max(256).optional(), ref2: z.string().max(256).optional(), ref3: z.string().max(256).optional(), ref4: z.string().max(256).optional(), ref5: z.string().max(256).optional()}); export const upcomingTransactionRecord = z.object({billing_date: z.string().optional(), billing_end: z.string().optional(), amount: z.number().optional(), total_amount: z.number().optional(), tax1: z.number().optional(), tax2: z.number().optional()}); export const recurringPaymentSchedule = z.object({period: z.string().optional(), increment: z.number().int().optional(), start_date: z.string().optional(), second_billing: z.string().optional(), expiry_date: z.string().optional(), never_expires: z.boolean().optional(), bill_at_end_of_month: z.boolean().optional(), upcoming_transactions: z.array(upcomingTransactionRecord).optional()}); export const recurringPaymentResponse = z.object({account_id: z.number().optional(), state: z.enum(["Active", "OnHold", "Closed"]).max(6).optional(), language: z.string().max(3).optional(), payment: recurringPaymentDetails.optional(), billing: address.optional(), card: recurringPaymentCard.optional(), custom: recurringPaymentCustom.optional(), schedule: recurringPaymentSchedule.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 }; /** * Get account * * GET /recurring/{accountId} * * Also accepts auth: Sub-Merchant-Id */ export const getApiRecurringAccountId = async (args: GetApiRecurringAccountIdArgs, options?: GetApiRecurringAccountIdOptions) => { const {baseUrl = "https://api.na.bambora.com/v1", fetch: fetchFn = fetch, Passcode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Passcode !== undefined) headers.set("Authorization", Passcode) const url = `${baseUrl.replace(/\/+$/, '')}/recurring/${encodeURIComponent(String(args.accountId))}${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(recurringPaymentResponse, await res.json()) };