public void TestEmptyLlllvar() { IsoMessage t = txtfact.NewMessage(0x100); IsoMessage b = binfact.NewMessage(0x100); t.SetValue(4, "", IsoType.LLLLVAR, 0); b.SetValue(4, "", IsoType.LLLLVAR, 0); CheckString(t.WriteData(), b.WriteData(), 4); }
public void TestEmptyLlllbin() { IsoMessage t = txtfact.NewMessage(0x100); IsoMessage b = binfact.NewMessage(0x100); t.SetValue(7, new sbyte[0], IsoType.LLLLBIN, 0); b.SetValue(7, new sbyte[0], IsoType.LLLLBIN, 0); CheckBin(t.WriteData(), b.WriteData(), 7); }
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)); }
private void CheckBin(sbyte[] txt, sbyte[] bin, int field) { IsoMessage t = txtfact.ParseMessage(txt, 0); IsoMessage b = binfact.ParseMessage(bin, 0); Assert.True(t.HasField(field)); Assert.True(b.HasField(field)); Assert.Empty(((sbyte[])t.GetObjectValue(field))); Assert.Empty(((sbyte[])b.GetObjectValue(field))); }
public void TestTemplate() { IsoMessage m = mfact.NewMessage(0x100); Assert.Equal("010060000000000000000001X0002FF", m.DebugString()); m.Binary = true; Assert.Equal(new sbyte[] { 1, 0, (sbyte)0x60, 0, 0, 0, 0, 0, 0, 0, 0, 1, (sbyte)'X', 0, 1, unchecked ((sbyte)0xff) }, m.WriteData()); }
/// <summary> /// /// </summary> /// <remarks> /// I could not get the library to correct parse the sample Base64 encoded string. /// </remarks> public static void GetIsoMsg() { MessageFactory <IsoMessage> mf = new MessageFactory <IsoMessage>(); mf.Encoding = Encoding.UTF8; var iso8583_DecodedString = System.Convert.FromBase64String(ISO8583_BASE64); sbyte[] signed = (sbyte[])(Array)iso8583_DecodedString; IsoMessage isoMessage = mf.ParseMessage(signed, 0, true); }
private void CheckString(sbyte[] txt, sbyte[] bin, int field) { IsoMessage t = txtfact.ParseMessage(txt, 0); IsoMessage b = binfact.ParseMessage(bin, 0); Assert.True(t.HasField(field)); Assert.True(b.HasField(field)); string value = (string)(t.GetObjectValue(field)); string valueb = (string)(b.GetObjectValue(field)); Assert.True(value.IsEmpty()); Assert.True(valueb.IsEmpty()); }
private void TestFields(IsoMessage m, List <int> fields) { for (var i = 2; i < 128; i++) { if (fields.Contains(i)) { Assert.True(m.HasField(i)); } else { Assert.False(m.HasField(i)); } } }
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!")) ; }
public void TestNewMessage() { IsoMessage m = mfact.NewMessage(0x200); m.SetValue(2, "Variable length text", IsoType.LLLLVAR, 0); m.SetValue(3, "FFFF", IsoType.LLLLBIN, 0); Assert.Equal("020060000000000000000020Variable length text0004FFFF", m.DebugString()); m.Binary = (true); m.SetValue(2, "XX", IsoType.LLLLVAR, 0); m.SetValue(3, new sbyte[] { unchecked ((sbyte)0xff) }, IsoType.LLLLBIN, 0); Assert.Equal(new sbyte[] { 2, 0, (sbyte)0x60, 0, 0, 0, 0, 0, 0, 0, 0, 2, (sbyte)'X', (sbyte)'X', 0, 1, unchecked ((sbyte)0xff) }, m.WriteData()); }
public void TestMessageType() { var msg = new IsoMessage { Type = 0x1100, Encoding = CodePagesEncodingProvider.Instance.GetEncoding(1047), BinBitmap = true }; var enc = msg.WriteData(); Assert.Equal(12, enc.Length); Assert.Equal(unchecked ((sbyte)241), enc[0]); Assert.Equal(unchecked ((sbyte)241), enc[1]); Assert.Equal(unchecked ((sbyte)240), enc[2]); Assert.Equal(unchecked ((sbyte)240), enc[3]); var mf = new MessageFactory <IsoMessage>(); var pmap = new Dictionary <int, FieldParseInfo>(); mf.ForceStringEncoding = true; mf.UseBinaryBitmap = true; mf.Encoding = CodePagesEncodingProvider.Instance.GetEncoding(1047); mf.SetParseMap(0x1100, pmap); var m2 = mf.ParseMessage(enc, 0); Assert.Equal(msg.Type, m2.Type); //Now with text bitmap msg.BinBitmap = false; msg.ForceStringEncoding = true; var enc2 = msg.WriteData(); Assert.Equal(20, enc2.Length); mf.UseBinaryBitmap = false; m2 = mf.ParseMessage(enc2, 0); Assert.Equal(msg.Type, m2.Type); }
public void TestParsing() { IsoMessage m = mfact.ParseMessage("010060000000000000000001X0002FF".GetSignedbytes(), 0); Assert.NotNull(m); Assert.Equal("X", m.GetObjectValue(2)); Assert.Equal(new sbyte[] { unchecked ((sbyte)0xff) }, (sbyte[])m.GetObjectValue(3)); mfact.UseBinaryMessages = (true); m = mfact.ParseMessage(new byte[] { 1, 0, (byte)0x60, 0, 0, 0, 0, 0, 0, 0, 0, 2, (byte)'X', (byte)'X', 0, 1, (byte)0xff }.ToSignedBytes(), 0); Assert.NotNull(m); Assert.Equal("XX", m.GetObjectValue(2)); Assert.Equal(new sbyte[] { unchecked ((sbyte)0xff) }, (sbyte[])m.GetObjectValue(3)); }
public void TestBinaryBitmap() { MessageFactory <IsoMessage> mf = new MessageFactory <IsoMessage>(); ConfigParser.ConfigureFromClasspathConfig(mf, @"/Resources/issue4.xml"); IsoMessage bm = mf.GetMessageTemplate(0x800); bm.BinBitmap = true; var bb = bm.WriteToBuffer(2); Assert.Equal(62, bb.Length); //"Wrong message length for new BIN" var memStream = new MemoryStream(); var binWriter = new BinaryWriter(memStream); foreach (var @sbyte in bb) { binWriter.Write(@sbyte); } var binReader = new BinaryReader(memStream); memStream.Position = 0; var buf = binReader.ReadBytes(2); Array.Reverse(buf); // due to the Big Endianness of Java Assert.Equal(60, BitConverter.ToInt16(buf, 0)); MessageFactory <IsoMessage> mfp = new MessageFactory <IsoMessage> { UseBinaryBitmap = true }; ConfigParser.ConfigureFromClasspathConfig(mfp, @"/Resources/issue4.xml"); var buf2 = binReader.ReadBytes((int)(memStream.Length - memStream.Position)); bm = mfp.ParseMessage(buf2.ToSignedBytes(), 0); Assert.True(bm.BinBitmap, "Parsed message should have binary bitmap flag set"); Assert.False(bm.Binary); var bbp = bm.WriteToBuffer(2); Assert.Equal(bb, bbp); // "Parsed-reencoded BIN differs from original" }
public void TestParseLengthWithRadix10() { // Given var input = "0100" + // MTI "7000000000000000" + // bitmap "10" + "ABCDEFGHIJ" + // F2 length (10 = 10) + value "26" + "01234567890123456789012345" + // F3 length (26 = 26) + value "ZZZZZZZZ"; // F4 // When IsoMessage m = mfact.ParseMessage(input.GetSignedBytes(), 0); // Then Assert.NotNull(m); Assert.Equal("ABCDEFGHIJ", m.GetObjectValue(2)); Assert.Equal("01234567890123456789012345", HexCodec.HexEncode((sbyte[])m.GetObjectValue(3), 0, 13)); Assert.Equal("ZZZZZZZZ", m.GetObjectValue(4)); }
public void TestL4bin() { sbyte[] fieldData = new sbyte[1000]; mfact.UseBinaryMessages = true; IsoMessage m = mfact.NewMessage(0x100); m.SetValue(3, fieldData, IsoType.LLLLBIN, 0); fieldData = m.WriteData(); //2 for message header //8 bitmap //3 for field 2 (from template) //1002 for field 3 Assert.Equal(1015, fieldData.Length); m = mfact.ParseMessage(fieldData, 0); Assert.True(m.HasField(3)); fieldData = m.GetObjectValue(3) as sbyte[]; Assert.Equal(1000, fieldData.Length); }
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); }
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() + "'"); } } }
public void TestMultilevelExtendParseGuides() { string configXml = @"/Resources/issue34.xml"; MessageFactory <IsoMessage> mfact = Config(configXml); //Parse a 200 string m200 = "0200422000000880800001X1231235959123456101010202020TERMINAL484"; string m210 = "0210422000000A80800001X123123595912345610101020202099TERMINAL484"; string m400 = "0400422000000880800401X1231235959123456101010202020TERMINAL484001X"; string m410 = "0410422000000a80800801X123123595912345610101020202099TERMINAL484001X"; IsoMessage m = mfact.ParseMessage(m200.GetSbytes(), 0); Assert.NotNull(m); Assert.Equal("X", m.GetObjectValue(2)); Assert.Equal("123456", m.GetObjectValue(11)); Assert.Equal("TERMINAL", m.GetObjectValue(41)); Assert.Equal("484", m.GetObjectValue(49)); m = mfact.ParseMessage(m210.GetSbytes(), 0); Assert.NotNull(m); Assert.Equal("X", m.GetObjectValue(2)); Assert.Equal("123456", m.GetObjectValue(11)); Assert.Equal("TERMINAL", m.GetObjectValue(41)); Assert.Equal("484", m.GetObjectValue(49)); Assert.Equal("99", m.GetObjectValue(39)); m = mfact.ParseMessage(m400.GetSbytes(), 0); Assert.NotNull(m); Assert.Equal("X", m.GetObjectValue(2)); Assert.Equal("123456", m.GetObjectValue(11)); Assert.Equal("TERMINAL", m.GetObjectValue(41)); Assert.Equal("484", m.GetObjectValue(49)); Assert.Equal("X", m.GetObjectValue(62)); m = mfact.ParseMessage(m410.GetSbytes(), 0); Assert.NotNull(m); Assert.Equal("X", m.GetObjectValue(2)); Assert.Equal("123456", m.GetObjectValue(11)); Assert.Equal("TERMINAL", m.GetObjectValue(41)); Assert.Equal("484", m.GetObjectValue(49)); Assert.Equal("99", m.GetObjectValue(39)); Assert.Equal("X", m.GetObjectValue(61)); }
public void TestSimpleCompositeTemplate() { string configXml = @"/Resources/composites.xml"; MessageFactory <IsoMessage> mfact = Config(configXml); IsoMessage m = mfact.NewMessage(0x100); //Simple composite Assert.NotNull(m); Assert.False(m.HasField(1)); Assert.False(m.HasField(2)); Assert.False(m.HasField(3)); Assert.False(m.HasField(4)); CompositeField f = (CompositeField)m.GetObjectValue(10); Assert.NotNull(f); Assert.Equal(f.GetObjectValue(0), "abcde"); Assert.Equal(f.GetObjectValue(1), "llvar"); Assert.Equal(f.GetObjectValue(2), "12345"); Assert.Equal(f.GetObjectValue(3), "X"); Assert.False(m.HasField(4)); }
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); }
public void TestNestedCompositeParser() { string configXml = @"/Resources/composites.xml"; MessageFactory <IsoMessage> mfact = Config(configXml); IsoMessage m = mfact.ParseMessage("01010040000000000000019ALPHA11F1F205F03F4X".GetSbytes(), 0); Assert.NotNull(m); Assert.True(m.HasField(10)); CompositeField f = (CompositeField)m.GetObjectValue(10); Assert.NotNull(f.GetField(0)); Assert.NotNull(f.GetField(1)); Assert.NotNull(f.GetField(2)); Assert.Null(f.GetField(3)); Assert.Equal("ALPHA", f.GetObjectValue(0)); Assert.Equal("X", f.GetObjectValue(2)); f = (CompositeField)f.GetObjectValue(1); Assert.Equal("F1", f.GetObjectValue(0)); Assert.Equal("F2", f.GetObjectValue(1)); f = (CompositeField)f.GetObjectValue(2); Assert.Equal("F03", f.GetObjectValue(0)); Assert.Equal("F4", f.GetObjectValue(1)); }
private void NestedCompositeTemplate(int type, int fnum) { string configXml = @"/Resources/composites.xml"; MessageFactory <IsoMessage> mfact = Config(configXml); IsoMessage m = mfact.NewMessage(type); Assert.NotNull(m); Assert.False(m.HasField(1)); Assert.False(m.HasField(2)); Assert.False(m.HasField(3)); Assert.False(m.HasField(4)); CompositeField f = (CompositeField)m.GetObjectValue(fnum); Assert.Equal(f.GetObjectValue(0), "fghij"); Assert.Equal(f.GetObjectValue(2), "67890"); Assert.Equal(f.GetObjectValue(3), "Y"); f = (CompositeField)f.GetObjectValue(1); Assert.Equal(f.GetObjectValue(0), "KL"); Assert.Equal(f.GetObjectValue(1), "mn"); f = (CompositeField)f.GetObjectValue(2); Assert.Equal(f.GetObjectValue(0), "123"); Assert.Equal(f.GetObjectValue(1), "45"); }
public void TestMessages() { //Create a message with both factories var ascii = _mfactAscii.NewMessage(0x600); var bin = _mfactBin.NewMessage(0x600); Assert.False(ascii.Binary || ascii.BinBitmap); Assert.True(bin.Binary); //HEXencode the binary message, headers should be similar to the ASCII version sbyte[] v = bin.WriteData(); var hexBin = HexCodec.HexEncode(v, 0, v.Length); var hexAscii = ascii.WriteData().SbyteString(Encoding.Default).ToUpper(CultureInfo.CurrentCulture); Assert.Equal("0600", hexBin.Substring(0, 4)); //Should be the same up to the field 42 (first 80 chars) Assert.Equal(hexAscii.Substring(0, 88), hexBin.Substring(0, 88)); Assert.Equal(ascii.GetObjectValue(43), v.SbyteString(44, 40, Encoding.Default).Trim()); //Parse both messages sbyte[] asciiBuf = ascii.WriteData(); IsoMessage ascii2 = _mfactAscii.ParseMessage(asciiBuf, 0); TestParsed(ascii2); Assert.Equal(ascii.GetObjectValue(7).ToString(), ascii2.GetObjectValue(7).ToString()); IsoMessage bin2 = _mfactBin.ParseMessage(bin.WriteData(), 0); //Compare values, should be the same TestParsed(bin2); Assert.Equal(bin.GetObjectValue(7).ToString(), bin2.GetObjectValue(7).ToString()); //Test the debug string ascii.SetValue(60, "XXX", IsoType.LLVAR, 0); bin.SetValue(60, "XXX", IsoType.LLVAR, 0); Assert.Equal(ascii.DebugString(), bin.DebugString()); // "Debug strings differ" Assert.True(ascii.DebugString().Contains("03XXX"), "LLVAR fields wrong"); }
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); } }
/// <summary> /// Cria template de mensagem baseado no MTI da mensagem /// </summary> /// <param name="tipo">MTI</param> /// <returns></returns> //public IMensagem CriarTemplate(MTI mti) //{ // IsoMessage isoMessage; // IMensagem retorno; // try // { // isoMessage = _factory.NewTemplateMessage(Convert.ToInt32(mti)); // retorno = new BaseMensagemFormatoISO(isoMessage); // } // catch (Exception ex) // { // throw ex; // } // return retorno; //} //public IMensagem CriarMensagemParse(MTI tipo) //{ // IsoMessage isoMessage; // IMensagem retorno; // try // { // //isoMessage = _factory.NewParseMessage(Convert.ToInt32(tipo)); // //retorno = new BaseMensagemFormatoISO(isoMessage); // } // catch (Exception ex) // { // throw ex; // } // return retorno; //} /// <summary> /// Cria sua respectiva mensagem ISO de resposta a partir de uma mensagem de entrada obdecendo o layout definido em: formatoMensagens.xml /// Será gerado a mensagem de resposta baseado no MTI da mensagem de entrada, seguindo o exemplo: 0100 -> 0110, 0420 -> 0430, .... /// </summary> /// <param name="mensagem"></param> /// <returns>Mensagem ISO de resposta baseada na mensagem ISO de entrada</returns> public IsoMessage GerarResposta(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, IsoMessage mensagem) { //IMensagem retorno; IsoMessage retorno; try { //MensagemAdapter respostaAdaptada = mensagem as MensagemAdapter; //if (respostaAdaptada == null) // throw new InvalidCastException("Mensagem no formato diferente do esperado."); //retorno = new BaseMensagemFormatoISO(_factory.CreateResponse(respostaAdaptada.Mensagem)); retorno = _factory.CreateResponse(interfaceDados, mensagem); } catch (Exception ex) { throw ex; } return(retorno); }
public Byte[] Format(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, IsoMessage mensagem) { Byte[] retorno; try { retorno = _factory.FormatMessage(interfaceDados, mensagem.ToString()); } catch (Exception ex) { throw ex; } return(retorno); }
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()); } }
/// <summary> /// Parses a byte buffer containing an ISO8583 message. The buffer must /// not include the length header. If it includes the ISO message header, /// then its length must be specified so the message type can be found. /// </summary> /// <param name="mti_bitmap">The byte buffer containing the message, starting /// at the ISO header or the message type.</param> /// <param name="isoHeaderLength">Specifies the position at which the message /// type is located, which is algo the length of the ISO header.</param> /// <param name="encoder">The encoder to use for reading string values.</param> /// <returns>The parsed message.</returns> private IsoMessage ParseMessage(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, Byte[] mti_bitmap, Byte[] messageData, Int32 isoHeaderLength, Encoding encoder, ParseMode parseMode) { Int32 pos = 0; IsoMessage m = (isoHeaderLength > 0) ? new IsoMessage(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, encoder.GetString(mti_bitmap, 0, isoHeaderLength)) : new IsoMessage(EncodingType.NotDefined, null); m.isValid = true; m.MapFields = interfaceDados.MensagemInterfaceHeader.MapaCamposVAR; m.IdInterface = interfaceDados.MensagemInterfaceHeader.IdMensagemInterfaceHeader; Int32 type = ((mti_bitmap[isoHeaderLength] - 48) << 12) | ((mti_bitmap[isoHeaderLength + 1] - 48) << 8) | ((mti_bitmap[isoHeaderLength + 2] - 48) << 4) | (mti_bitmap[isoHeaderLength + 3] - 48); m.Type = type; //Parse the bitmap BitArray bs = ((HexByteValue(mti_bitmap[isoHeaderLength + ConfigParser.ISOLengthMTI]) & 8) > 0) ? new BitArray(128) : new BitArray(64); Int32 tamanhoMensagemAtePrimeiroMapaDeBits = ConfigParser.ISOLengthMTI + ConfigParser.ISOLengthBitMap; for (Int32 i = isoHeaderLength + ConfigParser.ISOLengthMTI; i < isoHeaderLength + tamanhoMensagemAtePrimeiroMapaDeBits; i++) { Int32 hex = HexByteValue(mti_bitmap[i]); bs.Set(pos++, (hex & 8) > 0); bs.Set(pos++, (hex & 4) > 0); bs.Set(pos++, (hex & 2) > 0); bs.Set(pos++, (hex & 1) > 0); } //Extended bitmap if (bs.Get(0)) { Int32 tamanhoMensagemAteSegundoMapaDeBits = tamanhoMensagemAtePrimeiroMapaDeBits + ConfigParser.ISOLengthBitMap; for (Int32 i = isoHeaderLength + tamanhoMensagemAtePrimeiroMapaDeBits; i < isoHeaderLength + tamanhoMensagemAteSegundoMapaDeBits; i++) { Int32 hex = HexByteValue(mti_bitmap[i]); bs.Set(pos++, (hex & 8) > 0); bs.Set(pos++, (hex & 4) > 0); bs.Set(pos++, (hex & 2) > 0); bs.Set(pos++, (hex & 1) > 0); } } pos = 0; m.BitArray = bs; foreach (KeyValuePair <Int32, FieldParseInfo> i in interfaceDados.MensagemInterfaceHeader.MapaCamposVAR) { try { if (!bs.Get(i.Key - 1)) //Sу efetua checagem se o field do mapa existe na mensagem, caso nгo tenha, pula pro proximo field ou gera exception e pula pro proximo. Class BitArray nгo tem opзгo para checar se um bit estб ligado/desligado { continue; } } catch (ArgumentOutOfRangeException) { continue; } FieldParseInfo fpi = i.Value; IsoValue val = fpi.Parse(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, messageData, pos, encoder, false); if (val.OriginalValue != null) { if (parseMode == ParseMode.Complete) { if (fpi.SubDataElements_Fields != null) { if (fpi.SubDataElements_Fields.Count > 0) { val = parseDataElement_Field(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, fpi.Base, fpi.SubDataElements_Fields, val, encoder, ref pos); } if (val == null) { m.isValid = false; return(m); } } } if (interfaceDados.MensagemInterfaceHeader.TipoCodificacao == EncodingType.EBCDIC && fpi.Base == ConfigParser.Base.Hexadecimal) { pos += val.Length / 2; } else { pos += val.Length; } if (val.Type == IsoType.LLVAR) { pos += 2; } else if (val.Type == IsoType.LLLVAR) { pos += 3; } m.SetField(i.Key, val); if (fpi.Required && !bs.Get(i.Key - 1)) { m.isValid = false; } } } return(m); }
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); }
private void TestFieldType(IsoType type, FieldParseInfo fieldParser, int offset1, int offset2) { var bigintCodec = new BigIntBcdCodec(); var longCodec = new LongBcdCodec(); var mfact = new MessageFactory <IsoMessage>(); var tmpl = new IsoMessage { Binary = true, Type = 0x200 }; tmpl.SetValue(2, 1234567890L, longCodec, type, 0); tmpl.SetValue(3, b29, bigintCodec, type, 0); mfact.AddMessageTemplate(tmpl); mfact.SetCustomField(2, longCodec); mfact.SetCustomField(3, bigintCodec); var parser = new Dictionary <int, FieldParseInfo> { { 2, fieldParser }, { 3, fieldParser } }; mfact.SetParseMap(0x200, parser); mfact.UseBinaryMessages = true; //Test encoding tmpl = mfact.NewMessage(0x200); var buf = tmpl.WriteData(); var message = HexCodec.HexEncode(buf, 0, buf.Length); Console.WriteLine("MESSAGE: " + message); for (var i = 0; i < 5; i++) { var b = longData2[i]; Assert.Equal(b, buf[i + offset1]); } for (var i = 0; i < 15; i++) { Assert.Equal(bigintData1[i], buf[i + offset2]); } //Test parsing tmpl = mfact.ParseMessage(buf, 0); Assert.Equal(1234567890L, tmpl.GetObjectValue(2)); Assert.Equal(b29, tmpl.GetObjectValue(3)); }