Пример #1
0
        public void TestSimpleFieldSetter()
        {
            IsoMessage iso = mf.NewMessage(0x200);
            IsoValue   f3  = iso.GetField(3);

            iso.UpdateValue(3, "999999");
            Assert.Equal("999999", iso.GetObjectValue(3));
            IsoValue nf3 = iso.GetField(3);

            Assert.NotSame(f3, nf3);
            Assert.Equal(f3.Type, nf3.Type);
            Assert.Equal(f3.Length, nf3.Length);
            Assert.Same(f3.Encoder, nf3.Encoder);
            Assert.Throws <ArgumentException>(() => iso.UpdateValue(4, "INVALID!"))
            ;
        }
Пример #2
0
        public void TestTemplating()
        {
            IsoMessage iso1 = mf.NewMessage(0x200);
            IsoMessage iso2 = mf.NewMessage(0x200);

            Assert.NotSame(iso1, iso2);
            Assert.Equal(iso1.GetObjectValue(3), iso2.GetObjectValue(3));
            Assert.NotSame(iso1.GetField(3), iso2.GetField(3));
            Assert.NotSame(iso1.GetField(48), iso2.GetField(48));
            CustomField48 cf48_1 = (CustomField48)iso1.GetObjectValue(48);
            int           origv  = cf48_1.V2;

            cf48_1.V2 = (origv + 1000);
            CustomField48 cf48_2 = (CustomField48)iso2.GetObjectValue(48);

            Assert.Same(cf48_1, cf48_2);
            Assert.Equal(cf48_2.V2, origv + 1000);
        }
Пример #3
0
        public void TestParser()
        {
            string configXml = @"/Resources/config.xml";
            MessageFactory <IsoMessage> mfact = Config(configXml);

            //Headers
            Assert.NotNull(mfact.GetIsoHeader(0x800));
            Assert.NotNull(mfact.GetIsoHeader(0x810));
            Assert.Equal(mfact.GetIsoHeader(0x800), mfact.GetIsoHeader(0x810));

            //Templates
            IsoMessage m200 = mfact.GetMessageTemplate(0x200);

            Assert.NotNull(m200);
            IsoMessage m400 = mfact.GetMessageTemplate(0x400);

            Assert.NotNull(m400);

            for (int i = 2; i < 89; i++)
            {
                IsoValue v = m200.GetField(i);
                if (v == null)
                {
                    Assert.False(m400.HasField(i));
                }
                else
                {
                    Assert.True(m400.HasField(i));
                    Assert.Equal(v, m400.GetField(i));
                }
            }

            Assert.False(m200.HasField(90));
            Assert.True(m400.HasField(90));
            Assert.True(m200.HasField(102));
            Assert.False(m400.HasField(102));

            //Parsing guides
            string     s800 = "0800201080000000000012345611251125";
            string     s810 = "08102010000002000000123456112500";
            IsoMessage m    = mfact.ParseMessage(s800.GetSbytes(), 0);

            Assert.NotNull(m);
            Assert.True(m.HasField(3));
            Assert.True(m.HasField(12));
            Assert.True(m.HasField(17));
            Assert.False(m.HasField(39));
            m = mfact.ParseMessage(s810.GetSbytes(),
                                   0);
            Assert.NotNull(m);
            Assert.True(m.HasField(3));
            Assert.True(m.HasField(12));
            Assert.False(m.HasField(17));
            Assert.True(m.HasField(39));
        }
Пример #4
0
        public void TestExtendCompositeWithSameField()
        {
            string configXml = @"/Resources/issue47.xml";
            MessageFactory <IsoMessage> mfact = Config(configXml);

            string m200 = "02001000000000000004000000100000013ABCDEFGHIJKLM";

            IsoMessage isoMessage = mfact.ParseMessage(m200.GetSbytes(), 0);

            // check field num 4
            IsoValue field4 = isoMessage.GetField(4);

            Assert.Equal(IsoType.AMOUNT, field4.Type);
            Assert.Equal(IsoType.AMOUNT.Length(), field4.Length);

            // check nested field num 4 from composite field 62
            CompositeField compositeField62 = (CompositeField)isoMessage.GetField(62).Value;
            IsoValue       nestedField4     = compositeField62.GetField(0); // first in list

            Assert.Equal(IsoType.ALPHA, nestedField4.Type);
            Assert.Equal(13, nestedField4.Length);
        }
Пример #5
0
        private static void ParseTemplates <T>(XmlNodeList nodes,
                                               MessageFactory <T> mfact) where T : IsoMessage
        {
            List <XmlElement> subs = null;

            for (var i = 0; i < nodes.Count; i++)
            {
                var elem = (XmlElement)nodes.Item(i);
                if (elem == null)
                {
                    continue;
                }
                var type = ParseType(elem.GetAttribute("type"));
                if (type == -1)
                {
                    throw new IOException("Invalid ISO8583 type for template: " + elem.GetAttribute("type"));
                }
                if (!elem.GetAttribute("extends").IsEmpty())
                {
                    subs ??= new List <XmlElement>(nodes.Count - i);
                    subs.Add(elem);
                    continue;
                }

                var m = (T) new IsoMessage();
                m.Type     = type;
                m.Encoding = mfact.Encoding;
                var fields = elem.GetElementsByTagName("field");

                for (var j = 0; j < fields.Count; j++)
                {
                    var f = (XmlElement)fields.Item(j);
                    if (f?.ParentNode != elem)
                    {
                        continue;
                    }
                    var num = int.Parse(f.GetAttribute("num"));

                    var v = GetTemplateField(
                        f,
                        mfact,
                        true);

                    if (v != null)
                    {
                        v.Encoding = mfact.Encoding;
                    }

                    m.SetField(
                        num,
                        v);
                }

                mfact.AddMessageTemplate(m);
            }

            if (subs == null)
            {
                return;
            }

            foreach (var elem in subs)
            {
                var type = ParseType(elem.GetAttribute("type"));
                var @ref = ParseType(elem.GetAttribute("extends"));

                if (@ref == -1)
                {
                    throw new ArgumentException(
                              "Message template " + elem.GetAttribute("type") +
                              " extends invalid template " + elem.GetAttribute("extends"));
                }

                IsoMessage tref = mfact.GetMessageTemplate(@ref);

                if (tref == null)
                {
                    throw new ArgumentException(
                              "Message template " + elem.GetAttribute("type") +
                              " extends nonexistent template " + elem.GetAttribute("extends"));
                }

                var m = (T) new IsoMessage();

                m.Type     = type;
                m.Encoding = mfact.Encoding;

                for (var i = 2; i <= 128; i++)
                {
                    if (tref.HasField(i))
                    {
                        m.SetField(
                            i,
                            (IsoValue)tref.GetField(i).Clone());
                    }
                }

                var fields = elem.GetElementsByTagName("field");

                for (var j = 0; j < fields.Count; j++)
                {
                    var f   = (XmlElement)fields.Item(j);
                    var num = int.Parse(f?.GetAttribute("num") ?? string.Empty);

                    if (f?.ParentNode != elem)
                    {
                        continue;
                    }

                    var v = GetTemplateField(
                        f,
                        mfact,
                        true);

                    if (v != null)
                    {
                        v.Encoding = mfact.Encoding;
                    }

                    m.SetField(
                        num,
                        v);
                }

                mfact.AddMessageTemplate(m);
            }
        }
Пример #6
0
        private void TestParsed(IsoMessage m)
        {
            Assert.Equal(m.Type,
                         0x600);
            Assert.Equal(decimal.Parse("1234.00"),
                         m.GetObjectValue(4));
            Assert.True(m.HasField(7),
                        "No field 7!");
            Assert.Equal("000123",
                         m.GetField(11).ToString()); // Wrong Trace
            var buf = (sbyte[])m.GetObjectValue(41);

            sbyte[] exp =
            {
                unchecked ((sbyte)0xab),
                unchecked ((sbyte)0xcd),
                unchecked ((sbyte)0xef),
                0,
                0,
                0,
                0,
                0
            };
            Assert.Equal(8,
                         buf.Length); //Field 41 wrong length

            Assert.Equal(exp,
                         buf); //"Field 41 wrong value"

            buf = (sbyte[])m.GetObjectValue(42);
            exp = new sbyte[]
            {
                0x0a,
                unchecked ((sbyte)0xbc),
                unchecked ((sbyte)0xde),
                0
            };
            Assert.Equal(4,
                         buf.Length); // "field 42 wrong length"
            Assert.Equal(exp,
                         buf);        // "Field 42 wrong value"
            Assert.True(((string)m.GetObjectValue(43)).StartsWith("Field of length 40",
                                                                  StringComparison.Ordinal));

            buf = (sbyte[])m.GetObjectValue(62);
            exp = new sbyte[]
            {
                1,
                0x23,
                0x45,
                0x67,
                unchecked ((sbyte)0x89),
                unchecked ((sbyte)0xab),
                unchecked ((sbyte)0xcd),
                unchecked ((sbyte)0xef),
                0x62,
                1,
                0x23,
                0x45,
                0x67,
                unchecked ((sbyte)0x89),
                unchecked ((sbyte)0xab),
                unchecked ((sbyte)0xcd)
            };
            Assert.Equal(exp,
                         buf);
            buf    = (sbyte[])m.GetObjectValue(64);
            exp[8] = 0x64;
            Assert.Equal(exp,
                         buf);
            buf = (sbyte[])m.GetObjectValue(63);
            exp = new sbyte[]
            {
                0,
                0x12,
                0x34,
                0x56,
                0x78,
                0x63
            };
            Assert.Equal(exp,
                         buf);
            buf    = (sbyte[])m.GetObjectValue(65);
            exp[5] = 0x65;
            Assert.Equal(exp,
                         buf);
        }
Пример #7
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);
        }
Пример #8
0
        /// <summary>
        /// SignOn/Off as POS
        /// ChungNN 03/2009
        /// </summary>
        /// <param name="request">IsoMessage</param>
        /// <returns>IsoMessage</returns>
        public IsoMessage PosSignOnOff(IsoMessage request)
        {
            topupObj = new TopupInterface();
            mfact    = new MessageFactory();
            mfact    = ConfigParser.CreateFromFile(AppConfiguration.App_Path + AppConfiguration.POSConfig_ISOFile);
            IsoMessage response = mfact.CreateResponse(request);

            string strRequestCode      = request.GetField(70).Value.ToString();
            int    nPos_ID             = int.Parse(request.GetField(52).Value.ToString());
            string strPosAdminPassword = request.GetField(48).Value.ToString();
            string strRequest          = request.GetField(11).Value.ToString();

            try
            {
                //Signon
                if (strRequestCode == "001")
                {
                    if (topupObj.PosLogon(nPos_ID, ref strRequest, strPosAdminPassword))
                    {
                        mfact.Setfield(11, strRequest, ref response);
                        mfact.Setfield(39, "00", ref response);
                        transObj.WriteLog("->POS signon(" + request.GetField(52).Value.ToString() + "|" + request.GetField(48).Value.ToString() + ") successfull");
                    }
                    else
                    {
                        mfact.Setfield(11, request.GetField(11), ref response);
                        mfact.Setfield(39, "01", ref response);
                        transObj.WriteLog("->POS signon(" + request.GetField(52).Value.ToString() + "|" + request.GetField(48).Value.ToString() + ") fail");
                    }
                }
                //Signoff
                else if (strRequestCode == "002")
                {
                    if (topupObj.PosLogout(nPos_ID, strRequest))
                    {
                        mfact.Setfield(11, request.GetField(11), ref response);
                        mfact.Setfield(39, "00", ref response);
                        transObj.WriteLog("->POS Signof successfull");
                    }
                    else
                    {
                        mfact.Setfield(11, request.GetField(11), ref response);
                        mfact.Setfield(39, "01", ref response);
                        transObj.WriteLog("->POS Signof fail");
                    }
                }

                //Key Exchange
                else if (strRequestCode == "161")
                {
                    transObj.WriteLog("->POS Key Exchange");
                }
            }
            catch (Exception ex)
            {
                transObj.WriteLog("->Exception=" + ex.Message);
                mfact.Setfield(39, "01", ref response);
            }

            mfact.Setfield(7, DateTime.Now.ToString("ddMMhhmmss"), ref response);

            return(response);
        }
Пример #9
0
        /// <summary>
        /// POS download softpin
        /// ChungNN 03/2009
        /// </summary>
        /// <param name="request">IsoMessage</param>
        /// <returns>IsoMessage</returns>
        public IsoMessage Download(IsoMessage request)
        {
            topupObj = new TopupInterface();

            //create response message
            mfact = new MessageFactory();
            mfact = ConfigParser.CreateFromFile(AppConfiguration.App_Path + AppConfiguration.POSConfig_ISOFile);
            IsoMessage response            = mfact.CreateResponse(request);
            bool       blnDownloadTemplate = !request.HasField(48);
            string     strRequest          = request.GetField(11).Value.ToString();
            int        nMerchant_ID        = int.Parse(request.GetField(2).Value.ToString());
            int        nPos_ID             = int.Parse(request.GetField(52).Value.ToString());

            //if exist session
            if (!Common.ServiceSessionManager.GetSessionInstance().IsExistedSession(nPos_ID.ToString(), strRequest))
            {
                mfact.Setfield(39, "01", ref response);
                transObj.WriteLog("->download fail, session not exist");
            }
            else
            {
                //Download template
                if (blnDownloadTemplate)
                {
                    try
                    {
                        //get request values
                        topupObj = new TopupInterface();
                        BatchBuyObject buyObj = new BatchBuyObject();

                        //String[] arrRequestValues = request.GetField(48).Value.ToString().Split(AppConfiguration.POS_Seperator_Char);
                        //string strCategoryName = arrRequestValues[0];
                        //string strServiceProviderName = arrRequestValues[1];
                        //int nProductValue = int.Parse(arrRequestValues[2]);
                        //int nStockQuantity = int.Parse(arrRequestValues[3]);
                        //int nDownloadQuantity = int.Parse(arrRequestValues[4]);

                        object[] SoftpinStock = new object[1];

                        StockObject stockObj = new StockObject();
                        stockObj.ProductValue        = 10000;
                        stockObj.CategoryName        = "Thẻ ĐTDĐ";
                        stockObj.ServiceProviderName = "Vinaphone";
                        stockObj.StockQuantity       = 0;
                        SoftpinStock[0] = stockObj;

                        buyObj = topupObj.PosDownloadSoftpinTemplate(nPos_ID, nMerchant_ID, strRequest, SoftpinStock);

                        if (buyObj.ErrorCode == 0)
                        {
                            mfact.Setfield(39, "00", ref response);
                            transObj.WriteLog("->download template successfull");
                        }
                        else
                        {
                            mfact.Setfield(39, "01", ref response);
                            transObj.WriteLog("->download template fail");
                        }
                    }
                    catch (Exception ex)
                    {
                        transObj.WriteLog("->download template execption =" + ex.ToString());
                        throw (ex);
                    }
                }
                //Download single
                else
                {
                    try
                    {
                        //get request values
                        topupObj = new TopupInterface();
                        BatchBuyObject buyObj = new BatchBuyObject();

                        String[] arrRequestValues       = request.GetField(48).Value.ToString().Split(AppConfiguration.POS_Seperator_Char);
                        string   strCategoryName        = arrRequestValues[0];
                        string   strServiceProviderName = arrRequestValues[1];
                        int      nProductValue          = int.Parse(arrRequestValues[2]);
                        int      nStockQuantity         = int.Parse(arrRequestValues[3]);
                        int      nDownloadQuantity      = int.Parse(arrRequestValues[4]);

                        buyObj = topupObj.PosDownloadSingleSoftpin(nPos_ID, nMerchant_ID, strRequest, strCategoryName, strServiceProviderName, nProductValue, nStockQuantity, nDownloadQuantity);

                        if (buyObj.ErrorCode == 0)
                        {
                            mfact.Setfield(39, "00", ref response);
                            transObj.WriteLog("->download single successfull");
                        }
                        else
                        {
                            mfact.Setfield(39, "01", ref response);
                            transObj.WriteLog("->download single fail");
                        }

                        //create response message
                        mfact    = new MessageFactory();
                        mfact    = ConfigParser.CreateFromFile(AppConfiguration.App_Path + AppConfiguration.POSConfig_ISOFile);
                        response = mfact.CreateResponse(request);
                    }
                    catch (Exception ex)
                    {
                        transObj.WriteLog("->download single execption =" + ex.ToString());
                        throw (ex);
                    }
                }
            }

            return(response);
        }
Пример #10
0
        public void PrintMessage(IsoMessage m)
        {
            Transaction transObj = new Transaction();

            if (m == null)
            {
                transObj.WriteLog("Mensaje nulo!");
                return;
            }
            transObj.WriteLog("TYPE: " + m.Type.ToString("x"));
            for (int i = 2; i < 128; i++)
            {
                if (m.HasField(i))
                {
                    transObj.WriteLog("F " + i.ToString() + ": " + m.GetObjectValue(i) + " -> '" + m.GetField(i).ToString() + "'");
                }
            }
        }