private Tuple <string, IList <Merchant> > ParseMasterCardFile(Stream fileStream, MerchantFileType merchantFileType) { bool validFile = true; string provisioningFileDate = null; List <Merchant> lstMerchants = new List <Merchant>(); using (StreamReader streamReader = new StreamReader(fileStream)) { int lineNumber = 0; string currentLine = streamReader.ReadLine(); while (!string.IsNullOrWhiteSpace(currentLine) && validFile) { string recordType = currentLine.Substring(0, RecordLengthConstants.RecordType); switch (recordType) { case HeaderRecord.RecordType: Log.Info("Parsing the header record"); provisioningFileDate = HeaderRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter); validFile = !string.IsNullOrWhiteSpace(provisioningFileDate); lineNumber++; break; case DetailRecord.ProvisioningRecordType: Log.Info($"Parsing the {merchantFileType.ToString()} merchant detail record"); Merchant merchant = DetailRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter, merchantFileType, lineNumber); if (merchant != null) { lstMerchants.Add(merchant); Log.Info($"Parsed the {merchantFileType.ToString()} in line number {lineNumber}"); } lineNumber++; break; case DetailRecord.ResponseFileRecordType: Log.Info($"Parsing the {merchantFileType.ToString()} merchant detail record"); merchant = DetailRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter, merchantFileType, lineNumber); if (merchant != null) { Merchant existingMerchant = null; //Check if we have already seen this merchant in the current auth/clearing file based on MCID or MCSiteId //If so, then update the payment info for the existing merchant. If not, add the //new merchant to the list. if (merchant.ExtendedAttributes != null && merchant.ExtendedAttributes.ContainsKey(MerchantConstants.MCID)) { existingMerchant = lstMerchants.FirstOrDefault(m => (m.ExtendedAttributes != null && m.ExtendedAttributes.ContainsKey(MerchantConstants.MCID) && m.ExtendedAttributes[MerchantConstants.MCID] == merchant.ExtendedAttributes[MerchantConstants.MCID])); } else if (merchant.ExtendedAttributes != null && merchant.ExtendedAttributes.ContainsKey(MerchantConstants.MCSiteId)) { existingMerchant = lstMerchants.FirstOrDefault(m => (m.ExtendedAttributes != null && m.ExtendedAttributes.ContainsKey(MerchantConstants.MCSiteId) && m.ExtendedAttributes[MerchantConstants.MCSiteId] == merchant.ExtendedAttributes[MerchantConstants.MCSiteId])); } if (existingMerchant != null) { existingMerchant.Payments.Add(merchant.Payments[0]); } else { lstMerchants.Add(merchant); } Log.Info($"Parsed the {merchantFileType.ToString()} in line number {lineNumber}"); } lineNumber++; break; case TrailerRecord.RecordType: Log.Info("Parsing the trailer record"); int detailRecordCount = TrailerRecord.Parse(currentLine, MerchantConstants.MasterCardRecordDelimiter); //linenumber -2 = discarding the header record to get the total detailed records read validFile = ((lineNumber - 1) == detailRecordCount); break; default: Log.Error($"Unknown record type {recordType} in line number {lineNumber}"); lineNumber++; break; } currentLine = streamReader.ReadLine(); } } return(validFile ? new Tuple <string, IList <Merchant> >(provisioningFileDate, lstMerchants) : null); }