import {z} from 'zod' export type DeviceDataInfo = {browser_java_enabled?: boolean | undefined, browser_header?: string | undefined, browser_language?: string | undefined, browser_color_depth?: string | undefined, browser_screen_height?: string | undefined, browser_screen_width?: string | undefined, browser_time_zone?: string | undefined, user_agent?: string | undefined, ip_address?: string | undefined, browser_javascript_enabled?: boolean | undefined}; export type LookupRequest = {account_age_indicator?: ('01' | '02' | '03' | '04' | '05') | undefined, account_change_date?: string | undefined, account_change_indicator?: ('01' | '02' | '03' | '04') | undefined, account_create_date?: string | undefined, account_id?: string | undefined, account_purchases?: number | undefined, account_pwd_change_date?: string | undefined, account_pwd_change_indicator?: ('01' | '02' | '03' | '04' | '05') | undefined, acs_window_size?: ('01' | '02' | '03' | '04' | '05') | undefined, add_card_attempts?: number | undefined, address_match?: ('Y' | 'N') | undefined, alias?: string | undefined, alternate_authentication_data?: string | undefined, alternate_authentication_date?: string | undefined, alternate_authentication_method?: ('01' | '02' | '03' | '04' | '05' | '06') | undefined, authentication_indicator?: ('01' | '02' | '03' | '04' | '05' | '06') | undefined, amount: number, billing_first_name: string, billing_last_name: string, billing_phone?: string | undefined, billing_address_1: string, billing_address_2?: string | undefined, billing_address_3?: string | undefined, billing_city: string, billing_postal_code?: string | undefined, billing_state?: string | undefined, billing_country_code: string, card?: string | undefined, encrypted_card?: string | undefined, public_key_alias?: string | undefined, reuse_token?: string | undefined, currency_code: string, card_type?: ('VSA' | 'MSC' | 'VSD' | 'VSE' | 'MAE' | 'AMX' | 'DSC' | 'DIN' | 'CBLA' | 'JCB' | 'ENR' | 'JAL' | 'CTB' | 'DNK' | 'CSI' | 'EAN' | 'UATP' | 'MAEI' | 'CB') | undefined, category_code?: number | undefined, challenge_indicator?: ('01' | '02' | '03' | '04' | '90') | undefined, device_info_id: string, delivery_email?: string | undefined, delivery_time_frame?: ('01' | '02' | '03' | '04') | undefined, device_channel?: ('SDK' | 'Browser' | '3RI') | undefined, device_data_info?: DeviceDataInfo | undefined, email: string, fraud_activity?: ('01' | '02') | undefined, gift_card_amount?: number | undefined, gift_card_count?: number | undefined, gift_card_currency_code?: string | undefined, installment?: number | undefined, merchant_reference: string, merchant_score?: string | undefined, message_category?: ('01' | '02') | undefined, phone_number?: string | undefined, payment_account_age?: string | undefined, payment_account_indicator?: ('01' | '02' | '03' | '04' | '05') | undefined, payment_use_case?: ('01' | '02' | '03' | '04' | '05') | undefined, pre_order_date?: string | undefined, pre_order_indicator?: ('01' | '02') | undefined, prior_authentication_data?: string | undefined, prior_authentication_method?: ('01' | '02' | '03' | '04') | undefined, prior_authentication_ref?: string | undefined, prior_authentication_time?: string | undefined, product_code?: ('PHY' | 'CHA' | 'ACF' | 'QCT' | 'PAL') | undefined, purchase_date?: string | undefined, recurring_end?: string | undefined, recurring_frequency?: number | undefined, reorder_indicator?: ('01' | '02') | undefined, requestor_id?: string | undefined, requestor_name?: string | undefined, sdk_max_timeout?: number | undefined, shipping_address_1?: string | undefined, shipping_address_2?: string | undefined, shipping_address_3?: string | undefined, shipping_address_usage_date?: string | undefined, shipping_address_usage_indicator?: ('01' | '02' | '03' | '04') | undefined, shipping_city?: string | undefined, shipping_country_code?: string | undefined, shipping_method_indicator?: ('01' | '02' | '03' | '04' | '05' | '06' | '07') | undefined, shipping_name_indicator?: ('01' | '02') | undefined, shipping_postal_code?: string | undefined, shipping_state?: string | undefined, threeds_contract_id: string, three_ri_indicator?: ('01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11') | undefined, threeds_version?: '1.0.2' | undefined, token?: string | undefined, transaction_mode?: ('M' | 'P' | 'R' | 'S' | 'T') | undefined, transaction_count_day?: number | undefined, transaction_count_year?: number | undefined, total_items?: string | undefined, work_phone?: string | undefined}; export type CreateApiV2LookupArgs = {body: LookupRequest}; export type CreateApiV2LookupOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const lookupResponse = z.object({acs_rendering_type: z.string().optional(), acs_transaction_id: z.string().optional(), acs_url: z.string().optional(), authentication_id: z.string().optional(), authentication_type: z.string().optional(), authorization_payload: z.string().optional(), cardholder_info: z.string().optional(), cavv: z.string().optional(), cavv_algorithm: z.string().optional(), challenge_required: z.string().optional(), card_brand: z.string().optional(), ds_transaction_id: z.string().optional(), eci_flag: z.string().optional(), enrolled: z.string().optional(), error_desc: z.string().optional(), error_no: z.string().optional(), network_score: z.string().max(2).optional(), order_id: z.string().optional(), pares_status: z.string().optional(), payload: z.string().optional(), reason_code: z.string().optional(), reason_desc: z.string().optional(), signature_verification: z.string().optional(), status_reason: z.string().optional(), third_party_token: z.string().optional(), threeds_version: z.string().optional(), token: z.string().optional(), transaction_id: z.string().optional(), xid: 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 }; /** * Cardholder Authentication API for 3DS * * POST /v2/lookup * * Also accepts auth: BearerAuth */ export const createApiV2Lookup = async (args: CreateApiV2LookupArgs, options?: CreateApiV2LookupOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/lookup${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(lookupResponse, await res.json()) };