import {z} from 'zod' export type CreateApiV2PaymentsArgs = {'x-idempotency-key': string, body: {from: string, to: Array<{to: string, amount_cents: number, currency_symbol?: 'USD' | undefined, invoice_description?: string | undefined}>, pay_now: boolean, network?: 'arbitrum' | undefined}}; export type CreateApiV2PaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2PaymentsResponse = z.object({success: z.literal(true), data: z.object({to_sign: z.union([z.object({domain: z.object({name: z.string(), version: z.string(), chainId: z.number(), verifyingContract: z.string()}), types: z.object({CreatePaymentAuthorizationForwardRequest: z.array(z.object({name: z.string(), type: z.string()})), RisePaymentAuthorizationRequest: z.array(z.object({name: z.string(), type: z.string()}))}), typed_data: z.object({from: z.string(), to: z.string(), salt: z.string(), expires: z.string(), data: z.object({groupID: z.string(), merkleRoot: z.string(), paymentsCount: z.string(), maxAmountPerPayment: z.string(), deadline: z.string()})}), primary_type: z.string()}), z.object({domain: z.object({name: z.string(), version: z.string(), chainId: z.number(), verifyingContract: z.string()}), types: z.object({CreatePaymentsAndExecutePayNowForwardRequest: z.array(z.object({name: z.string(), type: z.string()})), RisePayNowBatchRequest: z.array(z.object({name: z.string(), type: z.string()})), RisePayNow: z.array(z.object({name: z.string(), type: z.string()}))}), typed_data: z.object({from: z.string(), to: z.string(), salt: z.string(), expires: z.string(), data: z.object({groupID: z.string(), token: z.string(), payments: z.array(z.object({id: z.string(), recipient: z.string(), amount: z.string()}))})}), primary_type: z.string()})]), payment_data: z.object({checksum: z.string()}), failed_payments: z.array(z.object({userNanoid: z.string().min(15).max(15), error: z.string()})), duplicates: z.object({payments: z.array(z.object({to: z.string().min(15).max(15), amount_cents: z.number().int().gte(100).lte(10000000000), currency_symbol: z.enum(["AED", "ARS", "AUD", "BDT", "BIF", "BOB", "BRL", "BTC", "CAD", "CDF", "CHF", "CLP", "CNY", "COP", "CRC", "CZK", "DKK", "EGP", "ETB", "EUR", "GBP", "GHS", "GMD", "GTQ", "HKD", "HUF", "IDR", "ILS", "INR", "JOD", "JPY", "KES", "KRW", "KWD", "LKR", "MAD", "MMK", "MNT", "MWK", "MXN", "MYR", "MZN", "NGN", "NOK", "NPR", "NZD", "PEN", "PHP", "PKR", "PLN", "QAR", "RON", "RWF", "SEK", "SGD", "THB", "TRY", "TZS", "UGX", "USD", "USDC", "USDT", "UYU", "UZS", "VND", "XAF", "XOF", "ZAR"]), invoice_description: z.string().max(5000).optional()})), days_checked: z.number().int().gte(-9007199254740991).lte(9007199254740991)})})}); 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 typed data for payments * * POST /v2/payments */ export const createApiV2Payments = async (args: CreateApiV2PaymentsArgs, options?: CreateApiV2PaymentsOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", 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 (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payments${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(createApiV2PaymentsResponse, await res.json()) };