import {z} from 'zod' export type GetApiV3BatchpaymentIdBatchIdArgs = {batchId: number}; export type GetApiV3BatchpaymentIdBatchIdOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV3BatchpaymentIdBatchIdResponse = z.object({records: z.array(z.object({created: z.string().optional(), paymentToken: z.string().optional(), id: z.number().int().optional(), creatorName: z.string().optional(), merchantId: z.number().int().optional(), batch: z.string().optional(), batchId: z.number().int().optional(), tenderType: z.string().optional(), currency: z.string().optional(), amount: z.string().optional(), cardAccount: z.object({cardType: z.string().optional(), entryMode: z.string().optional(), last4: z.string().optional(), cardId: z.string().optional(), token: z.string().optional(), expiryMonth: z.string().optional(), expiryYear: z.string().optional(), hasContract: z.boolean().optional(), cardPresent: z.boolean().optional()}).optional(), authOnly: z.boolean().optional(), authCode: z.string().optional(), status: z.string().optional(), risk: z.object({avsResponseCode: z.string().optional(), avsZipMatch: z.boolean().optional()}).optional(), requireSignature: z.boolean().optional(), settledAmount: z.string().optional(), settledCurrency: z.string().optional(), settledDate: z.string().optional(), cardPresent: z.boolean().optional(), authMessage: z.string().optional(), availableAuthAmount: z.string().optional(), reference: z.string().optional(), tax: z.string().optional(), invoice: z.string().optional(), customerCode: z.string().optional(), customerName: z.string().optional(), clientReference: z.string().optional(), type: z.string().optional(), taxExempt: z.boolean().optional(), reviewIndicator: z.number().int().optional(), source: z.string().optional()})).optional(), recordCount: z.number().int().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 }; /** * Get a Batch's transactions * * GET /v3/batchpayment?id={batchId} */ export const getApiV3BatchpaymentIdBatchId = async (args: GetApiV3BatchpaymentIdBatchIdArgs, options?: GetApiV3BatchpaymentIdBatchIdOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/batchpayment?id=${encodeURIComponent(String(args.batchId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3BatchpaymentIdBatchIdResponse, await res.json()) };