/// <summary>
        /// Validates a list of card records.
        /// </summary>
        /// <param name="cardFileRecords"></param>
        /// <param name="issuerId"></param>
        /// <returns></returns>
        private Dictionary <string, CardFileRecord> validateCardRecords(List <CardFileRecord> cardFileRecords, int issuerId)
        {
            Dictionary <string, CardFileRecord> validCards    = new Dictionary <string, CardFileRecord>(); //<CardNumber, CardFileRecord>
            Dictionary <string, BranchLookup>   validBranches = new Dictionary <string, BranchLookup>();   //<BranchCode, BranchLookup>
            BranchService branchService = new BranchService();

            #region Loop through all the cards, checking valid branchs, Valid BIN's and duplicates in the file
            foreach (CardFileRecord cardFileRecord in cardFileRecords)
            {
                BranchLookup branchLookup;
                //See if we've already checked the DB for the branch. If it's not in the dictionary then do a read from the DB.
                if (!validBranches.TryGetValue(cardFileRecord.BranchCode, out branchLookup))
                {
                    //check if branch exists in the DB
                    branch Branch = branchService.GetBranchByBranchCode(issuerId, cardFileRecord.BranchCode, BranchStatus.ACTIVE);
                    branchLookup = new BranchLookup(cardFileRecord.BranchCode, 0, false);

                    if (Branch != null)
                    {
                        branchLookup = new BranchLookup(Branch.branch_code, Branch.branch_id, true);
                    }

                    validBranches.Add(cardFileRecord.BranchCode, branchLookup);
                }


                //check if branch exist
                //if not, add a record to rejected file
                if (!branchLookup.IsValid)
                {
                    //RegisterNonExistanceBranch(line, validCard.BranchCode, config);
                }
                else
                {
                    //Check if the card has been added to the dictionary already, if it has dont add it again.
                    if (validCards.ContainsKey(cardFileRecord.PsuedoPAN))
                    {
                        //duplicaterecords++;
                        //duplicatesInFileMessage += validCard.PsuedoPAN + ", ";
                    }
                    else
                    {
                        if (CheckIfIsLicenseCardRecord(cardFileRecord.PsuedoPAN))
                        {
                            cardFileRecord.BranchId = branchLookup.BranchId;
                            validCards.Add(cardFileRecord.PsuedoPAN, cardFileRecord);
                        }
                    }
                }
            }
            #endregion

            #region check card records for duplication in indigo
            //check if card record is in the DB
            List <string> results = fileLoaderDAL.ValidateCardsLoaded(new List <string>(validCards.Keys));

            foreach (string result in results)
            {
                //Because the results coming back are based on the keys, the dictionary should have this key, but checking just incase
                if (validCards.ContainsKey(result))
                {
                    //recored is already in DB
                    validCards.Remove(result);
                    //duplicaterecords++;
                    //duplicatesInDB += string.Format("Card Record is a duplicate in Indigo {0},", result);
                    //fileLoadComments += Environment.NewLine + string.Format("Card Record {0} is a duplicate to a previously loaded record. Record will be excluded".ToUpper() + Environment.NewLine, result);
                }
            }
            #endregion

            return(validCards);
        }
Example #2
0
        /// <summary>
        /// Reads all card info from file, checks for duplicates in the file, in the DB and verifies the branch is valid, returns a list of valid card number with their branch code.
        /// </summary>
        /// <param name="fullFileName"></param>
        /// <param name="config"></param>
        /// <param name="fileHistory"></param>
        /// <returns></returns>
        private Dictionary <string, ValidCard> ProcessCardFile_Tieto(string fullFileName, issuer config, file_history fileHistory)
        {
            //<PAN, validCard>
            Dictionary <string, ValidCard> validCards = new Dictionary <string, ValidCard>();
            var cardsFile = new TextFile(fullFileName, false, false);

            try
            {
                //<BranchCode, BranchLookup>
                Dictionary <string, BranchLookup> validBranches = new Dictionary <string, BranchLookup>();

                CardRecords = cardsFile.ReadFile();

                string duplicatesInFileMessage     = "";
                string duplicatesInDB              = "";
                string unlicensesCardsErrorMessage = "";//temporary until license management is fully implemented

                #region basic records processing and fields settings
                for (int i = 0; i < CardRecords.Count; i++)
                {
                    string line = CardRecords[i][0];
                    //text file, so each record only contains 1 string which then needs to be sub-stringed.

                    string[] fields = line.Split(new string[] {
                        " ".PadLeft(1), " ".PadLeft(2), " ".PadLeft(3),
                        " ".PadLeft(4), " ".PadLeft(5), " ".PadLeft(6),
                        " ".PadLeft(7), " ".PadLeft(8), " ".PadLeft(9),
                        " ".PadLeft(10), " ".PadLeft(11), " ".PadLeft(12),
                        " ".PadLeft(13), " ".PadLeft(14), " ".PadLeft(15)
                    }, StringSplitOptions.RemoveEmptyEntries);

                    ValidCard validCard = new ValidCard();
                    validCard.PAN            = fields[0].Substring(1, 16);
                    validCard.BranchCode     = validCard.PAN.Substring(6, 2);
                    validCard.PsuedoPAN      = fields[1] + fields[2];
                    validCard.SequenceNumber = fields[PostilionDetailedRecordColumns.SEQ_NR];

                    BranchLookup branchLookup;
                    //See if we've already checked the DB for the branch. If it's not in the dictionary then do a read from the DB.
                    if (!validBranches.TryGetValue(validCard.BranchCode, out branchLookup))
                    {
                        //check if branch exist
                        branch Branch = CheckDBForBranch(config.issuer_id, validCard.BranchCode);
                        branchLookup = new BranchLookup(validCard.BranchCode, 0, false);

                        if (Branch != null)
                        {
                            branchLookup = new BranchLookup(Branch.branch_code, Branch.branch_id, true);
                        }

                        validBranches.Add(validCard.BranchCode, branchLookup);
                    }


                    //check if branch exist
                    //if not, add a record to rejected file
                    if (!branchLookup.IsValid)
                    {
                        RegisterNonExistanceBranch(line, validCard.BranchCode, config);
                    }
                    else
                    {
                        //CardRecords[i] = new[] { validCard.PsuedoPAN, validCard.BranchCode }; //, custNames, pinBlock };

                        //Check if the card has been added to the dictionary already, if it has dont add it again.
                        if (validCards.ContainsKey(validCard.PsuedoPAN))
                        {
                            duplicaterecords++;
                            duplicatesInFileMessage += validCard.PsuedoPAN + ", ";
                        }
                        else
                        {
                            //check if card is within licensed ecobank bins -- temp solution
                            //TODO: RAB Remove hard coding here.... yuck yuck yuck
                            if (CheckIfIsLicenseCardRecord(validCard.PsuedoPAN))
                            {
                                validCard.BranchId = branchLookup.BranchId;
                                validCards.Add(validCard.PsuedoPAN, validCard);
                            }
                        }
                    }
                }
                #endregion

                #region check card records for duplication in indigo
                //check if card record is in the DB
                List <string> results = fileLoaderDAL.ValidateCardsLoaded(new List <string>(validCards.Keys));

                foreach (string result in results)
                {
                    //Because the results coming back are based on the keys, the dictionary should have this key, but checking just incase
                    if (validCards.ContainsKey(result))
                    {
                        //recored is already in DB
                        validCards.Remove(result);
                        duplicaterecords++;
                        duplicatesInDB   += string.Format("Card Record is a duplicate in Indigo {0},", result);
                        fileLoadComments += Environment.NewLine + string.Format("Card Record {0} is a duplicate to a previously loaded record. Record will be excluded".ToUpper() + Environment.NewLine, result);
                    }
                }
                #endregion

                processedrecords = validCards.Count;

                //update file status
                UpdateFileStatusAfterRead();

                //add comments for duplicates
                if (duplicatesInFileMessage.Trim().Length > 0)
                {
                    fileLoadComments += " Duplicate records in file: ".ToUpper() + duplicatesInFileMessage + ". ";
                }

                if (duplicatesInDB.Trim().Length > 0)
                {
                    fileLoadComments += " Records already exist in DB: ".ToUpper() + duplicatesInDB + ". ";
                }

                //add comments for unlicense records
                if (unlicensesCardsErrorMessage.Trim().Length > 0)
                {
                    fileLoadComments += " - Unlicensed cards processing is disabled. Unlicensed records: ".ToUpper() +
                                        unlicensedrecords + ". ";
                }
            }
            catch (Exception ex)
            {
                //invalid file structure
                LogFileWriter.WriteFileLoaderError(
                    config.issuer_code + ": " + fullFileName + " contains invalid data, or is not formatted correctly" +
                    ToString(), ex);
                fileLoadComments += " | file contains invalid data, or is not formatted correctly";
                fileStatus        = FileStatus.INVALID_FORMAT;
                invalidformatfile++;
            }

            fileHistory.file_statuses.file_status_id = (int)fileStatus;
            fileHistory.number_successful_records    = processedrecords;
            fileHistory.number_failed_records        = duplicaterecords;
            fileHistory.file_load_comments           = fileLoadComments;

            return(validCards);
        }
Example #3
0
        /// <summary>
        /// Validates a list of card records.
        /// </summary>
        /// <param name="cardFileRecords"></param>
        /// <param name="issuerId"></param>
        /// <returns></returns>
        private FileStatus validateCardRecords(List <CardFileRecord> cardFileRecords, int issuerId, out Dictionary <string, CardFileRecord> validCardRecords, ref List <FileCommentsObject> fileComments)
        {
            validCardRecords = new Dictionary <string, CardFileRecord>();
            Dictionary <string, CardFileRecord> validCards    = new Dictionary <string, CardFileRecord>(); //<CardNumber, CardFileRecord>
            Dictionary <string, BranchLookup>   validBranches = new Dictionary <string, BranchLookup>();   //<BranchCode, BranchLookup>
            List <issuer_product> validProducts = new List <issuer_product>();
            BranchService         branchService = new BranchService();

            //get a list of valid bins

            #region Loop through all the cards, checking valid branchs, Valid BIN's and duplicates in the file
            foreach (CardFileRecord cardFileRecord in cardFileRecords)
            {
                BranchLookup branchLookup;
                //See if we've already checked the DB for the branch. If it's not in the dictionary then do a read from the DB.
                if (!validBranches.TryGetValue(cardFileRecord.BranchCode.Trim().ToUpper(), out branchLookup))
                {
                    //check if branch exists in the DB
                    branch Branch = branchService.GetBranchByBranchCode(issuerId, cardFileRecord.BranchCode.Trim().ToUpper(), BranchStatus.ACTIVE);
                    branchLookup = new BranchLookup(cardFileRecord.BranchCode.Trim().ToUpper(), 0, false);

                    if (Branch != null)
                    {
                        branchLookup = new BranchLookup(Branch.branch_code.Trim().ToUpper(), Branch.branch_id, true);
                    }

                    validBranches.Add(cardFileRecord.BranchCode.Trim().ToUpper(), branchLookup);
                }


                //check if branch exist
                //if not, add a record to rejected file
                if (!branchLookup.IsValid)
                {
                    log.Error("No Valid Branch found in Indigo:\t" + branchLookup.BranchCode);
                    fileComments.Add(new FileCommentsObject("No Valid Branch found in Indigo:\t" + branchLookup.BranchCode));
                    return(FileStatus.NO_ACTIVE_BRANCH_FOUND);
                }
                else
                {
                    //Check if the card has been added to the dictionary already, if it has dont add it again.
                    if (validCards.ContainsKey(cardFileRecord.PsuedoPAN.Trim()))
                    {
                        fileComments.Add(new FileCommentsObject("Duplicate card found in card file:\t" + cardFileRecord.PsuedoPAN));
                        log.Error("Duplicate card found in card file:\t" + cardFileRecord.PsuedoPAN);
                        return(FileStatus.DUPLICATE_CARDS_IN_FILE);
                    }
                    else
                    {
                        if (CheckIfIsLicenseCardRecord(cardFileRecord.PAN.Trim()))
                        {
                            //See if we can link this card to a product
                            int lookupTries = 0;
                            int productId   = -1;

                            do
                            {
                                int charCount = 0;
                                foreach (var product in validProducts)
                                {
                                    //Check the actual PAN, not PSUDO pan... Product bin can be up to 9 characters.
                                    if (cardFileRecord.PAN.Trim().StartsWith(product.product_bin_code))
                                    {
                                        //Need to find the bincode with the least amount of characters, as this result may have more than one result.
                                        //E.G. Card number could be 123456-88771199 but the result may contain a record for products with:
                                        // 123456 and 123456999 becuse the search is only on the first 6 characters.
                                        if (product.product_bin_code.Length > charCount)
                                        {
                                            charCount = product.product_bin_code.Length;
                                            productId = product.product_id;
                                        }
                                    }
                                }

                                if (productId == -1)//Call the DB to see if we can get valid product for the user
                                {
                                    var result = fileLoaderDAL.FetchIssuerProduct(cardFileRecord.PsuedoPAN, branchLookup.BranchId);
                                    validProducts.AddRange(result);
                                }

                                lookupTries++;
                            }while (productId == -1 && lookupTries < 3);

                            if (productId > -1)
                            {
                                cardFileRecord.BranchId  = branchLookup.BranchId;
                                cardFileRecord.ProductId = productId;
                                validCards.Add(cardFileRecord.PsuedoPAN.Trim(), cardFileRecord);
                            }
                            else
                            {
                                //TODO; could not find product for card.
                                //throw new Exception("Could not find product for card.");
                                log.Error("Could not find product for card.");
                                fileComments.Add(new FileCommentsObject("Could not find product for card."));
                                return(FileStatus.NO_PRODUCT_FOUND_FOR_CARD);
                            }
                        }
                        else
                        {
                            log.Error("Unlicensed BIN in card file.");
                            fileComments.Add(new FileCommentsObject("Unlicensed BIN in card file."));
                            return(FileStatus.UNLICENSED_BIN);
                        }
                    }
                }
            }
            #endregion

            #region check card records for duplication in indigo
            //check if card record is in the DB
            List <string> results = fileLoaderDAL.ValidateCardsLoaded(new List <string>(validCards.Keys));

            foreach (string result in results)
            {
                CardFileRecord value;
                //Because the results coming back are based on the keys, the dictionary should have this key, but checking just incase
                if (validCards.TryGetValue(result.Trim(), out value))
                {
                    //card is already in DB
                    validCards.Remove(result.Trim());
                    log.Error("Duplicate cards in database.");
                    fileComments.Add(new FileCommentsObject("Duplicate cards in database."));
                    return(FileStatus.DUPLICATE_CARDS_IN_DATABASE);
                }
            }
            #endregion

            validCardRecords = validCards;
            return(FileStatus.VALID_CARDS);
        }