import {z} from 'zod' export type GetApiPayinsPayinIdArgs = {payinId: string, 'X-Hevn-Account'?: (string | null) | undefined}; export type GetApiPayinsPayinIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const payinState = z.enum(["pending", "awaitingFunds", "processing", "inReview", "completed", "failed", "refunded", "canceled"]); export const fiatPaymentType = z.enum(["sepa", "swift", "ach", "uaefts", "fedwire", "rtp", "fednow", "pix", "spei", "pse", "breb", "transfers_3_0", "ukfps", "npp", "nip", "bdt_local", "bdt_ewallet", "bob_local", "brl_local", "cad_eft", "clp_local", "cny_alipay", "cny_wechatpay", "cop_local", "crc_local", "dkk_local", "dop_local", "dop_ewallet", "egp_local", "gtq_local", "gtq_ewallet", "hkd_fps", "hnl_local", "idr_local", "idr_ewallet", "ils_local", "inr_local", "inr_upi", "jmd_local", "jpy_local", "krw_local", "myr_local", "nok_local", "php_local", "php_ewallet", "pkr_raast", "pkr_ewallet", "pln_local", "sar_local", "sle_local", "thb_local", "try_fast", "tzs_local", "ugx_local", "vnd_local", "xof_local", "zmw_local"]); export const accountKey = z.enum(["USDC", "EURC", "RD", "USDC_ARB", "FDIC_USD", "FDIC_EUR", "KYC_CARDS"]); export const payinQuoteView = z.object({amount: z.string(), currency: z.string(), settlementAmount: z.string(), destinationAccount: accountKey.nullable().optional(), feeAmount: z.string().nullable().optional(), feeCurrency: z.string().nullable().optional(), rate: z.string().nullable().optional()}); export const payinView = z.object({id: z.string(), status: payinState, rail: fiatPaymentType.nullable().optional(), quote: payinQuoteView, paymentReference: z.string().nullable().optional(), originChainId: z.string().nullable().optional(), destinationChainId: z.string().nullable().optional(), expiresAt: z.string().nullable().optional(), createdAt: 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 }; /** * Retrieve an incoming transfer * * GET /payins/{payinId} */ export const getApiPayinsPayinId = async (args: GetApiPayinsPayinIdArgs, options: GetApiPayinsPayinIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Hevn-Account'] != null) headers.set("X-Hevn-Account", String(args['X-Hevn-Account'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/payins/${encodeURIComponent(String(args.payinId))}${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(payinView, await res.json()) };