import {z} from 'zod' export type PatchApiCheckoutSessionsCheckoutSessionArgs = {checkout_session: string, 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}}; export type PatchApiCheckoutSessionsCheckoutSessionOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiCheckoutSessionsCheckoutSessionResponse = z.object({merchant_order_id: z.string().optional(), checkout_session: z.string().optional(), country: z.string().optional(), payment_description: z.string().optional(), callback_url: z.unknown(), 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(), used: z.boolean().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 }; /** * Copy of Create Checkout Session * * PATCH /checkout/sessions/{checkout_session} */ export const patchApiCheckoutSessionsCheckoutSession = async (args: PatchApiCheckoutSessionsCheckoutSessionArgs, options?: PatchApiCheckoutSessionsCheckoutSessionOptions) => { 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/${encodeURIComponent(String(args.checkout_session))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiCheckoutSessionsCheckoutSessionResponse, await res.json()) };