import {z} from 'zod' export type GetApiFdTransactionsArgs = {sort?: string | undefined, search?: string | undefined, from?: number | undefined, size?: number | undefined, groupBy?: string | undefined, type?: string | undefined, minAmount?: string | undefined, maxAmount?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, paymentType?: string | undefined, cardType?: string | undefined, dr_type?: string | undefined, dr_quick?: string | undefined, dr_from?: string | undefined, dr_to?: string | undefined, dr_count?: string | undefined, dr_unit?: string | undefined}; export type GetApiFdTransactionsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiFdTransactionsResponse = z.object({recordCount: z.number().int().optional(), totalRecords: z.number().int().optional(), records: z.array(z.object({locationName: z.string().optional(), accountNumber: z.string().optional(), transactionType: z.string().optional(), cardType: z.string().optional(), cardNumber: z.string().optional(), entryMode: z.string().optional(), qualificationCode: z.string().optional(), authorizationCode: z.string().optional(), amount: z.number().int().optional(), transactionDate: z.string().optional(), entryDate: z.string().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 }; /** * FirstData Transactions Report * * GET /fd/transactions */ export const getApiFdTransactions = async (args: GetApiFdTransactionsArgs, options?: GetApiFdTransactionsOptions) => { 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 (args.sort != null) query.append("sort", String(args.sort)) if (args.search != null) query.append("search", String(args.search)) if (args.from != null) query.append("from", String(args.from)) if (args.size != null) query.append("size", String(args.size)) if (args.groupBy != null) query.append("groupBy", String(args.groupBy)) if (args.type != null) query.append("type", String(args.type)) if (args.minAmount != null) query.append("minAmount", String(args.minAmount)) if (args.maxAmount != null) query.append("maxAmount", String(args.maxAmount)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.paymentType != null) query.append("paymentType", String(args.paymentType)) if (args.cardType != null) query.append("cardType", String(args.cardType)) if (args.dr_type != null) query.append("dr_type", String(args.dr_type)) if (args.dr_quick != null) query.append("dr_quick", String(args.dr_quick)) if (args.dr_from != null) query.append("dr_from", String(args.dr_from)) if (args.dr_to != null) query.append("dr_to", String(args.dr_to)) if (args.dr_count != null) query.append("dr_count", String(args.dr_count)) if (args.dr_unit != null) query.append("dr_unit", String(args.dr_unit)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/fd/transactions${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(getApiFdTransactionsResponse, await res.json()) };