public void Update(TransactionLog thisLog)
 {
     if (thisLog != null)
        {
            _db.Update(thisLog);
        }
 }
 public bool AddTransactionLog(TransactionLog model)
 {
     bool result = false;
        object obj = _db.AddTransactionLog(model);
        if (obj != null)
        {
            result = true;
        }
        return result;
 }
 public object AddTransactionLog(TransactionLog model)
 {
     object result = null;
        using (var session = DataAccess.OpenSession())
        {
        using (var transactn = session.BeginTransaction())
        {
            try
            {
                result = session.Save(model);
                transactn.Commit();
            }
            catch (Exception)
            {
                transactn.Rollback();
                throw;
            }
        }
        }
        return result;
 }
        public TransactionLog GetByOriginalDataElement(string originalDataElement)
        {
            TransactionLog trxLog = null;
               if (!string.IsNullOrEmpty(originalDataElement))
               {

                trxLog = _Session.QueryOver<TransactionLog>().Where(x => x.OriginalDataElement == originalDataElement).SingleOrDefault();
               }
               else
               {
               trxLog = new TransactionLog();
               }
               return trxLog;
        }
        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;
               }
        }
        private Iso8583Message BuildReversalIsoMessage(TransactionLog log)
        {
            Iso8583Message echoMessage = new Iso8583Message(421);
               try
               {
               echoMessage.Fields.Add(2, log.CardPAN);
               echoMessage.Fields.Add(3, string.Format("{0}2000", "01"));
               echoMessage.Fields.Add(4, log.Amount.ToString());
               DateTime transmissionDate = DateTime.Now;
               echoMessage.Fields.Add(7, 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)));
               echoMessage.Fields.Add(11, log.STAN);
               echoMessage.Fields.Add(12, string.Format("{0:00}{1:00}{2:00}", transmissionDate.Hour,
                   transmissionDate.Minute, transmissionDate.Second));
               echoMessage.Fields.Add(13, string.Format("{0:00}{1:00}", transmissionDate.Month, transmissionDate.Day));

               //echoMsg.Fields.Add(15, DateTime.Today.ToString("yyMM"));
               echoMessage.Fields.Add(22, "123");
               echoMessage.Fields.Add(25, "12");
               echoMessage.Fields.Add(28, "C000000");
               echoMessage.Fields.Add(29, "C000000");
               //echoMsg.Fields.Add(30, "C00000000");
               echoMessage.Fields.Add(32, "100002");
               //echoMsg.Fields.Add(35, string.Format("{0}={1:yyMM}", cardPAN, xpiryDate));
               //echoMsg.Fields.Add(37, transactionLog.RetrivalRefNo);
               //echoMsg.Fields.Add(40, "101");
               echoMessage.Fields.Add(41, "1701230J");
               echoMessage.Fields.Add(42, "VIACARD");
               echoMessage.Fields.Add(43, "Yaba, Office, Lagos");
               echoMessage.Fields.Add(49, "566");
               echoMessage.Fields.Add(90, "00000000000000000000000" + log.OriginalDataElement);
               echoMessage.Fields.Add(102, log.Account1);
               echoMessage.Fields.Add(103, log.Account2);
               echoMessage.Fields.Add(123, "01".PadLeft(15, '0'));
               Message inner = new Message();
               inner.Fields.Add(2, "IMNODE_00426629");
               //inner.Fields.Add(3, "ATMsrc      PRUICCsnk   000119000119ICCGroup    ");
               //inner.Fields.Add(20, DateTime.Today.ToString("yyMMdd"));

               echoMessage.Fields.Add(127, inner);
               }
               catch (Exception)
               {
               Logger.Log("Problem building reversal message");
               return null;
               }
               return echoMessage;
        }