import {z} from 'zod' export type CheckoutCAAccountData = {account_number?: string | undefined, institution_code?: string | undefined, transit_number?: string | undefined}; export type CheckoutApiAccountSubtype = 'checkings' | 'savings'; export type CheckoutApiAccountType = 'depository'; export type CheckoutUSAccountData = {account_number?: string | undefined, routing_number?: string | undefined}; export type CheckoutCreateBankAccountInput = {account_data_type?: ('us' | 'ca') | undefined, ca_account_data?: CheckoutCAAccountData | undefined, custom_name?: string | undefined, institution_name?: string | undefined, receiver_name?: string | undefined, subtype?: CheckoutApiAccountSubtype | undefined, type?: CheckoutApiAccountType | undefined, us_account_data?: CheckoutUSAccountData | undefined}; export type CreateApiCheckoutPaymentMethodsBankArgs = {body: CheckoutCreateBankAccountInput}; export type CreateApiCheckoutPaymentMethodsBankOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const checkoutCreateBankAccountResponse = z.object({id: z.string().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 }; /** * Create a bank account payment method from checkout token * * POST /checkout/payment-methods/bank */ export const createApiCheckoutPaymentMethodsBank = async (args: CreateApiCheckoutPaymentMethodsBankArgs, options: CreateApiCheckoutPaymentMethodsBankOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set("Authorization", BearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/checkout/payment-methods/bank${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(checkoutCreateBankAccountResponse, await res.json()) };