import {z} from 'zod' export type Limit = number; export type Offset = number; export type Sort = string; export type GetApiFundingsArgs = {fundedAfter?: string | undefined, fundedBefore?: string | undefined, limit?: Limit | undefined, offset?: Offset | undefined, sort?: Sort | undefined, fundingDetailId?: string | undefined, 'Partner-Code'?: string | undefined, 'Merchant-Id'?: string | undefined}; export type GetApiFundingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const recordId = z.string(); export const externalMerchantId = z.string().max(30); export const merchantTradeName = z.string(); export const allianceCode = z.string().min(1).max(50).nullable(); export const funding = z.object({id: recordId, funded: z.string(), posted: z.string(), status: z.enum(["RELEASED", "HELD", "UNKNOWN"]), type: z.object({code: z.string(), description: z.string()}), merchant: z.object({id: externalMerchantId, name: merchantTradeName, allianceCode: allianceCode, partnerCode: z.string().min(1).max(50).nullable()}), account: z.object({id: z.string(), reference: z.string().nullable(), iban: z.string().nullable(), currencyCode: z.string().min(3).max(3).nullable(), type: z.object({code: z.string().nullable(), description: z.string()})}), financial: z.object({netAmount: z.string()}), meta: z.object({paymentReference: z.string().nullable()}), _links: z.object({transactions: z.object({href: z.string().optional()}).optional(), "funding-details": z.object({href: z.string().optional()}).optional()}).optional()}); export const getApiFundingsResponse = z.array(funding); 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 }; /** * List Fundings * * GET /fundings */ export const getApiFundings = async (args: GetApiFundingsArgs, options?: GetApiFundingsOptions) => { const {baseUrl = "https://prod.emea.api.fiservapps.com/ipp/omnipay/acquirerprofile/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fundedAfter != null) query.append("fundedAfter", String(args.fundedAfter)) if (args.fundedBefore != null) query.append("fundedBefore", String(args.fundedBefore)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.fundingDetailId != null) query.append("fundingDetailId", String(args.fundingDetailId)) if (args['Partner-Code'] != null) headers.set("Partner-Code", String(args['Partner-Code'])) if (args['Merchant-Id'] != null) headers.set("Merchant-Id", String(args['Merchant-Id'])) const url = `${baseUrl.replace(/\/+$/, '')}/fundings${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(getApiFundingsResponse, await res.json()) };