import {z} from 'zod' export type RainforestCard = {brand: string, last4: string, expMonth: number, expYear: number}; export type RainforestACH = {accountHolderType: string, accountNumberLast4: string, bankName: string, routingNumber: string}; export type ApplePay = {type: string, brand: string, brandDesc: string, description: string}; export type BillingContact = {name: string | null, addressLine1: string | null, addressLine2: string | null, city: string | null, state: string | null, postalCode: string | null, country: string | null, email: string | null, phone: string | null}; export type RainforestPaymentMethod = {paymentMethodId: string, paymentMethodConfigId: string, fingerprint: string | null, methodType: string, card: RainforestCard | null, ach: RainforestACH | null, applePay: ApplePay | null, billingContact: BillingContact | null, metadata: Record, createdAt: string, updatedAt: string}; export type CreateApiUserPatientPaymentMethodsCreateArgs = {TENANT: string, 'API-KEY': string, patient_id: string, body: RainforestPaymentMethod}; export type CreateApiUserPatientPaymentMethodsCreateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const paymentMethodType = z.enum(["CARD", "ACH", "APPLE_PAY"]); export const paymentMethodSummary = z.object({id: z.string(), patientId: z.string().nullable(), rainforestPaymentMethodId: z.string(), brand: z.string().nullable(), last4: z.string().nullable(), expMonth: z.number().int().nullable(), expYear: z.number().int().nullable(), accountHolderType: z.string().nullable(), accountNumberLast4: z.number().int().nullable(), bankName: z.string().nullable(), routingNumber: z.number().int().nullable(), description: z.string().nullable(), type: paymentMethodType.nullable(), isDefault: z.boolean().optional(), isArchived: z.boolean().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 Payment Method * * POST /user/patient/payment-methods/create */ export const createApiUserPatientPaymentMethodsCreate = async (args: CreateApiUserPatientPaymentMethodsCreateArgs, options: CreateApiUserPatientPaymentMethodsCreateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) const url = `${baseUrl.replace(/\/+$/, '')}/user/patient/payment-methods/create${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(paymentMethodSummary, await res.json()) };