import {z} from 'zod' export type GetApiV2PaymentsPaymentIdChargebacksChargebackIdArgs = {embed?: (string | null) | undefined, testmode?: boolean | undefined, 'idempotency-key'?: string | undefined}; export type GetApiV2PaymentsPaymentIdChargebacksChargebackIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const chargebackToken = z.string(); export const amount = z.object({currency: z.string(), value: z.string()}); export const paymentToken = z.string(); export const settlementToken = z.string(); export const createdAt = z.string(); export const url = z.object({href: z.string(), type: z.string()}); export const urlNullable = z.object({href: z.string().optional(), type: z.string().optional()}).nullable(); export const entityChargeback = z.object({resource: z.string(), id: chargebackToken, amount: amount, reason: z.object({code: z.string(), description: z.string()}).nullable().optional(), paymentId: paymentToken, settlementId: settlementToken.nullable().optional(), createdAt: createdAt, reversedAt: z.string().nullable().optional(), _links: z.object({self: url, payment: url, settlement: urlNullable.optional(), documentation: url})}); 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 chargeback * * GET /v2/payments/{paymentId}/chargebacks/{chargebackId} * * Also accepts auth: advancedAccessToken, oAuth */ export const getApiV2PaymentsPaymentIdChargebacksChargebackId = async (args: GetApiV2PaymentsPaymentIdChargebacksChargebackIdArgs, options?: GetApiV2PaymentsPaymentIdChargebacksChargebackIdOptions) => { const {baseUrl = "https://api.mollie.com", 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 (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payments/${encodeURIComponent(String(args.paymentId))}/chargebacks/${encodeURIComponent(String(args.chargebackId))}${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(entityChargeback, await res.json()) };