import {z} from 'zod' export type PaymentId = string; export type RefundType = 'full' | 'partial'; export type Reference = string; export type FullRefundPaymentRequest = {type: RefundType, reference: Reference, webhooks?: {url: string} | undefined}; export type AccountId = string; export type RefundSplitAccount = {id: AccountId, split: {amount: number}}; export type PartialRefundPaymentRequest = {type: 'full' | 'partial', reference: string, amount: number, splits: {accounts: Array}, webhooks?: {url: string} | undefined}; export type RefundPaymentRequest = FullRefundPaymentRequest | PartialRefundPaymentRequest; export type CreateApiV2PaymentsPaymentIdRefundsArgs = {payment_id: PaymentId, 'Getpaid-Idempotency-Key': string, body: RefundPaymentRequest}; export type CreateApiV2PaymentsPaymentIdRefundsOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const refundId = z.string().min(30).max(50); export const refundType = z.enum(["full", "partial"]); export const refundStatus = z.enum(["initiated", "completed", "declined", "failed"]); export const reference = z.string().min(1).max(50); export const createdAt = z.string().min(24).max(24); export const updatedAt = z.string().min(24).max(24); export const accountId = z.string().min(30).max(50); export const refundSplitAccount = z.object({id: accountId, split: z.object({amount: z.number().int()})}); export const fullRefundPaymentResponse = z.object({id: refundId, type: refundType, status: refundStatus, reference: reference, created_at: createdAt, updated_at: updatedAt, amount: z.number().int(), splits: z.object({accounts: z.array(refundSplitAccount)})}); export const partialRefundPaymentResponse = z.object({id: refundId, type: refundType, status: refundStatus, reference: reference, created_at: createdAt, updated_at: updatedAt}); export const refundPaymentResponse = z.discriminatedUnion("type", [fullRefundPaymentResponse, partialRefundPaymentResponse]); 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 }; /** * Refund a payment * * POST /v2/payments/{payment_id}/refunds */ export const createApiV2PaymentsPaymentIdRefunds = async (args: CreateApiV2PaymentsPaymentIdRefundsArgs, options?: CreateApiV2PaymentsPaymentIdRefundsOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Getpaid-Idempotency-Key'] != null) headers.set("Getpaid-Idempotency-Key", String(args['Getpaid-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(refundPaymentResponse, await res.json()) };