import {z} from 'zod' export type GetApi10KbAccountsAccountIdPaymentMethodsArgs = {accountId: string, withPluginInfo?: boolean | undefined, includedDeleted?: boolean | undefined, pluginProperty?: Array | undefined, audit?: ('FULL' | 'MINIMAL' | 'NONE') | undefined}; export type GetApi10KbAccountsAccountIdPaymentMethodsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pluginProperty = z.object({key: z.string().optional(), value: z.string().optional(), isUpdatable: z.boolean().optional()}); export const paymentMethodPluginDetail = z.object({externalPaymentMethodId: z.string().optional(), isDefaultPaymentMethod: z.boolean().optional(), properties: z.array(pluginProperty).optional()}); export const entity = z.object({id: z.string().optional(), updatedDate: z.string().optional(), createdDate: z.string().optional()}); export const auditLog = z.object({changeType: z.string().optional(), changeDate: z.string().optional(), objectType: z.enum(["ACCOUNT", "ACCOUNT_EMAIL", "BLOCKING_STATES", "BUNDLE", "CUSTOM_FIELD", "INVOICE", "PAYMENT", "TRANSACTION", "INVOICE_ITEM", "INVOICE_PAYMENT", "SUBSCRIPTION", "SUBSCRIPTION_EVENT", "SERVICE_BROADCAST", "PAYMENT_ATTEMPT", "PAYMENT_METHOD", "TAG", "TAG_DEFINITION", "TENANT", "TENANT_KVS"]).optional(), objectId: z.string().optional(), changedBy: z.string().optional(), reasonCode: z.string().optional(), comments: z.string().optional(), userToken: z.string().optional(), history: entity.optional()}); export const paymentMethod = z.object({paymentMethodId: z.string().optional(), externalKey: z.string().optional(), accountId: z.string().optional(), isDefault: z.boolean().optional(), pluginName: z.string().optional(), pluginInfo: paymentMethodPluginDetail.optional(), auditLogs: z.array(auditLog).optional()}); export const getApi10KbAccountsAccountIdPaymentMethodsResponse = z.array(paymentMethod); 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 account payment methods * * GET /1.0/kb/accounts/{accountId}/paymentMethods * * Also accepts auth: Killbill_Api_Key, Killbill_Api_Secret */ export const getApi10KbAccountsAccountIdPaymentMethods = async (args: GetApi10KbAccountsAccountIdPaymentMethodsArgs, options: GetApi10KbAccountsAccountIdPaymentMethodsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.withPluginInfo != null) query.append("withPluginInfo", String(args.withPluginInfo)) if (args.includedDeleted != null) query.append("includedDeleted", String(args.includedDeleted)) for (const item of args.pluginProperty ?? []) query.append("pluginProperty", String(item)) if (args.audit != null) query.append("audit", String(args.audit)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/kb/accounts/${encodeURIComponent(String(args.accountId))}/paymentMethods${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(getApi10KbAccountsAccountIdPaymentMethodsResponse, await res.json()) };