This code will allow a user to complete their KYC process by authorizing HyperVerge and subscribing to the HyperVerge result API.
completeKYC(panNumber: any, bankAccountNumber: any, ifscCode: any) {
this.apiService.authorizeHyperVerge().subscribe((data: any) => {
let accessToken = data.result.token; //fetching the access token from api response
let workflowId = "Aadhaar-Pennydrop";
let transactionId = uuidv4(); //generating a unique transaction id
const hyperKycConfig = new this.windowRef.HyperKycConfig(accessToken, workflowId, transactionId);
hyperKycConfig.setInputs({
idNumber: panNumber,
ifsc: ifscCode,
accountNumber: bankAccountNumber
});
const handler = (HyperKycResult: any) => {
//setting the request object for calling the Hyperverge result api
let requestObject = {
transactionId: HyperKycResult.transactionId
};
this.apiService.fetchKYCResult(requestObject).subscribe(async (result: any) => { //calling the Hyperverge result api
console.log(result);
// error handling
let error = result.result.results[0].apiResponse.result.error;
if (error == undefined) {
error = result.result.results[1].apiResponse.result.error;
if (error == undefined) {
error = "Error. Please try again."
}
}
switch (HyperKycResult.status) {
case "error":
this._snackBar.showErrorMessage(error);
break;
case "auto_approved":
let userBankAccountDetails = result.result.results[1].apiResponse.result; //storing the user bank acc details
// logic to update the bank account details and isKYCDone in our database.
await getDocs(query(this.investorCollection, where("bankAccountNumber", "==", userBankAccountDetails.accountNumber))).then(docsSnap => {
if (docsSnap.size === 1) {
this._snackBar.showErrorMessage("This bank account number is linked with another account.");
} else {
let fields = {
bankAccountName: userBankAccountDetails.accountName,
bankAccountNumber: userBankAccountDetails.accountNumber,
ifscCode: userBankAccountDetails.ifsc,
isKyc: userBankAccountDetails.bankTxnStatus
};
//sending an email to the user that your KYC is complete.
const auth = getAuth();
onAuthStateChanged(auth, user => {
this.updateInvestors(user?.uid, fields);
let requestObject = {
email: user?.email,
name: localStorage.getItem("username")!.split(" ")[0],
emailType: "kyc-verification"
};
this.apiService.sendEmail(requestObject);
});
this._snackBar.showSuccessMessage("KYC Completed.");
}
this.windowRef.location.reload();
});
break;
case "auto_declined":
this._snackBar.showErrorMessage(error);
break;
}
});
};
this.windowRef.HyperKYCModule.launch(hyperKycConfig, handler);
});
}