import {z} from 'zod' export type GetApiPaymentsPaymentIdRefundsRefundIdArgs = {embed?: 'payment' | undefined, testmode?: boolean | undefined}; export type GetApiPaymentsPaymentIdRefundsRefundIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentsPaymentIdRefundsRefundIdResponse = z.object({resource: z.string(), id: z.string(), mode: z.string(), description: z.string().max(255).nullable(), amount: z.object({currency: z.string(), value: z.string()}), settlementAmount: z.object({currency: z.string(), value: z.string()}).nullable().optional(), metadata: z.union([z.string(), z.number(), z.record(z.string(), z.unknown()), z.array(z.string())]).nullable(), paymentId: z.string(), settlementId: z.string().nullable().optional(), status: z.enum(["queued", "pending", "processing", "refunded", "failed", "canceled"]), createdAt: z.string(), externalReference: z.object({type: z.string().optional(), id: z.string().optional()}).optional(), reverseRouting: z.boolean().nullable().optional(), routingReversals: z.array(z.object({amount: z.object({currency: z.string(), value: z.string()}).optional(), source: z.object({type: z.string().optional(), organizationId: z.string().optional()}).optional()})).nullable().optional(), testmode: z.boolean().nullable().optional(), _links: z.object({self: z.object({href: z.string(), type: z.string()}), payment: z.object({href: z.string(), type: z.string()}), settlement: z.object({href: z.string().optional(), type: z.string().optional()}).nullable().optional(), documentation: z.object({href: z.string(), type: z.string()})})}); 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 payment refund * * GET /payments/{paymentId}/refunds/{refundId} * * Also accepts auth: advancedAccessToken, oAuth */ export const getApiPaymentsPaymentIdRefundsRefundId = async (args: GetApiPaymentsPaymentIdRefundsRefundIdArgs, options?: GetApiPaymentsPaymentIdRefundsRefundIdOptions) => { const {baseUrl = "https://api.mollie.com/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (args.embed != null) query.append("embed", String(args.embed)) if (args.testmode != null) query.append("testmode", String(args.testmode)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.paymentId))}/refunds/${encodeURIComponent(String(args.refundId))}${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(getApiPaymentsPaymentIdRefundsRefundIdResponse, await res.json()) };