import {z} from 'zod' export type Decimal = number; export type CreateApiPayinRefundArgs = {body: {intent_id: string, external_id: string, amount?: Decimal | undefined}}; export type CreateApiPayinRefundOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const decimal = z.number(); export const country = z.string(); export const bank = z.object({code: z.string().optional(), bicfi: z.string().optional(), name: z.string().max(50).optional(), country: country.optional()}); export const withdrawalAccountDetailsResponse = z.object({id: z.string().optional(), account_format: z.enum(["iban", "clabe", "cci", "pix", "bre_b", "alias", "cbu", "cvu"]).optional(), account_number: z.string(), description: z.string().max(50).optional(), selected: z.boolean().optional(), branch: z.string().optional(), bank: bank.optional()}); export const payoutCustomer = z.object({name: z.string().max(50).optional(), tax_id_type: z.enum(["rfc", "cnpj", "cpf", "ruc", "curp", "le", "dni", "lm", "pas", "ce", "nit", "cc", "pa", "ti", "nuip", "ppt", "pep", "cuil", "cuit", "other"]).optional(), tax_id: z.string().optional(), external_id: z.string(), withdrawal_account: withdrawalAccountDetailsResponse.optional()}); export const payoutStatesEnum = z.enum(["created", "initiated", "pending", "failed", "settled", "cancelled"]).max(50); export const payoutTransferState = z.object({state: payoutStatesEnum.optional(), timestamp: z.string().optional(), message: z.string().optional()}); export const payoutTransfer = z.object({id: z.string().optional(), origin: z.string().optional(), description: z.string().max(100).optional(), external_id: z.string().max(50).optional(), amount: decimal.optional(), fee: decimal.optional(), total_amount: decimal.optional(), reference: z.string().max(50).optional(), timestamp: z.string().optional(), customer: payoutCustomer.optional(), events: z.array(payoutTransferState).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 }; /** * Create a refund * * POST /payin/refund */ export const createApiPayinRefund = async (args: CreateApiPayinRefundArgs, options: CreateApiPayinRefundOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/payin/refund${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(payoutTransfer, await res.json()) };