import {z} from 'zod' export type CreateApiCheckoutSessionsArgs = {body: {customer_id?: string | undefined, merchant_order_id: string, payment_description: string, callback_url?: string | undefined, country: 'AR' | 'BO' | 'BR' | 'CL' | 'CO' | 'CR' | 'EC' | 'SV' | 'GT' | 'HN' | 'MX' | 'NI' | 'PA' | 'PY' | 'PE' | 'US' | 'UY', account_id: string, amount?: {currency: string, value: number} | undefined, alternative_amount?: ({currency: string | null, value: number}) | undefined, metadata?: Array<{key: string, value: string}> | undefined, workflow?: ('SDK_CHECKOUT' | 'CHECKOUT' | 'SDK_SEAMLESS') | undefined, installments?: ({plan_id?: string | undefined, plan?: Array<{installment: number, rate: number}> | undefined}) | undefined, recurring_payment?: ({description?: string | undefined, management_url?: string | undefined, billing_agreement?: string | undefined, regular_billing?: ({label: string, amount: string, interval_unit: 'year' | 'month' | 'day' | 'hour' | 'minute', interval_count: number}) | undefined, trial_billing?: ({label: string, amount: string, interval_unit: 'year' | 'month' | 'day' | 'hour' | 'minute', interval_count: number}) | undefined}) | undefined, checkout_id?: string | undefined}}; export type CreateApiCheckoutSessionsOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCheckoutSessionsResponse = z.union([z.object({merchant_order_id: z.string().optional(), checkout_session: z.string().optional(), country: z.string().optional(), payment_description: z.string().optional(), customer_id: z.string().optional(), callback_url: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), created_at: z.string().optional(), metadata: z.unknown(), workflow: z.string().optional(), installments: z.object({plan_id: z.unknown(), plan: z.unknown()}).optional(), checkout_id: z.string().optional()}), z.object({merchant_order_id: z.string().optional(), checkout_session: z.string().optional(), country: z.string().optional(), payment_description: z.string().optional(), customer_id: z.string().optional(), callback_url: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), created_at: z.string().optional(), metadata: z.array(z.object({key: z.string().optional(), value: z.string().optional()})).optional(), workflow: z.string().optional(), installments: z.object({plan_id: z.unknown(), plan: z.unknown()}).optional(), checkout_id: 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 Checkout Session * * POST /checkout/sessions */ export const createApiCheckoutSessions = async (args: CreateApiCheckoutSessionsArgs, options?: CreateApiCheckoutSessionsOptions) => { 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(/\/+$/, '')}/checkout/sessions${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(createApiCheckoutSessionsResponse, await res.json()) };