import {z} from 'zod' export type CreatePayoutRequest = {client_reference_id: string, quote_id: string, purpose_code: string, statement_reference?: string | undefined}; export type CreateApiV2PayoutsArgs = {'X-API-Key-ID': string, 'X-API-Timestamp': string, 'X-API-Nonce': string, Digest: string, Authorization: string, body: CreatePayoutRequest}; export type CreateApiV2PayoutsOptions = {baseUrl?: string | undefined, Ed25519?: string | undefined, fetch?: typeof fetch | undefined}; export const feeLine = z.object({type: z.enum(["transfer_fee", "fx_fee", "rail_fee"]), amount: z.string(), currency: z.literal("USD"), rate: z.string()}); export const billingBreakdown = z.object({currency: z.literal("USD"), transfer_fee: z.string(), fx_fee: z.string(), rail_fee: z.string(), total_fee: z.string(), fee_lines: z.array(feeLine)}); export const bankDestinationView = z.object({type: z.literal("bank_account"), account_holder_type: z.enum(["individual", "business"]), account_holder_name: z.string(), bank_country: z.string(), bank_name: z.string(), account_number_masked: z.string(), swift_code: z.string()}); export const failure = z.object({code: z.string(), message: z.string()}).nullable(); export const payoutReturn = z.object({status: z.literal("refunded"), reason_code: z.literal("recipient_bank_rejected"), reason: z.string(), returned_amount: z.string(), refund_amount: z.string(), retained_fee: z.string(), currency: z.literal("USD"), returned_at: z.number().int()}).nullable(); export const payout = z.object({payout_id: z.string(), client_reference_id: z.string(), quote_id: z.string(), provider: z.literal("sgb"), status: z.enum(["processing", "completed", "failed"]), payout_type: z.literal("swift"), source_currency: z.literal("USD"), destination_currency: z.literal("USD"), destination_country: z.string(), amount_mode: z.enum(["send", "receive"]), fee_paid_by: z.enum(["PAYER", "BENEFICIARY"]), sending_amount: z.string(), receiving_amount: z.string(), transfer_fee_amount: z.string(), bank_fee_amount: z.string(), fee_rate_decimal: z.string(), rate: z.string(), billing: billingBreakdown, destination: bankDestinationView, purpose_code: z.string(), statement_reference: z.string().nullable(), bank_reference: z.string().nullable(), failure: failure, return: payoutReturn, is_duplicate: z.boolean(), created_at: z.number().int(), updated_at: z.number().int(), completed_at: z.number().int().nullable()}); export const payoutEnvelope = z.object({code: z.literal(0), message: z.string(), data: payout}); 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 fiat payout * * POST /v2/payouts */ export const createApiV2Payouts = async (args: CreateApiV2PayoutsArgs, options?: CreateApiV2PayoutsOptions) => { const {baseUrl = "https://openapi.infini.money", fetch: fetchFn = fetch, Ed25519} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key-ID'] != null) headers.set("X-API-Key-ID", String(args['X-API-Key-ID'])) if (args['X-API-Timestamp'] != null) headers.set("X-API-Timestamp", String(args['X-API-Timestamp'])) if (args['X-API-Nonce'] != null) headers.set("X-API-Nonce", String(args['X-API-Nonce'])) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Ed25519 !== undefined) headers.set("Authorization", Ed25519) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(payoutEnvelope, await res.json()) };