import {z} from 'zod' export type GetApiApiV2ChannelPaymentUuidArgs = {uuid: string}; export type GetApiApiV2ChannelPaymentUuidOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeRateDto = z.object({base: z.string().optional(), counter: z.string().optional(), rate: z.number().optional()}); export const networkFeeDto = z.object({paidCurrency: z.string().optional(), paidAmount: z.number().optional(), displayCurrency: z.string().optional(), displayAmount: z.number().optional()}); export const merchantChannelPaymentDto = z.object({channelId: z.string().optional(), merchantId: z.string().optional(), merchantDisplayName: z.string().optional(), reference: z.string().optional(), dateCreated: z.number().int().optional(), lastUpdated: z.number().int().optional(), status: z.enum(["DETECTED", "COMPLETE", "HELD", "CANCELLED"]).optional(), uuid: z.string().optional(), hash: z.string().optional(), address: z.string().optional(), tag: z.string().optional(), paidCurrency: z.string().optional(), displayCurrency: z.string().optional(), walletCurrency: z.string().optional(), feeCurrency: z.string().optional(), paidAmount: z.number().optional(), displayAmount: z.number().optional(), walletAmount: z.number().optional(), feeAmount: z.number().optional(), exchangeRate: exchangeRateDto.optional(), displayRate: exchangeRateDto.optional(), risk: z.object({level: z.string().optional(), resourceName: z.string().optional(), resourceCategory: z.string().optional(), alerts: z.array(z.unknown()).optional()}).optional(), sources: z.array(z.string()).optional(), networkFee: networkFeeDto.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 }; /** * Get channel payment * * GET /api/v2/channel/payment/{uuid} */ export const getApiApiV2ChannelPaymentUuid = async (args: GetApiApiV2ChannelPaymentUuidArgs, options?: GetApiApiV2ChannelPaymentUuidOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/channel/payment/${encodeURIComponent(String(args.uuid))}${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(merchantChannelPaymentDto, await res.json()) };