public override void ParseResponse(byte[] response) { if (response == null) { throw new ApiException("Response data is null"); } if (response.Length < INGENICO_GLOBALS.RAW_RESPONSE_LENGTH) { byte[] newResponse = new byte[INGENICO_GLOBALS.RAW_RESPONSE_LENGTH]; response.CopyTo(newResponse, 0); response = newResponse; } base.ParseResponse(response); var tlv = new TypeLengthValue(response.SubArray(12, 55)); string terminalStatusData = (string)tlv.GetValue((byte)StateResponseCode.Status, typeof(string), TLVFormat.State); _terminalStatus = (TerminalStatus)Convert.ToByte(terminalStatusData.Substring(0, 1)); _salesMode = (SalesMode)Convert.ToByte(terminalStatusData.Substring(1, 1)); _terminalCapabilities = terminalStatusData.Substring(2, 6); _additionalTerminalCapabilities = terminalStatusData.Substring(8, 10); _appVersionNumber = (string)tlv.GetValue((byte)StateResponseCode.AppVersionNumber, typeof(string), TLVFormat.State); _handsetNumber = (string)tlv.GetValue((byte)StateResponseCode.HandsetNumber, typeof(string), TLVFormat.State); _terminalId = (string)tlv.GetValue((byte)StateResponseCode.TerminalId, typeof(string), TLVFormat.State); }
public void Decode_SingleMessage() { var bytes = new byte[] { 0x01, 0x01, 0x03 }; var messages = TypeLengthValue.Decode(bytes); Assert.AreEqual(messages.Count, 1, "Should be 1 message"); Assert.AreEqual(messages[0].Type, 0x01, "Msg 1 Type"); Assert.AreEqual(messages[0].Value[0], 0x03, "Msg 1 Byte 1"); }
public void Encode_SingleMessage() { TypeLengthValue.TypeValue message1 = new TypeLengthValue.TypeValue { Type = 0x01, Value = new byte[] { 0x01, 0x05, 0x03 } }; var bytes = TypeLengthValue.Encode(message1); Assert.AreEqual(5, bytes.Length); Assert.AreEqual(3, bytes[1]); }
private void ParseData() { var tlv = new TypeLengthValue(_buffer); Type stringType = typeof(string); _authCode = (string)tlv.GetValue((byte)RepFieldCode.AuthCode, stringType); _cashbackAmount = (string)tlv.GetValue((byte)RepFieldCode.CashbackAmount, stringType); _gratuityAmount = (string)tlv.GetValue((byte)RepFieldCode.GratuityAmount, stringType); _finalAmount = (string)tlv.GetValue((byte)RepFieldCode.FinalTransactionAmount, stringType); _availableAmount = (string)tlv.GetValue((byte)RepFieldCode.AvailableAmount, stringType); _dccCode = (string)tlv.GetValue((byte)RepFieldCode.DccCurrency, stringType); _dccAmount = (string)tlv.GetValue((byte)RepFieldCode.DccConvertedAmount, stringType); _txnSubType = EnumConverter.FromDescription <TransactionSubTypes>(tlv.GetValue((byte)RepFieldCode.TransactionSubType, stringType)); _dccStatus = (DynamicCurrencyStatus?)tlv.GetValue((byte)RepFieldCode.DccOperationStatus, typeof(DynamicCurrencyStatus?)); _splitSaleAmount = (string)tlv.GetValue((byte)RepFieldCode.SplitSalePaidAmount, stringType); _paymentMethod = (PaymentMethod?)tlv.GetValue((byte)RepFieldCode.PaymentMethod, typeof(PaymentMethod?)); }
public void Encode_ManyMessages() { TypeLengthValue.TypeValue message1 = new TypeLengthValue.TypeValue { Type = 0x01, Value = new byte[] { 0x01, 0x05, 0x03 } }; TypeLengthValue.TypeValue message2 = new TypeLengthValue.TypeValue { Type = 0x0A, Value = new byte[] { 0x01, 0x05, 0x03 } }; TypeLengthValue.TypeValue message3 = new TypeLengthValue.TypeValue { Type = 0x0D, Value = new byte[] { 0x01, 0x05, 0x03 } }; var bytes = TypeLengthValue.Encode(message1, message2, message3); Assert.AreEqual(15, bytes.Length); //All messages of length 3 Assert.AreEqual(3, bytes[1]); Assert.AreEqual(3, bytes[6]); Assert.AreEqual(3, bytes[11]); }
public void Decode_ManyMessage() { var bytes = new byte[] { 0x01, 0x01, 0x03, 0x02, 0x03, 0x07, 0x07, 0x07, 0x03, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x04, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 }; var messages = TypeLengthValue.Decode(bytes); Assert.AreEqual(messages.Count, 4, "Should be 4 messages"); Assert.AreEqual(messages[1].Type, 0x02, "Msg 2 Type"); Assert.AreEqual(messages[2].Type, 0x03, "Msg 3 Type"); Assert.AreEqual(messages[3].Type, 0x04, "Msg 4 Type"); Assert.AreEqual(messages[3].Value[0], 0x01, "Msg 4 Byte 1"); Assert.AreEqual(messages[3].Value[1], 0x02, "Msg 4 Byte 2"); Assert.AreEqual(messages[3].Value[2], 0x03, "Msg 4 Byte 3"); Assert.AreEqual(messages[3].Value[3], 0x04, "Msg 4 Byte 4"); Assert.AreEqual(messages[3].Value[4], 0x05, "Msg 4 Byte 5"); Assert.AreEqual(messages[3].Value[9], 0x05, "Msg 4 Byte 10"); }
private void ParseData() { string strBuffer = Encoding.UTF8.GetString(_buffer); // XML if (strBuffer.Contains(INGENICO_GLOBALS.XML_TAG)) { if (!strBuffer.EndsWith(">")) { char[] xmlArr = strBuffer.ToCharArray(); for (int i = strBuffer.Length - 1; i <= strBuffer.Length; i--) { if (xmlArr[i] == '>') { XMLData = strBuffer.Substring(0, (i + 1)); break; } } } else { XMLData = strBuffer; } // Convert String to XML XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(XMLData); XmlElement root = xmlDoc.DocumentElement; string rootTag = root.Name; if (rootTag == INGENICO_GLOBALS.ADDITIONAL_MSG) { RequestType = PATRequestType.AdditionalMessage; } else if (rootTag == INGENICO_GLOBALS.TRANSFER_DATA) { RequestType = PATRequestType.TransferData; } else if (rootTag == INGENICO_GLOBALS.TRANSACTION_XML) { XmlNodeList nList = xmlDoc.GetElementsByTagName("RECEIPT"); XmlNode node = nList.Item(0); if (node.NodeType == XmlNodeType.Element) { XmlElement element = (XmlElement)node; string sType = element.GetAttribute("STYPE"); if (sType.Equals("SPLITSALE REPORT")) { RequestType = PATRequestType.SplitSaleReport; } else if (sType.Equals("CUSTOMER")) { RequestType = PATRequestType.Ticket; } else { RequestType = PATRequestType.EndOfDayReport; } } else { throw new ApiException("First child node is not an element"); } } else { throw new ApiException("The root tag of the xml cannot recognize"); } } else { // Workaround for split sale but not final logic if (strBuffer.ToLower().Contains("split_sale")) { RequestType = PATRequestType.SplitSaleReport; XMLData = strBuffer; } // Message Frame 2 else if (_buffer.Length >= INGENICO_GLOBALS.MSG_FRAME_TWO_LEN) { RequestType = PATRequestType.TransactionOutcome; TransactionOutcome = new TransactionOutcomeRequest(_buffer); } else { // Message Frame 1 RequestType = (PATRequestType)strBuffer.Substring(11, 1).ToInt32(); string privData = strBuffer.Substring(16); if (privData.Length < 55) { switch (RequestType) { case PATRequestType.TableLock: case PATRequestType.TableUnlock: TableId = privData; break; default: break; } } else { var tlvData = new TypeLengthValue(_buffer); WaiterId = tlvData.GetValue((byte)PATPrivateDataCode.WaiterId, typeof(string))?.ToString(); TableId = tlvData.GetValue((byte)PATPrivateDataCode.TableId, typeof(string), TLVFormat.PayAtTable)?.ToString(); TerminalId = tlvData.GetValue((byte)PATPrivateDataCode.TerminalId, typeof(string))?.ToString(); TerminalCurrency = tlvData.GetValue((byte)PATPrivateDataCode.TerminalCurrency, typeof(string))?.ToString(); } } } }