import {z} from 'zod' export type EmbeddedCustomer = {email?: string | undefined, name?: string | undefined, surname?: string | undefined, phone?: string | undefined, address?: string | undefined, postal_code?: string | undefined, city?: string | undefined, country?: string | undefined, identity_number?: string | undefined, ip_address?: string | undefined, customer_id?: string | undefined}; export type EmbeddedBasketItem = {id: string, name: string, brand_name?: string | undefined, qty?: number | undefined, unit_price?: number | undefined, category?: number | undefined}; export type EmbeddedSessionRequest = {merchant_api_key: string, amount: number, currency?: string | undefined, language?: string | undefined, customer?: EmbeddedCustomer | undefined, basket_items?: Array | undefined, customer_basket?: Array> | undefined, submerchant_external_id?: string | undefined, is_submerchant_payment?: boolean | undefined, campaign_name?: string | undefined, billing_tax_office?: string | undefined, billing_tax_number?: string | undefined, success_url?: string | undefined, fail_url?: string | undefined, metadata?: Record | undefined, execution_mode?: string | undefined, sandbox_scenario?: string | undefined}; export type CreateApiApiV1EmbeddedSessionArgs = {body: EmbeddedSessionRequest}; export type CreateApiApiV1EmbeddedSessionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const embeddedSessionResponse = z.object({status: z.number().int(), message: z.string(), session_id: z.string().optional(), checkout_url: z.string().optional(), expires_at: 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 Session * * POST /api/v1/embedded/session */ export const createApiApiV1EmbeddedSession = async (args: CreateApiApiV1EmbeddedSessionArgs, options?: CreateApiApiV1EmbeddedSessionOptions) => { const {baseUrl = "https://api.paylox.io", 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(/\/+$/, '')}/api/v1/embedded/session${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(embeddedSessionResponse, await res.json()) };