import {z} from 'zod' export type Currency = string; export type Decimal = number; export type Customer = {name: string, document_type?: (string | null) | undefined, document_number?: (string | null) | undefined, email?: (string | null) | undefined}; export type IntentDataRequest = {destination_id: string, external_id: string, concept?: string | undefined, currency: Currency, amount: Decimal, payment_method?: ('transfer' | 'qr') | undefined, customer: Customer | string}; export type CreateApiPayinIntentArgs = {body: IntentDataRequest}; export type CreateApiPayinIntentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; 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 virtualAccountDetails = z.object({account_format: z.enum(["clabe", "bre_b", "alias"]), account_number: z.string(), name: z.string().max(50).optional(), active: z.boolean().optional(), branch: z.string().optional(), bank: bank.optional()}); export const qRCodeDetails = z.object({emv_code: z.string(), img: z.string(), external_link: z.string().optional(), expire_date: z.string().optional()}); export const payinCustomer = 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(), virtual_account: virtualAccountDetails.optional(), qr_code: qRCodeDetails.optional()}); export const intentDataResponse = z.object({id: z.string(), customer: payinCustomer}); 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 payment intent * * POST /payin/intent */ export const createApiPayinIntent = async (args: CreateApiPayinIntentArgs, options: CreateApiPayinIntentOptions) => { 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/intent${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(intentDataResponse, await res.json()) };