import {z} from 'zod' export type PaymentCredential = {type: string} | {[key: string]: unknown}; export type EvmAuthCaptureEscrowInstrument = {id: string, handler_id: string, type: 'evm_auth_capture_escrow', billing_address?: ({extended_address?: string | undefined, street_address?: string | undefined, address_locality?: string | undefined, address_region?: string | undefined, address_country?: string | undefined, postal_code?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: string | undefined}) | undefined, credential?: PaymentCredential | undefined, display?: {[key: string]: unknown} | undefined, payment_info_hash: string, operator: string, payer: string, receiver: string, token: {address?: string | undefined, symbol: string, decimals: number}, max_amount: {value: string, currency: {chain_id: number, address: string, decimals: number}}, preapproval_expires_at: string, authorization_expires_at: string, refund_expires_at: string, nonce: string, chain_id: number, contract: string, amount: {value: string, currency: {chain_id: number, address: string, decimals: number}}} | {[key: string]: unknown}; export type CreateApiPaymentInstrumentsArgs = {body: EvmAuthCaptureEscrowInstrument}; export type CreateApiPaymentInstrumentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentCredential = z.object({type: z.string()}).and(z.record(z.string(), z.unknown())); export const evmAuthCaptureEscrowInstrument = z.object({id: z.string(), handler_id: z.string(), type: z.literal("evm_auth_capture_escrow"), billing_address: z.object({extended_address: z.string().optional(), street_address: z.string().optional(), address_locality: z.string().optional(), address_region: z.string().optional(), address_country: z.string().optional(), postal_code: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), phone_number: z.string().optional()}).optional(), credential: paymentCredential.optional(), display: z.record(z.string(), z.unknown()).optional(), payment_info_hash: z.string(), operator: z.string(), payer: z.string(), receiver: z.string(), token: z.object({address: z.string().optional(), symbol: z.string(), decimals: z.number().int().gte(0).lte(255)}), max_amount: z.object({value: z.string(), currency: z.object({chain_id: z.number().int().gte(1), address: z.string(), decimals: z.number().int().gte(0).lte(255)})}), preapproval_expires_at: z.string(), authorization_expires_at: z.string(), refund_expires_at: z.string(), nonce: z.string(), chain_id: z.number().int().gte(1), contract: z.string(), amount: z.object({value: z.string(), currency: z.object({chain_id: z.number().int().gte(1), address: z.string(), decimals: z.number().int().gte(0).lte(255)})})}).and(z.record(z.string(), z.unknown())); 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 }; /** * Register payment instrument * * POST /payment-instruments */ export const createApiPaymentInstruments = async (args: CreateApiPaymentInstrumentsArgs, options?: CreateApiPaymentInstrumentsOptions) => { const {baseUrl = "http://localhost:8000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/payment-instruments${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(evmAuthCaptureEscrowInstrument, await res.json()) };