import {z} from 'zod' export type BrowserInformation = {color_depth?: (number | null) | undefined, java_enabled?: (boolean | null) | undefined, java_script_enabled?: (boolean | null) | undefined, language?: (string | null) | undefined, screen_height?: (number | null) | undefined, screen_width?: (number | null) | undefined, time_zone?: (number | null) | undefined, ip_address?: (string | null) | undefined, accept_header?: (string | null) | undefined, user_agent?: (string | null) | undefined, os_type?: (string | null) | undefined, os_version?: (string | null) | undefined, device_model?: (string | null) | undefined, accept_language?: (string | null) | undefined, referer?: (string | null) | undefined}; export type VisaEligibilityCheckData = {consumerPresent: boolean, consumerStatus?: (string | null) | undefined, customData?: (BrowserInformation | null) | undefined}; export type MasterCardEligibilityCheckData = {consumerPresent: boolean, idLookupSessionId?: (string | null) | undefined, lastUsedCardTimestamp?: (string | null) | undefined}; export type ClickToPayEligibilityCheckData = {visa?: (VisaEligibilityCheckData | null) | undefined, mastercard?: (MasterCardEligibilityCheckData | null) | undefined}; export type AuthenticationEligibilityCheckData = {click_to_pay: ClickToPayEligibilityCheckData}; export type AuthenticationEligibilityCheckRequest = {client_secret?: (string | null) | undefined, eligibility_check_data: AuthenticationEligibilityCheckData}; export type CreateApiAuthenticationAuthenticationIdEligibilityCheckArgs = {body: AuthenticationEligibilityCheckRequest}; export type CreateApiAuthenticationAuthenticationIdEligibilityCheckOptions = {baseUrl: string, publishableKey?: string | undefined, fetch?: typeof fetch | undefined}; export const authenticationSdkNextAction = z.union([z.literal("await_merchant_callback"), z.object({deny: z.object({message: z.string()})}), z.literal("proceed")]); export const authenticationEligibilityCheckResponse = z.object({authentication_id: z.string(), sdk_next_action: authenticationSdkNextAction}); 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 }; /** * Authentication - POST Eligibility Check * * POST /authentication/{authentication_id}/eligibility-check */ export const createApiAuthenticationAuthenticationIdEligibilityCheck = async (args: CreateApiAuthenticationAuthenticationIdEligibilityCheckArgs, options: CreateApiAuthenticationAuthenticationIdEligibilityCheckOptions) => { const {baseUrl, fetch: fetchFn = fetch, publishableKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publishableKey !== undefined) headers.set("api-key", publishableKey) const url = `${baseUrl.replace(/\/+$/, '')}/authentication/${encodeURIComponent(String(args.authentication_id))}/eligibility-check${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(authenticationEligibilityCheckResponse, await res.json()) };