public static void LogTransaction(Iso8583Message incomingMessage, SourceNode sourceNode = null, Scheme scheme = null, Fee fee = null, bool needReversal = false)
        {
            var instCode = incomingMessage.Fields[2].ToString().Substring(0, 6);
               string transactionTypeCode = (incomingMessage.Fields[3].ToString().Substring(0, 2));
               string channelCode = incomingMessage.Fields[123].ToString().Substring(13, 2);
               string cardPan = incomingMessage.Fields[2].ToString();
               string response = string.Empty;
               string responseCode = string.Empty;
               DateTime transmissionDate = DateTime.UtcNow;

               TransactionLog transactionLog = new TransactionLog();
               try
               {
               transactionLog.MTI = incomingMessage.MessageTypeIdentifier.ToString();
               transactionLog.STAN = incomingMessage.Fields[11].ToString();
               transactionLog.Amount = Convert.ToDouble(incomingMessage.Fields[4].ToString());
               transactionLog.CardPAN = cardPan;
               var channel = new ChannelManager().GetByCode(channelCode);
               if (channel != null)
               {
                   transactionLog.Channel = channel.Name;
               }
               var trnx = new TransactionTypeManager().GetByCode(transactionTypeCode);
               if (trnx != null)
               {
                   transactionLog.TransactionType = trnx.Name;
               }
               transactionLog.SourceNode = sourceNode.Name;
               transactionLog.TransactionDate = transmissionDate;
               transactionLog.DateCreated = DateTime.Now;
               transactionLog.DateModified = DateTime.Now;

               string orDataElt = incomingMessage.Fields[90].ToString();
               int length = orDataElt.Length;
               transactionLog.OriginalDataElement = orDataElt.Length > 19 ? orDataElt.Remove(0, (length - 19)) : orDataElt;

               try
               {
                   responseCode = incomingMessage.Fields[39].Value.ToString();
               }
               catch (Exception) { }

               if (scheme != null)
               {
                   transactionLog.Scheme = scheme.Name;
                   transactionLog.Route = scheme.Route.Name;

                   transactionLog.SinkNode = scheme.Route.SinkNode.Name;
               }

               try
               {
                   string value = incomingMessage.Fields[28].Value.ToString();
                   decimal result = 0;
                   if (Decimal.TryParse(value, out result))
                   {
                       transactionLog.Charge = result;
                   }
               }
               catch (Exception) { }
               if (fee != null)
               {
                   transactionLog.Fee = fee.Name;
               }
               if (responseCode != null)
               {
                   transactionLog.ResponseCode = responseCode;
               }
               if (responseCode != null)
               {
                   transactionLog.ResponseDescription = MessageDefinition.GetResponseDescription(responseCode);
               }
               string acc1 = incomingMessage.Fields[102].Value.ToString();
               string acc2 = incomingMessage.Fields[103].Value.ToString();
               transactionLog.Account1 = acc1;
               transactionLog.Account2 = acc2;
               transactionLog.IsReversePending = needReversal;

               if (incomingMessage.MessageTypeIdentifier.ToString() == "430" && responseCode == "00")
               {
                   transactionLog.IsReversed = true;
                   // SetReversalStatus(incomingMsg, responseCode);  //here
               }

               if (new TransactionLogManager().AddTransactionLog(transactionLog))
               {
                   Log("Transaction log::: " + transactionLog.STAN + " " + transactionLog.TransactionDate);
               }
               else
               {
                   Log("Transaction log::: not successful");
               }
               }
               catch (Exception ex)
               {
               Log("Error occurred while logging transaction \n" + ex.Message);
               Console.ForegroundColor = ConsoleColor.Red;
               }
        }
        public Iso8583Message ValidateMessage(Iso8583Message originalMessage, int sourceID)
        {
            Logger.Log("\n Enter Validator");
               //get source Node.
               SourceNode sourceNode = new SourceNodeManager().GetByID(sourceID);

               DateTime transmissionDate = DateTime.UtcNow;
               //format TransactionDate
               string transactionDate = string.Format("{0}{1}",
                 string.Format("{0:00}{1:00}", transmissionDate.Month, transmissionDate.Day),
                 string.Format("{0:00}{1:00}{2:00}", transmissionDate.Hour,
                 transmissionDate.Minute, transmissionDate.Second));

               //set Original Data Element
             string originalDataElement = string.Format("{0}{1}{2}", originalMessage.MessageTypeIdentifier.ToString(), originalMessage.Fields[11].ToString(), transactionDate);
               //add original data element to original message
               originalMessage.Fields.Add(90, originalDataElement);

               //Do Message Log
               Logger.LogTransaction(originalMessage, sourceNode);

               //  Check if it is reversal message and do the needful
               if (originalMessage.MessageTypeIdentifier == 421)
               {
               Logger.Log("\n This is a reversal");
               bool conReversal;
               Iso8583Message reversalIsoMsg = GetReversalMessage(originalMessage, out conReversal);
               if (!conReversal)
               {
                   Logger.LogTransaction(reversalIsoMsg);
                   return reversalIsoMsg;
               }
               originalMessage = reversalIsoMsg;
               Logger.LogTransaction(originalMessage, sourceNode);
               }

               string theCardPan = originalMessage.Fields[2].Value.ToString();
               string tranasactionTypeCode = originalMessage.Fields[3].Value.ToString().Substring(0, 2);
               double amount = Convert.ToDouble(originalMessage.Fields[4].Value);
               string orgExpiry = originalMessage.Fields[14].Value.ToString();

               string code = originalMessage.Fields[123].ToString().Substring(13, 2);

               Channel channel = new ChannelManager().GetByCode(code);
               Fee fee = null;
              // string cardPAN = theCardPan.Substring(0, 6);
               Route theRoute = new RouteManager().GetRouteByCardPan(theCardPan.Substring(0, 6));
               TransactionType transactionType = new TransactionTypeManager().GetByCode(tranasactionTypeCode);
               Iso8583Message responseMessage;

               //check if card has expired
               DateTime cardExpiryDate = ParseExpiryDate(orgExpiry);

               if(cardExpiryDate < DateTime.Now)
               {
               responseMessage = SetReponseMessage(originalMessage, "54");         //Expired card
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }

               if (amount <= 0 && tranasactionTypeCode != "31")
               {
               responseMessage = SetReponseMessage(originalMessage, "13");         //Invalid amount
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }

               if (theRoute == null)
               {
               Logger.Log("Sink node is null.");
               responseMessage = SetReponseMessage(originalMessage, "15");         //No such issuer
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }
               SinkNode sinkNode = theRoute.SinkNode;
               if (sinkNode == null)
               {
               Logger.Log("Sink node is null.");
               responseMessage = SetReponseMessage(originalMessage, "91");  //Issuer inoperative
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }

               Logger.Log("Loading SourceNode Schemes");

               var theSchemes = sourceNode.Schemes;
               Scheme scheme = null;
               try
               {
               scheme = theSchemes.Where(x => x.Route.CardPAN == theCardPan.Substring(0,6)).SingleOrDefault();
               }
               catch (Exception ex)
               {
               Logger.Log("Error: \n" + ex.Message);
               responseMessage = SetReponseMessage(originalMessage, "31"); // Lazy load error : Set correct response code later
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }

               if (scheme == null)
               {
               responseMessage = SetReponseMessage(originalMessage, "92"); // Route not allowed : Set correct response code later
               Logger.LogTransaction(responseMessage, sourceNode);
               return responseMessage;
               }

              // int panCount = sourceNode.Schemes.Count(x => x.Route == theRoute);
               Logger.Log("Scheme : " + scheme.Name + " Loaded");

               Logger.Log("Getting fee:");
               fee = GetFeeIfTransactionIsAllowed(transactionType, channel, scheme);
               if (fee == null)
               {
               responseMessage = SetReponseMessage(originalMessage, "58"); // Transaction type not allowed in this scheme
               Logger.LogTransaction(responseMessage, sourceNode, scheme);
               return responseMessage;
               }
               else
               {
               originalMessage = SetFee(originalMessage, CalculateFee(fee, amount));
               }
               bool needReversal = false;

               Iso8583Message msgFromFEP = ToFEP(originalMessage, sinkNode, out needReversal);

               Logger.LogTransaction(msgFromFEP, sourceNode, scheme, fee, needReversal);

               return msgFromFEP;
        }