import {z} from 'zod' export type RecordId = string; export type GetApiFundingsIdDetailsArgs = {id: RecordId}; export type GetApiFundingsIdDetailsOptions = {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 paymentInstrumentCategory = z.enum(["CARD", "CASH", "ALTERNATIVE"]); export const fundingDetail = z.object({id: recordId, posted: z.string(), type: z.object({code: z.string(), description: z.string()}), merchant: z.object({id: externalMerchantId, name: merchantTradeName}), paymentInstrument: z.object({category: paymentInstrumentCategory, brand: z.enum(["AMEX", "CUP", "DINERS", "GIRO", "JCB", "MAESTRO", "MC", "PAGO", "PP", "PRIV", "VISA", "VPAY", "GIFT", "C_EGIFT", "CARTBLANC", "RUPAY"]).optional(), service: z.enum(["BLIK", "ELV", "IDEAL", "KLARNA", "PAYPAL", "PBL", "SEPADD", "SOFORT", "TOPUP", "EASYCASH", "VIPP", "MOBILEPAY", "TWINT", "SWISH", "ALIPAY", "WECHATPAY", "POSTFINANCE", "OTHER"]).optional()}), financial: z.object({currencyCode: z.string().min(3).max(3).nullable(), slips: z.number().int(), grossAmount: z.string(), netAmount: z.string(), fxRate: z.string().nullable()}), meta: z.object({paymentReference: z.string().nullable(), detailReference: z.string().nullable()}), _links: z.object({transactions: z.object({href: z.string().optional()}).optional(), funding: z.object({href: z.string().optional()}).optional()}).optional()}); export const getApiFundingsIdDetailsResponse = z.array(fundingDetail); 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 funding details * * GET /fundings/{id}/details */ export const getApiFundingsIdDetails = async (args: GetApiFundingsIdDetailsArgs, options?: GetApiFundingsIdDetailsOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/fundings/${encodeURIComponent(String(args.id))}/details${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(getApiFundingsIdDetailsResponse, await res.json()) };