Пример #1
0
        public void Respond()
        {
            PosInterface posObj   = new PosInterface();
            Transaction  transObj = new Transaction();

            Thread.Sleep(400);
            try
            {
                //transObj.WriteLog("request ISO message: {"+ Encoding.ASCII.GetString(msg)+"}");
                IsoMessage request  = mfact.ParseMessage(msg, 0);
                IsoMessage response = new IsoMessage();

                switch (request.Type.ToString("x"))
                {
                //pos request logon/logoff
                case "800":
                {
                    response = posObj.PosSignOnOff(request);
                    break;
                }

                //pos request download
                case "200":
                {
                    response = posObj.Download(request);
                    break;
                }

                //pos logon
                default:
                {
                    response = posObj.PosSignOnOff(request);
                    break;
                }
                }

                //Response message
                byte[] outGoing = response.getByte(4, true);
                Console.Out.WriteLine(System.Text.Encoding.ASCII.GetString(outGoing));
                response.Write(sock.GetStream(), 4, true);


                //Console.Out.WriteLine("Sending response conf {0}", response.GetField(39));
                ////response.Write(sock.GetStream(), 4, true);
                //Console.Out.WriteLine("Parsing outgoing: {0}", Encoding.ASCII.GetString(response.getByte(4, false)));
                //byte[] outGoing = response.getByte(4, true);
                //sock.GetStream().Write(outGoing, 0, outGoing.Length);
                //IsoMessage OutGoingMSG = mfact.ParseMessage(outGoing, 4);
                //PrintMessage(incoming);
                //PrintMessage(OutGoingMSG);



                //<parse type="0800">
                //<field num="2" type="LLVAR" length="0" />     <!--mã đại lý -->
                //<field num="7" type="NUMERIC" length="10"/>     <!--Transaction Date and Time -->
                //<field num="11" type="NUMERIC" length="6" />   <!--System Trace-->
                //<field num="18" type="NUMERIC" length="4" />    <!--Merchant Type: Giá trị là 6011 đối với POS-->
                //<field num="32" type="LLVAR" length="0" />      <!--Acquiring Institution Identification Code-->
                //<field num="48" type="LLVAR" length="0" />    <!--nội dung hướng dẫn -->
                //<field num="52" type="LLLVAR" length="0" />     <!--Đưa mã máy POS vào-->
                //<field num="70" type="LLLVAR" length="0" />    <!--kiểu network request cần xử lý-	001: Signon-	002: Signoff-	161: Key Exchange-->
                // </parse>
            }
            catch (IOException ex)
            {
                transObj.WriteLog("Fail to response, ex=" + ex.ToString());
            }
        }
Пример #2
0
        //private static bool SendMessage(string MSISDN, Int32 Amount, string Counter)
        public bool SendMessage(string MSISDN, Int32 Amount, string Counter, ref OutputVTload OutputVTObj)
        {
            //Send ISO message to VietTel Topup Gateway
            //ChungNN 02/2009 --------
            //----------------------------------------
            TransferProcess transObj       = new TransferProcess();
            bool            blnReturnValue = false;

            try
            {
                Encrypt.DigitalSign SignObj = new DigitalSign(AppConfiguration.AppPath + AppConfiguration.ViettelCerFilePath, AppConfiguration.AppPath + AppConfiguration.AppRSAPrivateKeyFilePath, AppConfiguration.AppRSAPrivateKeyPassword);
                string strDigitalSign       = string.Empty;


                MessageFactory mfact = ConfigParser.CreateFromFile(AppConfiguration.AppPath + AppConfiguration.ViettelConfig_ISOFile);
                mfact.AssignDate = true;
                //mfact.TraceGenerator = new i
                IsoMessage m = null;
                TcpClient  sock;

                byte[] lenbuf = new byte[1024];
                try
                {
                    sock = new TcpClient(AppConfiguration.ViettelServerAddress, AppConfiguration.ViettelServerPort);
                }
                catch
                {
                    return(blnReturnValue);
                    //throw (ex);
                }
                if ((sock == null) || !sock.Connected)
                {
                    return(blnReturnValue);
                }

                //if (MSISDN.Substring(0, 2) != "84")
                //    MSISDN = "84" + MSISDN;

                //fill ISO8583 message fields
                m = mfact.NewMessage(0x200);
                mfact.Setfield(2, MSISDN, ref m);
                mfact.Setfield(3, 0, ref m);
                mfact.Setfield(4, Amount, ref m);
                mfact.Setfield(7, DateTime.Now.ToString("yyyyMMddHHMMss"), ref m);
                mfact.Setfield(11, Counter, ref m);
                mfact.Setfield(63, AppConfiguration.ViettelClientID, ref m);

                //create Digital Signature
                if (m.HasField(2) && m.GetField(2) != null)
                {
                    strDigitalSign += m.GetField(2).ToString();
                }
                if (m.HasField(3) && m.GetField(3) != null)
                {
                    strDigitalSign += m.GetField(3).ToString();
                }
                if (m.HasField(4) && m.GetField(4) != null)
                {
                    strDigitalSign += m.GetField(4).ToString();
                }
                if (m.HasField(7) && m.GetField(7) != null)
                {
                    strDigitalSign += m.GetField(7).ToString();
                }
                if (m.HasField(11) && m.GetField(11) != null)
                {
                    strDigitalSign += m.GetField(11).ToString();
                }
                if (m.HasField(63) && m.GetField(63) != null)
                {
                    strDigitalSign += m.GetField(63).ToString();
                }

                OutputVTObj.request_transaction_time = m.GetField(7).ToString();
                //verify du lieu nhan dc tu server
                //SignObj._VerifyData("123456", strOub);

                //sign du lieu truoc khi gui di
                SignObj._signData(strDigitalSign, ref strDigitalSign);

                mfact.Setfield(64, strDigitalSign, ref m);
                m.Write(sock.GetStream(), 4, false);

                //<field num="2" type="LLVAR" length="0" />     <!--MSISDN-->
                //<field num="3" type="NUMERIC" length="6" />     <!--Processing Code-->
                //<field num="4" type="NUMERIC" length="12" />    <!--Transaction Amount-->
                //<field num="7" type="NUMERIC" length="14"/>     <!--Transmission Date & Time-->
                //<field num="11" type="NUMERIC" length="15" />   <!--System Trace Audit Number-->
                //<field num="39" type="NUMERIC" length="2" />    <!--Response Code-->
                //<field num="63" type="LLVAR" length="0" />      <!--Client ID-->
                //<field num="64" type="LLLVAR" length="0" />     <!--Message Signature-->

                //send ISO8583 message
                //Console.Out.Write(Encoding.ASCII.GetString(m.getByte(4, false)));

                Thread.Sleep(AppConfiguration.ViettelTimeout);

                while ((sock != null) && sock.Connected == true)
                {
                    try
                    {
                        Thread.Sleep(1000);
                        if (sock.GetStream().Read(lenbuf, 0, 4) == 4)
                        {
                            int size, k;
                            size = 0;

                            for (k = 0; k < 4; k++)
                            {
                                size = size + (int)(lenbuf[k] - 48) * (int)(Math.Pow(10, 3 - k));
                            }

                            byte[] buf = new byte[size + 1];
                            //We're not expecting ETX in this case
                            sock.GetStream().Read(buf, 0, buf.Length);

                            //Console.Out.Write(Encoding.ASCII.GetString(buf));
                            IsoMessage incoming = mfact.ParseMessage(buf, 0);

                            if (Convert.ToInt16(incoming.GetField(39).Value) == 0)
                            {
                                blnReturnValue = true;
                                //Ghi giao dich phuc vu doi soat
                                OutputVTObj.response_transaction_time = incoming.GetField(7).ToString();  //Response Transmission Time
                                OutputVTObj.response_code             = incoming.GetField(39).ToString(); //Response Code
                            }
                            else
                            {
                                //Ghi giao dich phuc vu doi soat
                                OutputVTObj.response_transaction_time = incoming.GetField(7).ToString();  //Response Transmission Time
                                OutputVTObj.response_code             = incoming.GetField(39).ToString(); //Response Code
                                blnReturnValue = true;
                            }
                        }
                        else
                        {
                            OutputVTObj.response_transaction_time = ""; //Response Transmission Time
                            OutputVTObj.response_code             = ""; //Response Code
                            blnReturnValue = false;
                        }
                    }
                    catch
                    {
                        //Console.Out.Write(ex.ToString());
                        blnReturnValue = false;
                    }
                }
            }
            catch //(Exception ex)
            {
                blnReturnValue = false;
            }

            return(blnReturnValue);
        }