public string PullRegistrationDetails(string MyPRN)
        {
            try
            {
                // ---------------------- Validate the Prn -------------------------
                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
                URAGateway.UraPmtService service = new URAGateway.UraPmtService();
                eTaxPmtLibrary.UraEncryption uraEnc = new eTaxPmtLibrary.UraEncryption();
                string EnctryptionAlgorith = "rijndael";
                FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
                FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
                MyAPIPass = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
                MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
                string UraPassword = Cryptographer.Decrypt(MyAPIPass);
                string s = uraEnc.EncryptedData(UraPassword, x509Cert_ura);                 
                URAGateway.PaymentRegEntity d = service.GetPRNDetails(MyAPIUserName, s, System.Configuration.ConfigurationManager.AppSettings["BANK_SHORT_CODE"], MyPRN);

                // ---------------------- Return Xml Response -------------------------
                string PaymentDetails = "";
                MemoryStream XmlMs = new MemoryStream();
                XmlTextWriter XmlTxtWtr = new XmlTextWriter(XmlMs, System.Text.Encoding.UTF8);
                XmlTxtWtr.Formatting = Formatting.Indented;
                XmlTxtWtr.Indentation = 4;
                XmlTxtWtr.WriteStartDocument();
                XmlTxtWtr.WriteStartElement("URA PAYMENT DETAILS");
                XmlTxtWtr.WriteElementString("PRN", d.Prn);
                XmlTxtWtr.WriteElementString("TIN", d.Tin);
                XmlTxtWtr.WriteElementString("CUSTNAME",d.TaxpayerName);
                XmlTxtWtr.WriteElementString("AMOUNT", d.Amount);
                XmlTxtWtr.WriteElementString("STATUS", d.StatusCode);
                XmlTxtWtr.WriteElementString("EXPIRYDATE",d.ExpiryDt);
                XmlTxtWtr.WriteEndElement();
                XmlTxtWtr.WriteEndDocument();
                XmlTxtWtr.Flush();
                StreamReader strmrdr = new StreamReader(XmlMs);
                XmlMs.Seek(0, SeekOrigin.Begin);
                PaymentDetails = strmrdr.ReadToEnd();
                return PaymentDetails;          

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string PullRegistrationDetails(string MyPRN)
        {
            try
            {
                // ---------------------- Validate the Prn -------------------------
                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
                URAGateway.UraPmtService     service = new URAGateway.UraPmtService();
                eTaxPmtLibrary.UraEncryption uraEnc  = new eTaxPmtLibrary.UraEncryption();
                string EnctryptionAlgorith           = "rijndael";
                FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
                FinpayiSecurity.ICrypto       Cryptographer       = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
                MyAPIPass     = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
                MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
                string UraPassword            = Cryptographer.Decrypt(MyAPIPass);
                string s                      = uraEnc.EncryptedData(UraPassword, x509Cert_ura);
                URAGateway.PaymentRegEntity d = service.GetPRNDetails(MyAPIUserName, s, System.Configuration.ConfigurationManager.AppSettings["BANK_SHORT_CODE"], MyPRN);

                // ---------------------- Return Xml Response -------------------------
                string        PaymentDetails = "";
                MemoryStream  XmlMs          = new MemoryStream();
                XmlTextWriter XmlTxtWtr      = new XmlTextWriter(XmlMs, System.Text.Encoding.UTF8);
                XmlTxtWtr.Formatting  = Formatting.Indented;
                XmlTxtWtr.Indentation = 4;
                XmlTxtWtr.WriteStartDocument();
                XmlTxtWtr.WriteStartElement("URA PAYMENT DETAILS");
                XmlTxtWtr.WriteElementString("PRN", d.Prn);
                XmlTxtWtr.WriteElementString("TIN", d.Tin);
                XmlTxtWtr.WriteElementString("CUSTNAME", d.TaxpayerName);
                XmlTxtWtr.WriteElementString("AMOUNT", d.Amount);
                XmlTxtWtr.WriteElementString("STATUS", d.StatusCode);
                XmlTxtWtr.WriteElementString("EXPIRYDATE", d.ExpiryDt);
                XmlTxtWtr.WriteEndElement();
                XmlTxtWtr.WriteEndDocument();
                XmlTxtWtr.Flush();
                StreamReader strmrdr = new StreamReader(XmlMs);
                XmlMs.Seek(0, SeekOrigin.Begin);
                PaymentDetails = strmrdr.ReadToEnd();
                return(PaymentDetails);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
        public String FinpayURA(string CustPRN, string EXREF, string DRACCBRANCH,
                                         string DRACCOUNTNUMBER, string DRAMOUNT, string DRACCURR, string TXNDATE,
                                         string TXNTIME)
        {
            string TINc = "";
            string TaxPayernamec = "";
            string amountduec = "";
            string ExpDatec = "";
            string RegDatec = "";
            string Remarkc = "";

            string PRNResponse = "";
            string OriginalPRN = "";
            string FCDBSetings = "";

            string FCDBINTERNALGL = "";
            string FCDBTELLERCODE = "";
            string FCDBDESC = "";
            string FCDBTILLID = "";
            string FCDBLIMIT = "";
            string FCDBMAKERy = "";
            string FCDBCHECKERy = "";
            string Subhosty = "";
            string Msgtypey = "";
            string Procodey = "";
            string Commissiony = "";
            string Trantypey = "";
            string Terminaly = "";
            string Batchy = "";
            string MessageFlagy = "";
            string Workstationy = "";
            string PosConfirmedy = "";
            string PosReversedy = "";
            string EftSourcey = "";
            string IbTxny = "";
            string Flexuseridy = "";
            string UtilityAccountY = "";
            string UtilityAccBranchY = "";
            string UtilityAccCurrencyY = "";
            string MerchantTypeY = "";

            string statuscodex = "";

            string[] FCDBSetting = null;

            bool isrepeat = false;
            bool istransacted = false;
            bool issuccess = false;
            bool isexisting = false;

            OriginalPRN = CustPRN;

            var FailureDetailsinfo = new URAFCDBResponse();
            var TaxDetailsinfo = new URAFCDBResponse();

            InterfaceUra IU = new InterfaceUra();
             //string results_= IU.PullRegistrationDetails(CustPRN);
            //***** Lets Validate this before even bothering flex.

            try
            {
                // ---------------------- Validate the Prn -------------------------
                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
                URAGateway.UraPmtService service = new URAGateway.UraPmtService();
                eTaxPmtLibrary.UraEncryption uraEnc = new eTaxPmtLibrary.UraEncryption();
                string EnctryptionAlgorith = "rijndael";
                FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
                FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
                MyAPIPass = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
                MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
                string UraPassword = Cryptographer.Decrypt(MyAPIPass);
                string s = uraEnc.EncryptedData(UraPassword, x509Cert_ura);
                URAGateway.PaymentRegEntity res = service.GetPRNDetails(MyAPIUserName, s, System.Configuration.ConfigurationManager.AppSettings["BANK_SHORT_CODE"], CustPRN);

                string remark = "";
                string TaxPayername = res.TaxpayerName;
                string amountdue = res.Amount;
                string TIN = res.Tin;
                string ExpDate = res.ExpiryDt;
                string RegDate = res.PaymentRegDt;
                string statuscode = res.StatusCode;
                if (amountdue != DRAMOUNT)
                {
                    return "UNSUCCESSFUL - INVALID AMOUNT " + DRAMOUNT + " INSTEAD OF " + amountdue;
                }
                Boolean auth = false;

                switch (statuscode)
                {
                    case "A":
                        //auth = true;
                        //remark = "Awaiting To Transact : " + res.StatusDesc;
                        break;
                    case "C":
                        //auth = false;
                        return   "Registration has been cancelled by URA! : " + res.StatusDesc;
                        //break;
                    case "X":
                        //auth = false;
                        return "Registration has expired! : " + res.StatusDesc;
                        //break;
                    case "T":
                        //auth = false;
                        return "Registration has already been transacted! : " + res.StatusDesc;
                        //break;
                    default:
                        //auth = false;
                        return "Results Unknown! : " + res.StatusDesc;
                        //break;
                }
                Datamanagement.PushRegistrationDetails(CustPRN);
                Datamanagement.UpdateURARegDetails(CustPRN, TaxPayername, amountdue, TIN, ExpDate, RegDate, remark, auth, statuscode);
                //urapt.Close();

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            
            //*****
           

            Thread.Sleep(5000);
            //Validate PRN here
            PRNResponse = Datamanagement.getPRNDetails(CustPRN);
            string[] prndets = null;

            prndets = PRNResponse.Split('|');

            string sp_reference = "";

            TINc = prndets[0];
            TaxPayernamec = prndets[1];
            amountduec = prndets[2];
            ExpDatec = prndets[3];
            RegDatec = prndets[4];
            Remarkc = prndets[5];
            statuscodex = prndets[6];

            switch (statuscodex)
            {
                case "A":
                    //Continue
                    break;
                default:
                    //Do Zilch
                    FailureDetailsinfo.EXRef = EXREF;
                    FailureDetailsinfo.PRNNUMBER = CustPRN;
                    FailureDetailsinfo.TaxAmount = DRAMOUNT;
                    FailureDetailsinfo.FailureDetails = new List<UraFailureDetials>();
                    FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                                                              {
                                                                  Remark = Remarkc,
                                                                  statuscode = statuscodex,
                                                                  Responcecode = "05"
                                                              });
                    return "UNSUCCESSFUL";
                    break;
            }

            //Get FCDB details here

            FCDBSetings = Datamanagement.getFCDBSettings("TAX");
            FCDBSetting = FCDBSetings.Split('|');

            FCDBINTERNALGL = FCDBSetting[0];
            FCDBTELLERCODE = FCDBSetting[1];
            FCDBDESC = FCDBSetting[2];
            FCDBTILLID = FCDBSetting[3];
            FCDBLIMIT = FCDBSetting[4];

            FCDBMAKERy = FCDBSetting[5];
            FCDBCHECKERy = FCDBSetting[6];
            Subhosty = FCDBSetting[7];
            Msgtypey = FCDBSetting[8];
            Procodey = FCDBSetting[9];
            Commissiony = FCDBSetting[10];
            Trantypey = FCDBSetting[11];
            Terminaly = FCDBSetting[12];
            Batchy = FCDBSetting[13];
            MessageFlagy = FCDBSetting[14];
            Workstationy = FCDBSetting[15];
            PosConfirmedy = FCDBSetting[16];
            PosReversedy = FCDBSetting[17];
            EftSourcey = FCDBSetting[18];
            IbTxny = FCDBSetting[19];
            Flexuseridy = FCDBSetting[20];
            UtilityAccountY = FCDBSetting[21];
            UtilityAccBranchY = FCDBSetting[22];
            UtilityAccCurrencyY = FCDBSetting[23];
            MerchantTypeY = FCDBSetting[24];


            //Check if already transacted
            istransacted = Datamanagement.isalreadyTransacted(CustPRN);
            switch (istransacted)
            {
                case true:
                    FailureDetailsinfo.EXRef = EXREF;
                    FailureDetailsinfo.PRNNUMBER = CustPRN;
                    FailureDetailsinfo.TaxAmount = DRAMOUNT;
                    FailureDetailsinfo.FailureDetails = new List<UraFailureDetials>();
                    FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                                                              {
                                                                  Remark = "Registration has already been transacted",
                                                                  Responcecode = "05"
                                                              });
                    return "UNSUCCESSFUL";
                    break;
                default:
                    break;
            }

            CustPRN = CustPRN + Trantypey;

            sp_reference = CustPRN + "-" + TaxPayernamec;

            isexisting = Datamanagement.isalreadyExisting(OriginalPRN);
            switch (isexisting)
            {
                case true:
                    //update source as fcdb
                    Datamanagement.UpdatePRNSource(OriginalPRN, "FCDB");
                    break;
                case false:
                    //insert into registration
                    Datamanagement.insertnewfcdbpayment(OriginalPRN, TINc, TaxPayernamec, amountduec, RegDatec, ExpDatec,
                                                        statuscodex, "FCDB");
                    break;
            }

            //update CLEAREDSTATUS here
            //Datamanagement.UpdateRegistration(CustPRN, "CLEARING CHEQUE");
            //check whether transaction already sent to the bridge
            isrepeat = Datamanagement.isalreadyPosted(CustPRN);
            if (isrepeat == false)
            {
                //post to the bridge
                Datamanagement.InsertIntoLog(CustPRN, Subhosty,
                                             Msgtypey, Procodey, TXNDATE, TXNTIME, DRACCBRANCH, CustPRN, CustPRN,
                                             DRAMOUNT, Commissiony, DRACCOUNTNUMBER, MerchantTypeY, EXREF, Terminaly,
                                             DRACCBRANCH,
                                             Batchy, Terminaly, sp_reference, DRACCOUNTNUMBER, DRACCURR, "0",
                                             MessageFlagy, Workstationy,
                                             PosConfirmedy, PosReversedy, "0", UtilityAccountY, UtilityAccBranchY,
                                             MerchantTypeY, EftSourcey, IbTxny, "", Flexuseridy);
            }

            Thread.Sleep(5000);
            issuccess = Datamanagement.isSuccessful(CustPRN);
            EXREF = Datamanagement.Ecternalref(CustPRN);
            Datamanagement.UpdateMSGIDResponded(CustPRN);
            switch (issuccess)
            {
                case true:

                    Datamanagement.UpdateRegistration(OriginalPRN, TXNDATE, FCDBTELLERCODE, DRACCBRANCH, "",
                                                      UtilityAccountY, DRACCOUNTNUMBER, "C", "T", EXREF, false,
                                                      FCDBCHECKERy, "11",
                                                      UtilityAccBranchY, "", Trantypey, DRACCURR, MerchantTypeY,
                                                      UtilityAccCurrencyY);
                    break;

                case false:
                    FailureDetailsinfo.EXRef = EXREF;
                    FailureDetailsinfo.PRNNUMBER = CustPRN;
                    FailureDetailsinfo.TaxAmount = DRAMOUNT;
                    FailureDetailsinfo.FailureDetails = new List<UraFailureDetials>();
                    FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                                                              {
                                                                  Remark = "Failed To Post To Core Banking",
                                                                  Responcecode = "05"
                                                              });
                    return "UNSUCCESSFUL";
                    break;
            }

            //Notify URA - If successfully posted
            Datamanagement.InsertIntoBridgeRegistration(OriginalPRN, "C", TINc, DRAMOUNT, TXNDATE, TXNDATE, "T",
                                                        DRACCBRANCH, CustPRN, "", "");

            //joannah.....9/7/2015
            Thread.Sleep(5000);
            //Notify URA here
             NotifyURA(OriginalPRN, "C", TINc, DRAMOUNT, TXNDATE, TXNDATE, "T",
                                                       DRACCBRANCH, CustPRN, "", "");

             var strUpdate = "Update REGISTRATIONS set Sent = 1, acknowledged = 1 where PRN = prn";

             try
             {
                 using (
                     var conn =
                         new SqlConnection( WebConfigurationManager.ConnectionStrings["Bridge"].ConnectionString))
                 {
                     conn.Open();
                     using (var cmd = new SqlCommand(strUpdate, conn))
                     {
                         var dr = cmd.ExecuteReader();
                     }
                 }
             }

             catch (Exception ex)
             {
                 
             }

             //joannah.....9/7/2015
            
             

            //RESPOND HERE

            TaxDetailsinfo.EXRef = EXREF;
            TaxDetailsinfo.PRNNUMBER = CustPRN;
            TaxDetailsinfo.TaxAmount = amountduec;
            TaxDetailsinfo.TAXDetails = new List<UraDetials>();
            TaxDetailsinfo.TAXDetails.Add(new UraDetials()
                                              {
                                                  amountdue = amountduec,
                                                  TaxPayername = TaxPayernamec,
                                                  TIN = TINc,
                                                  ExpDate = ExpDatec,
                                                  RegDate = RegDatec,
                                                  Remark = Remarkc,
                                                  Responcecode = "00"
                                              });

            return   TaxDetailsinfo.EXRef.ToString();
        }
        //**********
        //public static string NotifyURA(string PRN, string BankStatus, string Tin, string Amount, string Paid_dt,
        //                              string Value_dt, string Status, string Bank_branch_cd, string Bank_tr_no,
        //                              string Chq_no, string Reason)
        //{
        //    var uraEnc = new UraEncryption();

        //    string pass_Api = Decrypt(MyAPIPass);

        //    string s = uraEnc.EncryptedData(MyAPIPass, x509Cert_ura);
        //    var urapt = new UraPmtServiceSoapClient("UraPmtServiceSoap");
        //    urapt.Open();

        //    var MyTransactionEntity = new TransactionEntity();
        //    var MyTranArray = new URAPmtInterface.TransactionEntity[1];
        //    MyTranArray.Initialize();

        //    MyTransactionEntity.Bank_cd = ConfigurationManager.AppSettings["BANK_SHORT_CODE"];
        //    MyTransactionEntity.Prn = PRN;
        //    MyTransactionEntity.Tin = Tin;
        //    MyTransactionEntity.Amount = Amount;
        //    MyTransactionEntity.Paid_dt = Paid_dt;
        //    MyTransactionEntity.Value_dt = Value_dt;
        //    MyTransactionEntity.Status = Status;
        //    MyTransactionEntity.Bank_branch_cd = Bank_branch_cd;
        //    MyTransactionEntity.Bank_tr_no = Bank_tr_no;
        //    MyTransactionEntity.Chq_no = Chq_no;
        //    MyTransactionEntity.Reason = Reason;

        //    string MyString = MyTransactionEntity.Bank_cd + MyTransactionEntity.Prn + MyTransactionEntity.Tin +
        //                      MyTransactionEntity.Amount + MyTransactionEntity.Paid_dt + MyTransactionEntity.Value_dt +
        //                      MyTransactionEntity.Status + MyTransactionEntity.Bank_branch_cd +
        //                      MyTransactionEntity.Bank_tr_no + MyTransactionEntity.Chq_no + MyTransactionEntity.Reason;


        //    var privateKey = x509Cert_bnk.PrivateKey as RSACryptoServiceProvider;
        //    byte[] buffer = new UnicodeEncoding().GetBytes(MyString);
        //    byte[] hash = new SHA1Managed().ComputeHash(buffer);
        //    if (privateKey != null)
        //    {
        //        byte[] signature = privateKey.SignHash(hash, "SHA1");

        //        //verify the signature
        //        var publicKey = x509Cert_bnk.PublicKey.Key as RSACryptoServiceProvider;
        //        bool verify = publicKey != null && publicKey.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);

        //        MyTransactionEntity.Signature = signature;
        //    }

        //    //Add Transaction Entity to the Array
        //    MyTranArray[0] = MyTransactionEntity;

           // var res = urapt.NotifyUraPayment(MyAPIUserName, s, MyTranArray);

        //    return "";
        //}
        //**********

        //**********
        //static 

        public string NotifyURA(string PRN, string BankStatus, string Tin, string Amount, string Paid_dt,
                                      string Value_dt, string Status, string Bank_branch_cd, string Bank_tr_no,
                                   string Chq_no, string Reason)
       {
           Functions fn = new Functions();
           //Crypto cp = new Crypto();
           string EnctryptionAlgorithm = "rijndael";
           FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
           FinpayiSecurity.ICrypto CrypTool = null;
           //FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);

           string Finpayi = ConfigurationManager.ConnectionStrings["Finpay"].ConnectionString;
           string Bridgei = ConfigurationManager.ConnectionStrings["Bridge"].ConnectionString;
           X509Certificate2 x509Cert_ura = new X509Certificate2(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["URACertificate"]));
           X509Certificate2 x509Cert_bnk = new X509Certificate2(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["BankCertificate"]), HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["BankCertPass"]));

           string MyAPIUserName = "";
           string MyAPIPass = "";


           System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
           URAGateway.UraPmtService service = new URAGateway.UraPmtService();
           eTaxPmtLibrary.UraEncryption uraEnc = new eTaxPmtLibrary.UraEncryption();
           string EnctryptionAlgorith = "rijndael";
           //FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
           FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
           MyAPIPass = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
           MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
           string UraPassword = Cryptographer.Decrypt(MyAPIPass);
           string s = uraEnc.EncryptedData(UraPassword, x509Cert_ura);



            
        //    var urapt = new UraPmtServiceSoapClient("UraPmtServiceSoap");
        //    urapt.Open();

          var MyTransactionEntity = new URAGateway.TransactionEntity ();
          var MyTranArray = new URAGateway.TransactionEntity[1];
          MyTranArray.Initialize();

          MyTransactionEntity.Bank_cd = ConfigurationManager.AppSettings["BANK_SHORT_CODE"];
          MyTransactionEntity.Prn = PRN;
          MyTransactionEntity.Tin = Tin;
          MyTransactionEntity.Amount = Amount;
          MyTransactionEntity.Paid_dt = Paid_dt;
          MyTransactionEntity.Value_dt = Value_dt;
          MyTransactionEntity.Status = Status;
          MyTransactionEntity.Bank_branch_cd = Bank_branch_cd;
          MyTransactionEntity.Bank_tr_no = Bank_tr_no;
          MyTransactionEntity.Chq_no = Chq_no;
          MyTransactionEntity.Reason = Reason;

          string MyString = MyTransactionEntity.Bank_cd + MyTransactionEntity.Prn + MyTransactionEntity.Tin +
                            MyTransactionEntity.Amount + MyTransactionEntity.Paid_dt + MyTransactionEntity.Value_dt +
                            MyTransactionEntity.Status + MyTransactionEntity.Bank_branch_cd +
                            MyTransactionEntity.Bank_tr_no + MyTransactionEntity.Chq_no + MyTransactionEntity.Reason;


          var privateKey = x509Cert_bnk.PrivateKey as RSACryptoServiceProvider;
          byte[] buffer = new UnicodeEncoding().GetBytes(MyString);
          byte[] hash = new SHA1Managed().ComputeHash(buffer);
          if (privateKey != null)
          {
              byte[] signature = privateKey.SignHash(hash, "SHA1");

              //verify the signature
              var publicKey = x509Cert_bnk.PublicKey.Key as RSACryptoServiceProvider;
              bool verify = publicKey != null && publicKey.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);

              MyTransactionEntity.Signature = signature;
          }

          //Add Transaction Entity to the Array
          MyTranArray[0] = MyTransactionEntity;

          var res = service.NotifyUraPayment(MyAPIUserName, s, MyTranArray);

          return "";
        }
        public String FinpayURA(string CustPRN, string EXREF, string DRACCBRANCH,
                                string DRACCOUNTNUMBER, string DRAMOUNT, string DRACCURR, string TXNDATE,
                                string TXNTIME)
        {
            string TINc          = "";
            string TaxPayernamec = "";
            string amountduec    = "";
            string ExpDatec      = "";
            string RegDatec      = "";
            string Remarkc       = "";

            string PRNResponse = "";
            string OriginalPRN = "";
            string FCDBSetings = "";

            string FCDBINTERNALGL      = "";
            string FCDBTELLERCODE      = "";
            string FCDBDESC            = "";
            string FCDBTILLID          = "";
            string FCDBLIMIT           = "";
            string FCDBMAKERy          = "";
            string FCDBCHECKERy        = "";
            string Subhosty            = "";
            string Msgtypey            = "";
            string Procodey            = "";
            string Commissiony         = "";
            string Trantypey           = "";
            string Terminaly           = "";
            string Batchy              = "";
            string MessageFlagy        = "";
            string Workstationy        = "";
            string PosConfirmedy       = "";
            string PosReversedy        = "";
            string EftSourcey          = "";
            string IbTxny              = "";
            string Flexuseridy         = "";
            string UtilityAccountY     = "";
            string UtilityAccBranchY   = "";
            string UtilityAccCurrencyY = "";
            string MerchantTypeY       = "";

            string statuscodex = "";

            string[] FCDBSetting = null;

            bool isrepeat     = false;
            bool istransacted = false;
            bool issuccess    = false;
            bool isexisting   = false;

            OriginalPRN = CustPRN;

            var FailureDetailsinfo = new URAFCDBResponse();
            var TaxDetailsinfo     = new URAFCDBResponse();

            InterfaceUra IU = new InterfaceUra();

            //string results_= IU.PullRegistrationDetails(CustPRN);
            //***** Lets Validate this before even bothering flex.

            try
            {
                // ---------------------- Validate the Prn -------------------------
                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
                URAGateway.UraPmtService     service = new URAGateway.UraPmtService();
                eTaxPmtLibrary.UraEncryption uraEnc  = new eTaxPmtLibrary.UraEncryption();
                string EnctryptionAlgorith           = "rijndael";
                FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
                FinpayiSecurity.ICrypto       Cryptographer       = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
                MyAPIPass     = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
                MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
                string UraPassword = Cryptographer.Decrypt(MyAPIPass);
                string s           = uraEnc.EncryptedData(UraPassword, x509Cert_ura);
                URAGateway.PaymentRegEntity res = service.GetPRNDetails(MyAPIUserName, s, System.Configuration.ConfigurationManager.AppSettings["BANK_SHORT_CODE"], CustPRN);

                string remark       = "";
                string TaxPayername = res.TaxpayerName;
                string amountdue    = res.Amount;
                string TIN          = res.Tin;
                string ExpDate      = res.ExpiryDt;
                string RegDate      = res.PaymentRegDt;
                string statuscode   = res.StatusCode;
                if (amountdue != DRAMOUNT)
                {
                    return("UNSUCCESSFUL - INVALID AMOUNT " + DRAMOUNT + " INSTEAD OF " + amountdue);
                }
                Boolean auth = false;

                switch (statuscode)
                {
                case "A":
                    //auth = true;
                    //remark = "Awaiting To Transact : " + res.StatusDesc;
                    break;

                case "C":
                    //auth = false;
                    return("Registration has been cancelled by URA! : " + res.StatusDesc);

                //break;
                case "X":
                    //auth = false;
                    return("Registration has expired! : " + res.StatusDesc);

                //break;
                case "T":
                    //auth = false;
                    return("Registration has already been transacted! : " + res.StatusDesc);

                //break;
                default:
                    //auth = false;
                    return("Results Unknown! : " + res.StatusDesc);
                    //break;
                }
                Datamanagement.PushRegistrationDetails(CustPRN);
                Datamanagement.UpdateURARegDetails(CustPRN, TaxPayername, amountdue, TIN, ExpDate, RegDate, remark, auth, statuscode);
                //urapt.Close();
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            //*****


            Thread.Sleep(5000);
            //Validate PRN here
            PRNResponse = Datamanagement.getPRNDetails(CustPRN);
            string[] prndets = null;

            prndets = PRNResponse.Split('|');

            string sp_reference = "";

            TINc          = prndets[0];
            TaxPayernamec = prndets[1];
            amountduec    = prndets[2];
            ExpDatec      = prndets[3];
            RegDatec      = prndets[4];
            Remarkc       = prndets[5];
            statuscodex   = prndets[6];

            switch (statuscodex)
            {
            case "A":
                //Continue
                break;

            default:
                //Do Zilch
                FailureDetailsinfo.EXRef          = EXREF;
                FailureDetailsinfo.PRNNUMBER      = CustPRN;
                FailureDetailsinfo.TaxAmount      = DRAMOUNT;
                FailureDetailsinfo.FailureDetails = new List <UraFailureDetials>();
                FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                {
                    Remark       = Remarkc,
                    statuscode   = statuscodex,
                    Responcecode = "05"
                });
                return("UNSUCCESSFUL");

                break;
            }

            //Get FCDB details here

            FCDBSetings = Datamanagement.getFCDBSettings("TAX");
            FCDBSetting = FCDBSetings.Split('|');

            FCDBINTERNALGL = FCDBSetting[0];
            FCDBTELLERCODE = FCDBSetting[1];
            FCDBDESC       = FCDBSetting[2];
            FCDBTILLID     = FCDBSetting[3];
            FCDBLIMIT      = FCDBSetting[4];

            FCDBMAKERy          = FCDBSetting[5];
            FCDBCHECKERy        = FCDBSetting[6];
            Subhosty            = FCDBSetting[7];
            Msgtypey            = FCDBSetting[8];
            Procodey            = FCDBSetting[9];
            Commissiony         = FCDBSetting[10];
            Trantypey           = FCDBSetting[11];
            Terminaly           = FCDBSetting[12];
            Batchy              = FCDBSetting[13];
            MessageFlagy        = FCDBSetting[14];
            Workstationy        = FCDBSetting[15];
            PosConfirmedy       = FCDBSetting[16];
            PosReversedy        = FCDBSetting[17];
            EftSourcey          = FCDBSetting[18];
            IbTxny              = FCDBSetting[19];
            Flexuseridy         = FCDBSetting[20];
            UtilityAccountY     = FCDBSetting[21];
            UtilityAccBranchY   = FCDBSetting[22];
            UtilityAccCurrencyY = FCDBSetting[23];
            MerchantTypeY       = FCDBSetting[24];


            //Check if already transacted
            istransacted = Datamanagement.isalreadyTransacted(CustPRN);
            switch (istransacted)
            {
            case true:
                FailureDetailsinfo.EXRef          = EXREF;
                FailureDetailsinfo.PRNNUMBER      = CustPRN;
                FailureDetailsinfo.TaxAmount      = DRAMOUNT;
                FailureDetailsinfo.FailureDetails = new List <UraFailureDetials>();
                FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                {
                    Remark       = "Registration has already been transacted",
                    Responcecode = "05"
                });
                return("UNSUCCESSFUL");

                break;

            default:
                break;
            }

            CustPRN = CustPRN + Trantypey;

            sp_reference = CustPRN + "-" + TaxPayernamec;

            isexisting = Datamanagement.isalreadyExisting(OriginalPRN);
            switch (isexisting)
            {
            case true:
                //update source as fcdb
                Datamanagement.UpdatePRNSource(OriginalPRN, "FCDB");
                break;

            case false:
                //insert into registration
                Datamanagement.insertnewfcdbpayment(OriginalPRN, TINc, TaxPayernamec, amountduec, RegDatec, ExpDatec,
                                                    statuscodex, "FCDB");
                break;
            }

            //update CLEAREDSTATUS here
            //Datamanagement.UpdateRegistration(CustPRN, "CLEARING CHEQUE");
            //check whether transaction already sent to the bridge
            isrepeat = Datamanagement.isalreadyPosted(CustPRN);
            if (isrepeat == false)
            {
                //post to the bridge
                Datamanagement.InsertIntoLog(CustPRN, Subhosty,
                                             Msgtypey, Procodey, TXNDATE, TXNTIME, DRACCBRANCH, CustPRN, CustPRN,
                                             DRAMOUNT, Commissiony, DRACCOUNTNUMBER, MerchantTypeY, EXREF, Terminaly,
                                             DRACCBRANCH,
                                             Batchy, Terminaly, sp_reference, DRACCOUNTNUMBER, DRACCURR, "0",
                                             MessageFlagy, Workstationy,
                                             PosConfirmedy, PosReversedy, "0", UtilityAccountY, UtilityAccBranchY,
                                             MerchantTypeY, EftSourcey, IbTxny, "", Flexuseridy);
            }

            Thread.Sleep(5000);
            issuccess = Datamanagement.isSuccessful(CustPRN);
            EXREF     = Datamanagement.Ecternalref(CustPRN);
            Datamanagement.UpdateMSGIDResponded(CustPRN);
            switch (issuccess)
            {
            case true:

                Datamanagement.UpdateRegistration(OriginalPRN, TXNDATE, FCDBTELLERCODE, DRACCBRANCH, "",
                                                  UtilityAccountY, DRACCOUNTNUMBER, "C", "T", EXREF, false,
                                                  FCDBCHECKERy, "11",
                                                  UtilityAccBranchY, "", Trantypey, DRACCURR, MerchantTypeY,
                                                  UtilityAccCurrencyY);
                break;

            case false:
                FailureDetailsinfo.EXRef          = EXREF;
                FailureDetailsinfo.PRNNUMBER      = CustPRN;
                FailureDetailsinfo.TaxAmount      = DRAMOUNT;
                FailureDetailsinfo.FailureDetails = new List <UraFailureDetials>();
                FailureDetailsinfo.FailureDetails.Add(new UraFailureDetials()
                {
                    Remark       = "Failed To Post To Core Banking",
                    Responcecode = "05"
                });
                return("UNSUCCESSFUL");

                break;
            }

            //Notify URA - If successfully posted
            Datamanagement.InsertIntoBridgeRegistration(OriginalPRN, "C", TINc, DRAMOUNT, TXNDATE, TXNDATE, "T",
                                                        DRACCBRANCH, CustPRN, "", "");

            //joannah.....9/7/2015
            Thread.Sleep(5000);
            //Notify URA here
            NotifyURA(OriginalPRN, "C", TINc, DRAMOUNT, TXNDATE, TXNDATE, "T",
                      DRACCBRANCH, CustPRN, "", "");

            var strUpdate = "Update REGISTRATIONS set Sent = 1, acknowledged = 1 where PRN = prn";

            try
            {
                using (
                    var conn =
                        new SqlConnection(WebConfigurationManager.ConnectionStrings["Bridge"].ConnectionString))
                {
                    conn.Open();
                    using (var cmd = new SqlCommand(strUpdate, conn))
                    {
                        var dr = cmd.ExecuteReader();
                    }
                }
            }

            catch (Exception ex)
            {
            }

            //joannah.....9/7/2015



            //RESPOND HERE

            TaxDetailsinfo.EXRef      = EXREF;
            TaxDetailsinfo.PRNNUMBER  = CustPRN;
            TaxDetailsinfo.TaxAmount  = amountduec;
            TaxDetailsinfo.TAXDetails = new List <UraDetials>();
            TaxDetailsinfo.TAXDetails.Add(new UraDetials()
            {
                amountdue    = amountduec,
                TaxPayername = TaxPayernamec,
                TIN          = TINc,
                ExpDate      = ExpDatec,
                RegDate      = RegDatec,
                Remark       = Remarkc,
                Responcecode = "00"
            });

            return(TaxDetailsinfo.EXRef.ToString());
        }
        //**********
        //public static string NotifyURA(string PRN, string BankStatus, string Tin, string Amount, string Paid_dt,
        //                              string Value_dt, string Status, string Bank_branch_cd, string Bank_tr_no,
        //                              string Chq_no, string Reason)
        //{
        //    var uraEnc = new UraEncryption();

        //    string pass_Api = Decrypt(MyAPIPass);

        //    string s = uraEnc.EncryptedData(MyAPIPass, x509Cert_ura);
        //    var urapt = new UraPmtServiceSoapClient("UraPmtServiceSoap");
        //    urapt.Open();

        //    var MyTransactionEntity = new TransactionEntity();
        //    var MyTranArray = new URAPmtInterface.TransactionEntity[1];
        //    MyTranArray.Initialize();

        //    MyTransactionEntity.Bank_cd = ConfigurationManager.AppSettings["BANK_SHORT_CODE"];
        //    MyTransactionEntity.Prn = PRN;
        //    MyTransactionEntity.Tin = Tin;
        //    MyTransactionEntity.Amount = Amount;
        //    MyTransactionEntity.Paid_dt = Paid_dt;
        //    MyTransactionEntity.Value_dt = Value_dt;
        //    MyTransactionEntity.Status = Status;
        //    MyTransactionEntity.Bank_branch_cd = Bank_branch_cd;
        //    MyTransactionEntity.Bank_tr_no = Bank_tr_no;
        //    MyTransactionEntity.Chq_no = Chq_no;
        //    MyTransactionEntity.Reason = Reason;

        //    string MyString = MyTransactionEntity.Bank_cd + MyTransactionEntity.Prn + MyTransactionEntity.Tin +
        //                      MyTransactionEntity.Amount + MyTransactionEntity.Paid_dt + MyTransactionEntity.Value_dt +
        //                      MyTransactionEntity.Status + MyTransactionEntity.Bank_branch_cd +
        //                      MyTransactionEntity.Bank_tr_no + MyTransactionEntity.Chq_no + MyTransactionEntity.Reason;


        //    var privateKey = x509Cert_bnk.PrivateKey as RSACryptoServiceProvider;
        //    byte[] buffer = new UnicodeEncoding().GetBytes(MyString);
        //    byte[] hash = new SHA1Managed().ComputeHash(buffer);
        //    if (privateKey != null)
        //    {
        //        byte[] signature = privateKey.SignHash(hash, "SHA1");

        //        //verify the signature
        //        var publicKey = x509Cert_bnk.PublicKey.Key as RSACryptoServiceProvider;
        //        bool verify = publicKey != null && publicKey.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);

        //        MyTransactionEntity.Signature = signature;
        //    }

        //    //Add Transaction Entity to the Array
        //    MyTranArray[0] = MyTransactionEntity;

        // var res = urapt.NotifyUraPayment(MyAPIUserName, s, MyTranArray);

        //    return "";
        //}
        //**********

        //**********
        //static

        public string NotifyURA(string PRN, string BankStatus, string Tin, string Amount, string Paid_dt,
                                string Value_dt, string Status, string Bank_branch_cd, string Bank_tr_no,
                                string Chq_no, string Reason)
        {
            Functions fn = new Functions();
            //Crypto cp = new Crypto();
            string EnctryptionAlgorithm = "rijndael";

            FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
            FinpayiSecurity.ICrypto       CrypTool            = null;
            //FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);

            string           Finpayi      = ConfigurationManager.ConnectionStrings["Finpay"].ConnectionString;
            string           Bridgei      = ConfigurationManager.ConnectionStrings["Bridge"].ConnectionString;
            X509Certificate2 x509Cert_ura = new X509Certificate2(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["URACertificate"]));
            X509Certificate2 x509Cert_bnk = new X509Certificate2(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["BankCertificate"]), HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["BankCertPass"]));

            string MyAPIUserName = "";
            string MyAPIPass     = "";


            System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
            URAGateway.UraPmtService     service = new URAGateway.UraPmtService();
            eTaxPmtLibrary.UraEncryption uraEnc  = new eTaxPmtLibrary.UraEncryption();
            string EnctryptionAlgorith           = "rijndael";

            //FinpayiSecurity.CryptoFactory CryptographyFactory = new FinpayiSecurity.CryptoFactory();
            FinpayiSecurity.ICrypto Cryptographer = CryptographyFactory.MakeCryptographer(EnctryptionAlgorith);
            MyAPIPass     = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBPASS'");
            MyAPIUserName = fn.GetFinpayParameter("Select ItemValue from Parameters where ItemKey = 'URAWEBUSER'");
            string UraPassword = Cryptographer.Decrypt(MyAPIPass);
            string s           = uraEnc.EncryptedData(UraPassword, x509Cert_ura);



            //    var urapt = new UraPmtServiceSoapClient("UraPmtServiceSoap");
            //    urapt.Open();

            var MyTransactionEntity = new URAGateway.TransactionEntity();
            var MyTranArray         = new URAGateway.TransactionEntity[1];

            MyTranArray.Initialize();

            MyTransactionEntity.Bank_cd        = ConfigurationManager.AppSettings["BANK_SHORT_CODE"];
            MyTransactionEntity.Prn            = PRN;
            MyTransactionEntity.Tin            = Tin;
            MyTransactionEntity.Amount         = Amount;
            MyTransactionEntity.Paid_dt        = Paid_dt;
            MyTransactionEntity.Value_dt       = Value_dt;
            MyTransactionEntity.Status         = Status;
            MyTransactionEntity.Bank_branch_cd = Bank_branch_cd;
            MyTransactionEntity.Bank_tr_no     = Bank_tr_no;
            MyTransactionEntity.Chq_no         = Chq_no;
            MyTransactionEntity.Reason         = Reason;

            string MyString = MyTransactionEntity.Bank_cd + MyTransactionEntity.Prn + MyTransactionEntity.Tin +
                              MyTransactionEntity.Amount + MyTransactionEntity.Paid_dt + MyTransactionEntity.Value_dt +
                              MyTransactionEntity.Status + MyTransactionEntity.Bank_branch_cd +
                              MyTransactionEntity.Bank_tr_no + MyTransactionEntity.Chq_no + MyTransactionEntity.Reason;


            var privateKey = x509Cert_bnk.PrivateKey as RSACryptoServiceProvider;

            byte[] buffer = new UnicodeEncoding().GetBytes(MyString);
            byte[] hash   = new SHA1Managed().ComputeHash(buffer);
            if (privateKey != null)
            {
                byte[] signature = privateKey.SignHash(hash, "SHA1");

                //verify the signature
                var  publicKey = x509Cert_bnk.PublicKey.Key as RSACryptoServiceProvider;
                bool verify    = publicKey != null && publicKey.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);

                MyTransactionEntity.Signature = signature;
            }

            //Add Transaction Entity to the Array
            MyTranArray[0] = MyTransactionEntity;

            var res = service.NotifyUraPayment(MyAPIUserName, s, MyTranArray);

            return("");
        }