import { CheckC06Response } from "./ekycC06Type"; import { CheckFaceResponse } from "./ekycFaceType"; import { CheckLivenessResponse } from "./ekycLivenessType"; import { CheckOcrResponse } from "./ekycOCRType"; import { SDKTransactionResponse } from "./ekycTransactionType"; /** * SDKEnv – môi trường hoạt động của SDK (1.5.4+). * Map native: Android SDKEnv.DEV / PROD. */ export enum SDKEnv { DEV = "DEV", PROD = "PROD", } /** * AppIDType – loại app ID truyền vào SDK. * Map native: Android AppIDType.NONE / AppIDType.HD_BANK / AppIDType.VIKKI, * iOS .none / .hdBank / .vikki */ export enum AppIDType { NONE = "NONE", HD_BANK = "HD_BANK", VIKKI = "VIKKI", } export interface SDKEkycResult { isSdkCheckKey: boolean; nfcResponse?: string; checkC06Response?: CheckC06Response; checkFaceResponse?: CheckFaceResponse; ocrResponse?: CheckOcrResponse; checkLivenessResponse?: CheckLivenessResponse; transactionResponse?: SDKTransactionResponse; } export interface SDKEkycResultWithEvent { data?: SDKEkycResult; event?: string; } export interface SDKEkycResultStringWithEvent { data: string; event: string; } /** * Result from startEkycUI() – maps Android SDK data.ekycStateModel.eKYCFileModel. * Use the same shape as Android: data?.ekycStateModel?.eKYCFileModel?.imageFace. * * @example * const result = await sdkEKYC.startEkycUI(...); * const selfieBase64 = result?.imageFace; // base64 selfie (liveness/face) * const frontBase64 = result?.imageOcrFront; // base64 CCCD/CMND mặt trước * const backBase64 = result?.imageOcrBack; // base64 CCCD/CMND mặt sau * const selfiePath = result?.imageFacePath; // file path (e.g. for FormData) * const transactionId = result?.transactionId; */ export interface StartEkycUIResult { status: 'success'; event: string; data?: string; transactionId?: string; /** Base64 selfie (liveness) – same as Android data.ekycStateModel.eKYCFileModel.imageFace */ imageFace?: string; /** Base64 OCR front – same as Android eKYCFileModel.imageOcrFront */ imageOcrFront?: string; /** Base64 OCR back – same as Android eKYCFileModel.imageOcrBack */ imageOcrBack?: string; /** Absolute path to selfie file (e.g. for react-native-fs / FormData) */ imageFacePath?: string; /** Absolute path to OCR front image file */ imageOcrFrontPath?: string; /** Absolute path to OCR back image file */ imageOcrBackPath?: string; } /** * Lỗi thống nhất từ mọi callback (NFC, C06, OCR, Liveness, Face Compare, SMS OTP, eSign). * getEkycError() map native customCode/customMessage từ Android sang code, message. */ export interface EKYCError { /** Tên event lỗi (e.g. "FACE_ERROR", "NFC_ERROR") */ event: string; /** Mã lỗi từ SDK (map từ customCode của native) */ code: string; /** Thông báo lỗi (map từ customMessage của native) */ message: string; } /** * Parse chuỗi lỗi cũ từ native (trước khi gửi object): "CustomError(customCode=109, customMessage=Lỗi Face compare...)" * Giúp app vẫn lấy được customCode/customMessage khi dùng bản native chưa có userInfo. */ function parseLegacyErrorString(msg: string): { customCode: string; customMessage: string } | null { if (!msg || typeof msg !== 'string') return null; const match = msg.match(/CustomError\s*\(\s*customCode\s*=\s*([^,)]+)\s*,\s*customMessage\s*=\s*(.+)\s*\)\s*$/); if (match) { return { customCode: String(match[1]).trim(), customMessage: String(match[2]).trim(), }; } return null; } /** * Chuẩn hóa mọi rejection từ SDK thành EKYCError (chỉ code, message). * Map native customCode/customMessage (Android userInfo hoặc top-level) → code, message. * - Native mới: error.userInfo (event, customCode, customMessage). * - Native cũ: error.message dạng "CustomError(customCode=..., customMessage=...)" → parse. */ export function getEkycError(error: any): EKYCError { const u = error?.userInfo ?? error; const rawMessage = u?.customMessage ?? error?.customMessage ?? error?.message ?? 'Unknown error'; let code = u?.customCode ?? error?.customCode ?? String(error?.code ?? ''); let message = rawMessage; const legacy = parseLegacyErrorString(rawMessage); if (legacy) { code = legacy.customCode; message = legacy.customMessage; } else if (!u?.customCode && !u?.customMessage && error?.message) { message = error.message; } const event = u?.event ?? error?.code ?? 'EKYC_ERROR'; return { event, code, message, }; }