import {z} from 'zod' export type CreateApiPaymentLinksArgs = {body: {account_id: string, description?: string | undefined, country: 'AR' | 'BO' | 'BR' | 'CL' | 'CO' | 'CR' | 'EC' | 'SV' | 'GT' | 'HN' | 'MX' | 'NI' | 'PA' | 'PY' | 'PE' | 'US' | 'UY', merchant_order_id?: string | undefined, amount: {value: number, currency: string}, capture?: boolean | undefined, type?: string | undefined, status?: string | undefined, payment_method?: Record | undefined, installments_plan?: Record | undefined, timezone?: string | undefined, payments_number?: number | undefined, split_payment_methods?: boolean | undefined, taxes?: (Array<{type?: string | undefined, tax_base?: number | undefined, value?: number | undefined, percentage?: number | undefined}>) | undefined, customer_payer?: ({id?: string | undefined, merchant_customer_id?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, gender?: string | undefined, date_of_birth?: string | undefined, email?: string | undefined, nationality?: string | undefined, document?: ({document_number: string, document_type?: ('DNI' | 'CI' | 'LC' | 'LE' | 'CUIT' | 'CUIL' | 'PAS' | 'CPF' | 'RG' | 'CNH' | 'CNPJ' | 'RUT' | 'RUN' | 'CC' | 'CE' | 'NIT' | 'DUI' | 'PIC' | 'DPI' | 'IFR' | 'INE' | 'CP' | 'RFC' | 'CURP' | 'CIP' | 'CIC' | 'CUI' | 'RUC' | '') | undefined}) | undefined, phone?: ({country_code?: string | undefined, number?: string | undefined}) | undefined, billing_address?: ({address_line_1?: string | undefined, address_line_2?: string | undefined, city?: string | undefined, country?: ('AR' | 'BO' | 'BR' | 'CL' | 'CO' | 'CR' | 'EC' | 'SV' | 'GT' | 'HN' | 'MX' | 'NI' | 'PA' | 'PY' | 'PE' | 'US' | 'UY') | undefined, state?: string | undefined, zip_code?: string | undefined}) | undefined, shipping_address?: ({address_line_1?: string | undefined, address_line_2?: string | undefined, city?: string | undefined, country?: ('AR' | 'BO' | 'BR' | 'CL' | 'CO' | 'CR' | 'EC' | 'SV' | 'GT' | 'HN' | 'MX' | 'NI' | 'PA' | 'PY' | 'PE' | 'US' | 'UY') | undefined, state?: string | undefined, zip_code?: string | undefined}) | undefined, ip_address?: string | undefined}) | undefined, additional_data?: ({order?: ({shipping_amount?: number | undefined, fee_amount?: number | undefined, items?: (Array<{id: string, name: string, quantity: number, unit_amount: number, category?: string | undefined, brand?: string | undefined, sku_code?: string | undefined, manufacture_part_number?: string | undefined}>) | undefined}) | undefined, airline?: ({pnr?: string | undefined, legs?: (Array<{departure_airport: string, departure_datetime: string, departure_airport_timezone?: string | undefined, arrival_airport: string, carrier_code?: string | undefined, flight_number?: string | undefined, fare_basis_code?: string | undefined, fare_class_code?: string | undefined, base_fare?: number | undefined, base_fare_currency?: string | undefined, stopover_code?: string | undefined}>) | undefined}) | undefined, device?: ({locale?: string | undefined, geolocation?: string | undefined, event_uuid?: string | undefined, user_agent?: string | undefined}) | undefined, payer_risk_data?: ({login_platform?: string | undefined, approved_transactions_2m?: number | undefined, recent_transactions_3h?: number | undefined, is_paid_user?: boolean | undefined, account_creation_date?: string | undefined}) | undefined, seller_details?: ({name?: string | undefined, email?: string | undefined, reference?: string | undefined, website?: string | undefined, industry?: string | undefined, country?: string | undefined, document?: ({document_number: string, document_type?: ('DNI' | 'CI' | 'LC' | 'LE' | 'CUIT' | 'CUIL' | 'PAS' | 'CPF' | 'RG' | 'CNH' | 'CNPJ' | 'RUT' | 'RUN' | 'CC' | 'CE' | 'NIT' | 'DUI' | 'PIC' | 'DPI' | 'IFR' | 'INE' | 'CP' | 'RFC' | 'CURP' | 'CIP' | 'CIC' | 'CUI' | 'RUC' | '') | undefined}) | undefined, phone?: ({country_code?: string | undefined, number?: string | undefined}) | undefined, address?: ({address_line_1?: string | undefined, address_line_2?: string | undefined, city?: string | undefined, country?: ('AR' | 'BO' | 'BR' | 'CL' | 'CO' | 'CR' | 'EC' | 'SV' | 'GT' | 'HN' | 'MX' | 'NI' | 'PA' | 'PY' | 'PE' | 'US' | 'UY') | undefined, state?: string | undefined, zip_code?: string | undefined}) | undefined}) | undefined}) | undefined, callback_url?: string | undefined, one_time_use?: boolean | undefined, availability?: ({start_at?: string | undefined, finish_at?: string | undefined}) | undefined, payment_method_types: Array, metadata?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, vault_on_success?: boolean | undefined}}; export type CreateApiPaymentLinksOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPaymentLinksResponse = z.object({id: z.string().optional(), country: z.string().optional(), availability: z.object({start_at: z.string().optional(), finish_at: z.string().optional()}).optional(), status: z.string().optional(), merchant_order_id: z.string().optional(), description: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), capture: z.boolean().optional(), metadata: z.unknown(), split_payment_methods: z.boolean().optional(), payment_method_types: z.array(z.string()).optional(), one_time_use: z.boolean().optional(), payments: z.unknown(), callback_url: z.string().optional(), installments_plan: z.unknown(), customer_payer: z.unknown(), taxes: z.unknown(), additional_data: z.object({airline: z.unknown(), order: z.unknown(), seller_details: z.unknown(), device: z.object({locale: z.string().optional(), geolocation: z.string().optional(), event_uuid: z.string().optional(), user_agent: z.string().optional()}).optional(), payer_risk_data: z.object({login_platform: z.string().optional(), approved_transactions_2m: z.number().int().optional(), recent_transactions_3h: z.number().int().optional(), is_paid_user: z.boolean().optional(), account_creation_date: z.string().optional()}).optional()}).optional(), account_id: z.string().optional(), checkout_url: z.string().optional(), payments_number: z.number().int().optional(), merchant_image: z.string().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 Payment Link * * POST /payment-links */ export const createApiPaymentLinks = async (args: CreateApiPaymentLinksArgs, options?: CreateApiPaymentLinksOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/payment-links${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(createApiPaymentLinksResponse, await res.json()) };