package com.awesomeproject import android.app.Activity import android.content.Intent import android.util.Log import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import com.a5starcompany.sprintchecksdk.util.CheckoutMethod import com.a5starcompany.sprintchecksdk.util.KYCCallback import com.a5starcompany.sprintchecksdk.util.KYCConfig import com.a5starcompany.sprintchecksdk.util.KYCInitializationResult import com.a5starcompany.sprintchecksdk.util.KYCResult import com.a5starcompany.sprintchecksdk.util.KYCVerificationManager import com.facebook.react.bridge.ActivityEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.module.annotations.ReactModule @ReactModule(name = SprintCheckModule.NAME) class SprintCheckModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), ActivityEventListener { private var verificationPromise: Promise? = null private var kycLauncher: ActivityResultLauncher? = null init { reactContext.addActivityEventListener(this) } override fun getName(): String { return NAME } /** * Initializes the SprintCheck SDK with the provided API and encryption keys. * @param apiKey The API key for the SprintCheck SDK. * @param encryptionKey The encryption key for the SprintCheck SDK. * @param promise A promise to resolve on success or reject on failure. */ @ReactMethod fun initialize(apiKey: String, encryptionKey: String, promise: Promise) { Log.d("SprintCheckModule", "Initializing with API key: $apiKey") try { val config = KYCConfig(apiKey, encryptionKey) val kycManager = KYCVerificationManager.getInstance() val initResult = kycManager.initialize(config) when (initResult) { is KYCInitializationResult.Success -> { // Initialization successful, proceed with verification Log.d("mainActivity", "sdk initialised") promise.resolve("SDK initialized successfully") } is KYCInitializationResult.Failure -> { // Handle initialization failure Log.d("mainActivity", "sdk Initialization failed: ${initResult.errorMessage}") promise.reject("INIT_ERROR", "sdk Initialization failed: ${initResult.errorMessage}") return } } } catch (e: Exception) { promise.reject("INIT_ERROR", e.message) } } /** * Starts the BVN verification process. * @param email The user's email address. * @param promise A promise to resolve with verification results or reject on failure. */ @ReactMethod fun startBvnVerification(email: String, promise: Promise) { startVerification(CheckoutMethod.bvn, email, promise) } /** * Starts the Facial verification process. * @param email The user's email address. * @param promise A promise to resolve with verification results or reject on failure. */ @ReactMethod fun startFacialVerification(email: String, promise: Promise) { startVerification(CheckoutMethod.facial, email, promise) } /** * Starts the NIN verification process. * @param email The user's email address. * @param promise A promise to resolve with verification results or reject on failure. */ @ReactMethod fun startNinVerification(email: String, promise: Promise) { startVerification(CheckoutMethod.nin, email, promise) } /** * Sets up the ActivityResultLauncher for handling KYC verification results. */ private val callback = object : KYCCallback { override fun onKYCSuccess(result: KYCResult.Success) { // Handle successful verification // Access all result data val name = result.name val bvn = result.bvn val score = result.confidenceLevel val verificationId = result.reference val isLive = result.status val verify = result.verify val message = result.message val nin = result.nin val method = result.method Log.d("TAG", "result: ${result.toString()} $method") // Handle successful verification // handleVerificationSuccess(verificationId, score, isLive) // Create a WritableMap to send data back to React Native val successMap = com.facebook.react.bridge.Arguments.createMap().apply { putString("verificationId", verificationId) putInt("score", score) putBoolean("isLive", isLive) putBoolean("verify", verify) putString("message", message) putString("name", name) putString("bvn", bvn) putString("nin", nin) putString("status", "SUCCESS") // Custom status for React Native } verificationPromise?.resolve(successMap) } override fun onKYCFailure(result: KYCResult.Failure) { // Handle verification failure // Access error details val errorCode = result.errorCode val errorMessage = result.errorMessage // Handle verification failure or cancellation // handleVerificationFailure(errorCode, errorMessage) // Create a WritableMap for cancellation/error val errorMap = com.facebook.react.bridge.Arguments.createMap().apply { putString("errorCode", errorCode ?: "CANCELLED") putString("errorMessage", errorMessage ?: "Verification cancelled by user.") putString("status", "CANCELLED") // Custom status for React Native } verificationPromise?.reject(errorCode ?: "CANCELLED", errorMap) } override fun onKYCCancelled() { // Handle user cancellation val errorCode = "user csncelled" val errorMessage = "Verification cancelled by user" // Handle verification failure or cancellation // handleVerificationFailure(errorCode, errorMessage) // Create a WritableMap for cancellation/error val errorMap = com.facebook.react.bridge.Arguments.createMap().apply { putString("errorCode", errorCode ?: "CANCELLED") putString("errorMessage", errorMessage ?: "Verification cancelled by user.") putString("status", "CANCELLED") // Custom status for React Native } verificationPromise?.reject(errorCode ?: "CANCELLED", errorMap) } } /** * Common method to start any verification process (BVN, NIN, etc.). * @param method The checkout method (e.g., CheckoutMethod.bvn, CheckoutMethod.nin). * @param email The user's email address. * @param promise A promise to resolve with verification results or reject on failure. */ private fun startVerification(method: CheckoutMethod, email: String, promise: Promise) { val activity = reactApplicationContext.currentActivity!! this.verificationPromise = promise try { // Start the verification process KYCVerificationManager.getInstance().startVerification(activity,method,email,callback) } catch (e: Exception) { promise.reject("START_ERROR", "${e.message} Unknown error starting verification.") } } // --- ActivityEventListener methods --- /** * Handles the result of activities launched by this module. * This is the standard way for React Native modules to get results from activities. */ override fun onActivityResult( activity: Activity, requestCode: Int, resultCode: Int, data: Intent? ) { // Check if the result is for our KYC request and if a promise is pending. // if (requestCode == KYC_REQUEST_CODE) { // // Log for debugging purposes // Log.d(TAG, "onActivityResult - Request Code: $requestCode, Result Code: $resultCode") // Log.d(TAG, "onActivityResult - Data: ${data?.extras}") // // when (resultCode) { // Activity.RESULT_OK -> { // // Extract data from the intent for successful verification // val verificationId = data?.getStringExtra("reference") // val score = data?.getIntExtra("confidenceLevel", 0) ?: 0 // val isLive = data?.getBooleanExtra("status", false) ?: false // val verify = data?.getBooleanExtra("verify", false) ?: false // val message = data?.getStringExtra("message") // val name = data?.getStringExtra("name") // val bvn = data?.getStringExtra("bvn") // val nin = data?.getStringExtra("nin") // val method = data?.getStringExtra("method") // // // Create a WritableMap to send data back to React Native // val successMap = com.facebook.react.bridge.Arguments.createMap().apply { // putString("verificationId", verificationId) // putInt("score", score) // putBoolean("isLive", isLive) // putBoolean("verify", verify) // putString("message", message) // putString("name", name) // putString("bvn", bvn) // putString("nin", nin) // putString("method", method) // putString("status", "SUCCESS") // Custom status for React Native // } // verificationPromise?.resolve(successMap) // } // Activity.RESULT_CANCELED -> { // // Extract error information for cancelled verification // val errorCode = data?.getStringExtra("error_code") // val errorMessage = data?.getStringExtra("error_message") // // // Create a WritableMap for cancellation/error // val errorMap = com.facebook.react.bridge.Arguments.createMap().apply { // putString("errorCode", errorCode ?: "CANCELLED") // putString("errorMessage", errorMessage ?: "Verification cancelled by user.") // putString("status", "CANCELLED") // Custom status for React Native // } // verificationPromise?.reject(errorCode ?: "CANCELLED", errorMap) // } // else -> { // // Handle any other result codes not explicitly covered // val unknownErrorMap = com.facebook.react.bridge.Arguments.createMap().apply { // putString("errorCode", "UNKNOWN_RESULT") // putString("errorMessage", "Verification returned an unknown result code: $resultCode") // putString("status", "FAILED") // } // verificationPromise?.reject("UNKNOWN_RESULT", unknownErrorMap) // } // } // // Always clear the promise after handling the result to prevent memory leaks // verificationPromise = null // } } /** * Not used in this context, but required by ActivityEventListener interface. */ override fun onNewIntent(intent: Intent) { // Not used } companion object { const val NAME: String = "SprintCheck" private const val KYC_REQUEST_CODE = 9001 // A unique request code for our activity result private const val TAG = "SprintCheckModule" // Tag for logging } }