import {z} from 'zod' export type GetApiV3PaymentsPaymentIdRefundsRefundIdArgs = {'payment-id': string, 'refund-id': string}; export type GetApiV3PaymentsPaymentIdRefundsRefundIdOptions = {baseUrl?: string | undefined, JWSRequestSignatureRefund?: string | undefined, fetch?: typeof fetch | undefined}; export const refundId = z.string().min(24).max(24); export const paymentId = z.string().min(24).max(24); export const amount = z.number().int().gt(1).lte(999999999999); export const currency = z.literal("EUR"); export const refundStatus = z.enum(["PENDING", "REFUNDED", "FAILED"]); export const refundDescription = z.string(); export const refundCreationDate = z.string(); export const refundModel = z.object({refundId: refundId, paymentId: paymentId, amount: amount, currency: currency, status: refundStatus, description: refundDescription.optional(), creationDate: refundCreationDate}); 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 refund by id * * GET /v3/payments/{payment-id}/refunds/{refund-id} */ export const getApiV3PaymentsPaymentIdRefundsRefundId = async (args: GetApiV3PaymentsPaymentIdRefundsRefundIdArgs, options?: GetApiV3PaymentsPaymentIdRefundsRefundIdOptions) => { const {baseUrl = "https://merchant.api.preprod.bancontact.net", fetch: fetchFn = fetch, JWSRequestSignatureRefund} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (JWSRequestSignatureRefund !== undefined) headers.set("Signature", JWSRequestSignatureRefund) const url = `${baseUrl.replace(/\/+$/, '')}/v3/payments/${encodeURIComponent(String(args['payment-id']))}/refunds/${encodeURIComponent(String(args['refund-id']))}${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(refundModel, await res.json()) };