import {z} from 'zod' export type GetApiPaymentMethodsArgs = {Authorization: string, 'Sub-Account'?: string | undefined, customer_id?: string | undefined, payment_method_group_id?: string | undefined, payment_method_import_id?: string | undefined, created_before?: string | undefined, created_after?: string | undefined}; export type GetApiPaymentMethodsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const binDetails = z.object({type: z.string().optional(), card_brand: z.string().optional(), card_class: z.string().optional(), country: z.string().optional(), issuer: z.string().optional(), funding_source: z.string().optional()}).nullable(); export const cardWithBinDetails = z.object({id: z.string().optional(), acct_last_four: z.string().optional(), brand: z.string().optional(), digital_wallet: z.enum(["apple_pay", "google_pay", "null"]).nullable().optional(), name: z.string().nullable().optional(), token: z.string().optional(), month: z.string().optional(), year: z.string().optional(), metadata: z.object({}).nullable().optional(), address_line1_check: z.enum(["fail", "pass", "unavailable", "unchecked"]).optional(), address_postal_code_check: z.enum(["fail", "pass", "unavailable", "unchecked"]).optional(), bin_details: binDetails.nullable().optional()}); export const cardPaymentMethodWithBinDetails = z.object({id: z.string().optional(), status: z.string().optional(), invalid_reason: z.string().nullable().optional(), payment_method_import_id: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), card: cardWithBinDetails.optional(), customer_id: z.string().nullable().optional(), signature: z.string().nullable().optional(), account_id: z.string().nullable().optional()}); export const bankAccount = z.object({id: z.string().optional(), account_owner_name: z.string().optional(), account_type: z.string().optional(), bank_name: z.string().nullable().optional(), acct_last_four: z.string().optional(), token: z.string().optional(), metadata: z.object({}).nullable().optional()}); export const bankAccountPaymentMethodWithStatus = z.object({id: z.string().optional(), status: z.string().optional(), invalid_reason: z.string().nullable().optional(), payment_method_import_id: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), bank_account: bankAccount.optional(), customer_id: z.string().nullable().optional(), signature: z.string().nullable().optional(), account_id: z.string().nullable().optional()}); export const cardPresentPaymentMethod = z.object({id: z.string().optional(), status: z.string().optional(), invalid_reason: z.string().nullable().optional(), payment_method_import_id: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), card_present: z.object({id: z.string().optional(), brand: z.string().optional(), cardholder_name: z.string().optional(), last4: z.string().optional(), expiry: z.string().optional(), type: z.string().optional()}).optional(), customer_id: z.string().nullable().optional(), signature: z.string().nullable().optional(), account_id: z.string().nullable().optional()}); export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiPaymentMethodsResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(z.union([cardPaymentMethodWithBinDetails, bankAccountPaymentMethodWithStatus, cardPresentPaymentMethod])).optional(), page_info: pageInfo.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 }; /** * List Payment Methods * * GET /payment_methods */ export const getApiPaymentMethods = async (args: GetApiPaymentMethodsArgs, options?: GetApiPaymentMethodsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Sub-Account'] != null) headers.set("Sub-Account", String(args['Sub-Account'])) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.payment_method_group_id != null) query.append("payment_method_group_id", String(args.payment_method_group_id)) if (args.payment_method_import_id != null) query.append("payment_method_import_id", String(args.payment_method_import_id)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.created_after != null) query.append("created_after", String(args.created_after)) const url = `${baseUrl.replace(/\/+$/, '')}/payment_methods${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(getApiPaymentMethodsResponse, await res.json()) };