import {z} from 'zod' export type GetApiV3ReconciliationPaymentsArgs = {'payout-id'?: string | undefined, size?: number | undefined, page?: number | undefined, 'start-date'?: string | undefined, 'end-date'?: string | undefined}; export type GetApiV3ReconciliationPaymentsOptions = {baseUrl?: string | undefined, JWSRequestSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentChannel = z.enum(["ONLINE", "INSTORE", "INVOICE"]); export const currency = z.literal("EUR"); export const paymentDetails = z.object({paymentId: z.string().min(24).max(24), payoutId: z.string().min(24).max(24).optional(), paymentProfileId: z.string().min(24).max(24), merchantName: z.string().min(1), paymentChannel: paymentChannel, currency: currency, amount: z.number().int().gte(1), reference: z.string().min(1).optional(), description: z.string().min(1).optional(), transactionDate: z.string()}); export const payoutPaymentsResponse = z.object({size: z.number().int().gte(0), totalPages: z.number().int(), totalElements: z.number().int(), number: z.number().int(), payments: z.array(paymentDetails)}); 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 list of payments * * GET /v3/reconciliation/payments */ export const getApiV3ReconciliationPayments = async (args: GetApiV3ReconciliationPaymentsArgs, options?: GetApiV3ReconciliationPaymentsOptions) => { const {baseUrl = "https://merchant.api.preprod.bancontact.net", fetch: fetchFn = fetch, JWSRequestSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['payout-id'] != null) query.append("payout-id", String(args['payout-id'])) if (args.size != null) query.append("size", String(args.size)) if (args.page != null) query.append("page", String(args.page)) if (args['start-date'] != null) query.append("start-date", String(args['start-date'])) if (args['end-date'] != null) query.append("end-date", String(args['end-date'])) if (JWSRequestSignature !== undefined) headers.set("Signature", JWSRequestSignature) const url = `${baseUrl.replace(/\/+$/, '')}/v3/reconciliation/payments${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(payoutPaymentsResponse, await res.json()) };