import {z} from 'zod' export type SandboxPayinCompletionRequest = {remitter?: (string | null) | undefined}; export type CreateApiSandboxPayinsPayinIdCompleteArgs = {payinId: string, 'X-Hevn-Account'?: (string | null) | undefined, 'Idempotency-Key'?: (string | null) | undefined, body: SandboxPayinCompletionRequest | null}; export type CreateApiSandboxPayinsPayinIdCompleteOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const payoutStatus = z.enum(["awaitingSignature", "submitted", "settled", "failed", "refunded"]); export const balanceAccount = z.enum(["USDC", "EURC", "RD", "USDC_ARB"]); export const sandboxCreditResponse = z.object({depositId: z.string(), status: payoutStatus, token: balanceAccount, amount: z.string(), currency: z.string(), idempotencyKey: z.string(), payinId: z.string().nullable().optional(), transactionId: z.string().nullable().optional(), credited: z.string().nullable().optional(), fee: z.string().nullable().optional(), feeCurrency: z.string().nullable().optional(), rate: z.string().nullable().optional(), txHash: z.string().nullable().optional(), pollUrl: z.string().nullable().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 }; /** * Emulate the arrival of the funds a pay-in is waiting for * * POST /sandbox/payins/{payinId}/complete */ export const createApiSandboxPayinsPayinIdComplete = async (args: CreateApiSandboxPayinsPayinIdCompleteArgs, options: CreateApiSandboxPayinsPayinIdCompleteOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Hevn-Account'] != null) headers.set("X-Hevn-Account", String(args['X-Hevn-Account'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/sandbox/payins/${encodeURIComponent(String(args.payinId))}/complete${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(sandboxCreditResponse, await res.json()) };