import {z} from 'zod' export type CreateApiV1TransitPaymentArgs = {body: {integration_type: 'BYBIT' | 'PASS_THROUGH' | 'KRAKEN' | 'OKX', order_info: {merchant_name?: string | undefined, client_id?: string | undefined, payment_type: 'E_COMMERCE', merchant_trade_no: string, order_amount?: string | undefined, quote_id?: string | undefined, currency: 'USDC' | 'USDT' | 'BTC' | 'ETH', rail?: ('SOLANA' | 'BITCOIN' | 'ETHEREUM' | 'TRON' | 'BASE') | undefined, currency_type: 'crypto', redirection_url?: string | undefined, remarks?: string | undefined, success_url: string, failed_url: string, order_expire_time?: number | undefined, customer?: {uid: string, external_user_id: string, user_name: string, register_time: number, kyc_country: string} | undefined, goods: Array<{shopping_name: string, mcc_code: string, goods_name: string, goods_detail?: string | undefined}>, env: {terminal_type: 'APP' | 'WEB' | 'WAP' | 'MINIAPP' | 'OTHERS', device: string, browser_version: string, ip: string}, risk_info?: ({terminal_type: 'APP' | 'WEB' | 'WAP' | 'MINIAPP' | 'OTHERS'}) | undefined}, fin?: ({settlement_config: {settlement_via: 'ONE_TO_ONE' | 'MARKET_ORDER' | 'FEE_RETENTION', destination_details: {wallet_address: string, currency: 'USDC' | 'USDT' | 'BTC' | 'ETH' | 'PYUSD', rail: 'SOLANA' | 'BITCOIN' | 'ETHEREUM' | 'BASE' | 'TRON'}}, developer_fee?: ({fixed?: string | undefined, percentage?: string | undefined} | null) | undefined}) | undefined}}; export type CreateApiV1TransitPaymentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1TransitPaymentResponse = z.object({data: z.object({payment_id: z.string().optional(), integration_type: z.string().optional(), provider_response: z.object({pay_id: z.string().optional(), terminal_type: z.string().nullable().optional(), expire_time: z.number().int().optional(), create_time: z.number().int().optional(), checkout_link: z.string().optional(), qr_content: z.string().nullable().optional()}).optional()}).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 a Payment * * POST /v1/transit/payment */ export const createApiV1TransitPayment = async (args: CreateApiV1TransitPaymentArgs, options?: CreateApiV1TransitPaymentOptions) => { const {baseUrl = "https://sandbox.api.fin.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transit/payment${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(createApiV1TransitPaymentResponse, await res.json()) };