import {z} from 'zod' export type CreateApiRefundsIdVoidArgs = {id: string}; export type CreateApiRefundsIdVoidOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const refundStatus = z.enum(["pending", "succeeded", "failed", "voided"]); export const refundCustomer = z.object({id: z.string().optional(), email: z.string().optional(), full_name: z.string().optional()}); export const refundTransferAdjustmentAction = z.enum(["cancel", "reverse"]); export const refundTransferAdjustment = z.object({action: refundTransferAdjustmentAction, transfer_setup_id: z.string(), reversal_transfer_id: z.string().nullable().optional(), restoration_transfer_id: z.string().nullable().optional(), restored_at: z.string().nullable().optional(), amount_in_cents: z.number().int().gte(1)}); export const refund = z.object({id: z.string().optional(), status: refundStatus.optional(), failure_reason: z.string().nullable().optional(), customer: refundCustomer.optional(), user_id: z.string().optional(), account_id: z.string().optional(), account_refunded_amount_in_cents: z.number().int().optional(), customer_refunded_amount_in_cents: z.number().int().optional(), refund_application_fee: z.boolean().optional(), transfer_adjustments: z.array(refundTransferAdjustment).optional(), currency: z.string().optional(), created_at: z.string().optional(), voided_at: z.string().nullable().optional()}); 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 }; /** * Anular un reembolso * * POST /refunds/{id}/void */ export const createApiRefundsIdVoid = async (args: CreateApiRefundsIdVoidArgs, options?: CreateApiRefundsIdVoidOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/refunds/${encodeURIComponent(String(args.id))}/void${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(refund, await res.json()) };