import {z} from 'zod' export type GetApi3dSecureSessions3dSecureSessionIdArgs = {'Finix-Version'?: string | undefined}; export type GetApi3dSecureSessions3dSecureSessionIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createdAt = z.string(); export const updatedAt = z.string(); export const currency = z.enum(["CAD", "USD"]); export const tags = z.record(z.string(), z.unknown()).nullable(); export const schema3dSecureSession = z.object({id: z.string().optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), "3d_secure_authentication": z.object({cardholder_authentication: z.string().optional(), electronic_commerce_indicator: z.enum(["AUTHENTICATED", "ATTEMPTED", "NON_AUTHENTICATED"]).optional(), transaction_id: z.string().max(36).optional()}).nullable().optional(), "3d_secure_authentication_session_id": z.string().nullable().optional(), "3d_secure_authentication_url": z.string().nullable().optional(), "3d_secure_version": z.string().nullable().optional(), amount: z.number().int().optional(), authentication_type: z.enum(["null", "FRICTIONLESS", "CHALLENGE", "ATTEMPT"]).nullable().optional(), currency: currency.optional(), failure_code: z.enum(["null", "AUTHENTICATION_FAILED", "AUTHENTICATION_TIMED_OUT", "CARD_NOT_ENROLLED", "CHALLENGE_REQUIRED", "DIRECTORY_SERVER_UNAVAILABLE", "ISSUER_SERVER_UNAVAILABLE", "PROTOCOL_ERROR", "SUSPECTED_FRAUD", "TRANSACTION_CANCELLED_BY_CARDHOLDER", "TRANSACTION_NOT_PERMITTED", "TRANSIENT_SYSTEM_FAILURE", "UNSUPPORTED_3D_SECURE_VERSION"]).nullable().optional(), failure_message: z.string().nullable().optional(), merchant_id: z.string().optional(), payment_instrument_id: z.string().nullable().optional(), redirect_url: z.string().nullable().optional(), state: z.enum(["PENDING", "SUCCEEDED", "FAILED"]).optional(), tags: tags.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 }; /** * Fetch a 3D Secure Session * * GET /3d_secure_sessions/{3d_secure_session_id} */ export const getApi3dSecureSessions3dSecureSessionId = async (args: GetApi3dSecureSessions3dSecureSessionIdArgs, options?: GetApi3dSecureSessions3dSecureSessionIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/3d_secure_sessions/${encodeURIComponent(String(args['3d_secure_session_id']))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(schema3dSecureSession, await res.json()) };