import {z} from 'zod' export type Amount = number; export type Currency = 'EUR'; export type CreateRefundRequest = {amount: Amount, currency: Currency, description?: string | undefined}; export type CreateApiV3PaymentsPaymentIdRefundsArgs = {'payment-id': string, 'Idempotency-Key': string, body: CreateRefundRequest}; export type CreateApiV3PaymentsPaymentIdRefundsOptions = {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 refundDescription = z.string(); export const refundCreationDate = z.string(); export const refundCreationResponse = z.object({refundId: refundId, paymentId: paymentId, status: z.literal("PENDING"), amount: amount, currency: currency, 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 }; /** * Create refund for payment * * POST /v3/payments/{payment-id}/refunds */ export const createApiV3PaymentsPaymentIdRefunds = async (args: CreateApiV3PaymentsPaymentIdRefundsArgs, options?: CreateApiV3PaymentsPaymentIdRefundsOptions) => { const {baseUrl = "https://merchant.api.preprod.bancontact.net", fetch: fetchFn = fetch, JWSRequestSignatureRefund} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (JWSRequestSignatureRefund !== undefined) headers.set("Signature", JWSRequestSignatureRefund) const url = `${baseUrl.replace(/\/+$/, '')}/v3/payments/${encodeURIComponent(String(args['payment-id']))}/refunds${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(refundCreationResponse, await res.json()) };