import {z} from 'zod' export type Email = string; export type CreatePayoutRequest = {airtmUserEmail: Email, amount: number, commit?: boolean | undefined, code?: string | undefined, enterpriseFee?: number | undefined, notes: string, internalNote?: string | undefined, requireIdVerified?: boolean | undefined}; export type CreateApiPayoutsArgs = {body: CreatePayoutRequest}; export type CreateApiPayoutsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uuid = z.string(); export const email = z.string(); export const payoutStatus = z.enum(["BRIDGE_COMPLETED", "BRIDGE_CREATED", "BRIDGE_FAILED", "BRIDGE_MAX_RETRIES_REACHED", "BRIDGE_USER_SIGNUP_PENDING", "CANCELED", "CANCEL_REQUESTED", "COMMITTED", "COMPLETED", "COSMOEM_COMPLETED", "CREATED", "ERROR", "FAILED", "PENDING", "PENDING_SIGNATURE", "SIGNED", "PENDING_USER_ACTION", "PROCESSING"]); export const kycInformation = z.object({fullName: z.string().nullable(), birthDate: z.string().nullable(), identificationNumber: z.string().nullable(), countryCode: z.string().nullable(), state: z.string().nullable().optional()}); export const payoutResponse = z.object({id: uuid, bulkPayoutId: uuid.optional(), hash: z.string().optional(), code: z.string().optional(), airtmUserId: uuid, airtmUserEmail: email, requireIdVerified: z.boolean().optional(), notes: z.string().optional(), internalNote: z.string().optional(), status: payoutStatus, grossAmount: z.number(), amount: z.number(), netAmount: z.number(), airtmFee: z.number().optional(), enterpriseFee: z.number().optional(), reasonCode: z.string().optional(), reasonDescription: z.string().optional(), kycInformation: kycInformation.optional(), createdAt: z.string(), updatedAt: z.string()}); 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 payout * * POST /payouts */ export const createApiPayouts = async (args: CreateApiPayoutsArgs, options?: CreateApiPayoutsOptions) => { const {baseUrl = "https://api.enterprise.airtm.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payouts${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(payoutResponse, await res.json()) };