private Iso8583Message camposTramaIso(String trama, Iso8583MessageFormatter formato) { Iso8583Message _ISOMsg; String sInfoTrack2; String sMontoServicio; String sMontoPropina; String sMontoCashOver; String sInfoTrack1; String sMesesGracia; String sMontoBaseGravaIVA = ""; String sMontoBaseNoGravaIVA = ""; String sTipoTarjeta; String sTipoPlazoCredito; String sModoEntrada; String sCVV2; String sMonto; String sMontoIva; String sNumeroAutorizacion; String sNumeroLote; String sMTI = ""; String sTipoTransaccion = ""; CamposAdicionales camposAdicionales = new CamposAdicionales(); _sTipoTransaccion = trama.Substring(0, 2); sTipoTarjeta = trama.Substring(2, 2); sTipoPlazoCredito = trama.Substring(4, 4); sMesesGracia = trama.Substring(8, 2); sInfoTrack2 = trama.Substring(10, 37).Trim(); sModoEntrada = trama.Substring(47, 3); sCVV2 = trama.Substring(50, 4).Trim(); sMonto = trama.Substring(54, 12); sMontoBaseGravaIVA = trama.Substring(66, 12).Trim(); sMontoBaseNoGravaIVA = trama.Substring(78, 12).Trim(); sMontoIva = trama.Substring(90, 12).Trim(); sMontoServicio = trama.Substring(102, 12).Trim(); sMontoPropina = trama.Substring(114, 12).Trim(); sMontoCashOver = trama.Substring(126, 12).Trim(); _sSecuencial = trama.Substring(138, 6); _sHora = trama.Substring(144, 6); _sFecha = trama.Substring(150, 8); sNumeroAutorizacion = trama.Substring(158, 6).Trim(); _sTerminalId = trama.Substring(164, 8); _sMerchandId = trama.Substring(172, 15); sNumeroLote = trama.Substring(187, 6); sInfoTrack1 = trama.Substring(193, 99); _sMontoFijo = trama.Substring(292, 12).Trim(); _sNombreTarjeta = ""; sInfoTrack1 = sInfoTrack1.Trim(); if (sMontoIva != null) { if (sMontoIva.Length > 0) { camposAdicionales.setIVA(sMontoIva); } } if (sMontoServicio != null) { if (sMontoServicio.Length > 0) { camposAdicionales.setServicios(sMontoServicio); } } if (sMontoPropina != null) { if (sMontoPropina.Length > 0) { camposAdicionales.setPropina(sMontoPropina); } } if (_sMontoFijo != null) { if (_sMontoFijo.Length > 0) { camposAdicionales.setMontoFijo(_sMontoFijo); } } if (sMontoCashOver != null) { if (sMontoCashOver.Length > 0) { camposAdicionales.setCashOver(sMontoCashOver); } } if ((_sTipoTransaccion.CompareTo("01") == 0) || (_sTipoTransaccion.CompareTo("02") == 0)) { sMTI = "0200"; sTipoTransaccion = "00"; } else if (_sTipoTransaccion.CompareTo("03") == 0) { sMTI = "0200"; sTipoTransaccion = "20"; } else if (_sTipoTransaccion.CompareTo("04") == 0) { sMTI = "0400"; sTipoTransaccion = "00"; } _ISOMsg = new Iso8583Message(); _ISOMsg.Formatter = formato; _ISOMsg.MessageTypeIdentifier = Convert.ToInt32(sMTI); _ISOMsg.Fields.Add(3, sTipoTransaccion + sTipoTarjeta + "00"); _ISOMsg.Fields.Add(113, sTipoPlazoCredito + sMesesGracia); sInfoTrack2 = sInfoTrack2.Trim(); int posicion = sInfoTrack2.IndexOf("="); _sNumeroTarjeta = sInfoTrack2.Substring(0, posicion); _sNumeroTarjeta = protegeTarjeta(_sNumeroTarjeta); if (sModoEntrada.CompareTo("012") == 0) { _ISOMsg.Fields.Add(2, sInfoTrack2.Substring(0, posicion)); _ISOMsg.Fields.Add(14, sInfoTrack2.Substring(posicion + 1)); } else if (sModoEntrada.CompareTo("022") == 0) { _ISOMsg.Fields.Add(35, sInfoTrack2); } _ISOMsg.Fields.Add(22, sModoEntrada); if (sCVV2.Length > 0) { _ISOMsg.Fields.Add(48, sCVV2); } _ISOMsg.Fields.Add(4, sMonto); _ISOMsg.Fields.Add(114, camposAdicionales.toISOHiCC()); _ISOMsg.Fields.Add(11, _sSecuencial); _ISOMsg.Fields.Add(12, _sHora); _ISOMsg.Fields.Add(13, _sFecha.Substring(4)); sMontoBaseGravaIVA = sMontoBaseGravaIVA.Trim(); sMontoBaseNoGravaIVA = sMontoBaseNoGravaIVA.Trim(); _ISOMsg.Fields.Add(119, UTILIDADES.zeropad(sMontoBaseGravaIVA, 12) + UTILIDADES.zeropad(sMontoBaseNoGravaIVA, 12)); sNumeroAutorizacion = sNumeroAutorizacion.Trim(); if (sNumeroAutorizacion.Length == 6) { _ISOMsg.Fields.Add(38, sNumeroAutorizacion); } _ISOMsg.Fields.Add(41, _sTerminalId); _ISOMsg.Fields.Add(42, _sMerchandId); if (sInfoTrack1.Length > 0) { _sNombreTarjeta = extraeTarjetaHabiente(sInfoTrack1); _ISOMsg.Fields.Add(45, sInfoTrack1); } _ISOMsg.Fields.Add(112, sNumeroLote); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Datos:" + _sSecuencial + "|" + _sTerminalId + "|" + _sMerchandId, "LogEvent", "log"); } return(_ISOMsg); }
public String EnvioRequerimiento() { String _respuesta = ""; String sError = ""; String sPosteo; String sFileName; String sFecha; ParserContext parserContext; Hashtable ISO; Iso8583Message isoMsgResp; Socket socket; byte[] bBody; byte[] bBufferRecive; byte[] bBufferEnvio; int iLargoBody; if (this._trama.Length != _lengthTrama) { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + "Longitud de trama Plana de requerimiento no es valida", "LogEvent", "log"); } return(""); } if (this._iGrabaLog == 1) { UTILIDADES.mensaje("INPUT METODO : " + UTILIDADES.ProtegeTramaPlana(_trama), "LogTracer", "log"); } try { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Previo Socket", "LogEvent", "log"); } socket = InicializaSocket(); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Despues Socket", "LogEvent", "log"); } } catch (System.Net.Sockets.SocketException e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } throw new Exception(sError); } try { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Antes de ISO to BYTE", "LogEvent", "log"); } bBufferEnvio = TransformaISOByte(_ISOMsg, "6000081000"); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Despues de ISO to BYTE", "LogEvent", "log"); } } catch (Exception e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } throw new Exception(sError); } try { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Antes de Envio", "LogEvent", "log"); } socket.Send(bBufferEnvio); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Despues de Envio", "LogEvent", "log"); } if (this._iGrabaLog == 1) { UTILIDADES.mensaje("OUTPUT DLL : " + _ISOMsg.ToString(), "LogTracer", "log"); } if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "TimeOut Definido", "LogEvent", "log"); } socket.ReceiveTimeout = Convert.ToInt32(_timeoutPos); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "TimeOut Definido", "LogEvent", "log"); } bBufferRecive = new byte[socket.ReceiveBufferSize]; socket.Receive(bBufferRecive); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Buffer recibido", "LogEvent", "log"); } ISO = TransaformaIso(bBufferRecive); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Transforma buffer ISO", "LogEvent", "log"); } bBody = (byte[])ISO["Body"]; iLargoBody = bBody.Length; parserContext = new ParserContext(iLargoBody); parserContext.Initialize(); parserContext.ResizeBuffer(iLargoBody); parserContext.Write(bBody); isoMsgResp = (Iso8583Message)_ISOMsg.Formatter.Parse(ref parserContext); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Finaliza buffer ISO", "LogEvent", "log"); } if (this._iGrabaLog == 1) { UTILIDADES.mensaje("INPUT DLL : " + isoMsgResp.ToString(), "LogTracer", "log"); } } catch (Exception e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Excepcion :" + sError, "LogEvent", "log"); } if (((Field)_ISOMsg.Fields[3]).Value.ToString().CompareTo("003000") == 0) { try { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Inicia Posteo", "LogEvent", "log"); } sPosteo = UTILIDADES.F3DESEncriptarBloque(UTILIDADES.padMultiplo(UTILIDADES.Byte2Hex(_ISOMsg.GetBytes()), 16, "1C"), _sLlaveKEKIzquierda, _sLlaveKEKDerecha); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Finaliza cifrado", "LogEvent", "log"); } sFecha = DateTime.Now.ToString("HHmmss"); String sRuta = UTILIDADES.AppPath(true) + "Saf\\"; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Valida Ruta de Posteo", "LogEvent", "log"); } Directory.CreateDirectory(sRuta); sFileName = Convert.ToString(_ISOMsg.MessageTypeIdentifier) + ((Field)_ISOMsg.Fields[11]).Value.ToString() + ((Field)_ISOMsg.Fields[41]).Value.ToString() + ((Field)_ISOMsg.Fields[112]).Value.ToString() + sFecha + ".saf"; //new GrabaArchivo(sFileName, sPosteo, false); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Guarda Archivo de Posteo", "LogEvent", "log"); } UTILIDADES.almacenarArchivo(sRuta, sFileName, false, sPosteo); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Ejecuta Posteo", "LogEvent", "log"); } new Posteo(_ip, _puerto, _timeoutPos, _iGrabaLog, _iGrabaMsg, _sLlaveKEKIzquierda, _sLlaveKEKDerecha, _formato); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUG : " + "Finaliza Posteo", "LogEvent", "log"); } if (socket != null) { socket.Close(); } } catch (Exception ex) { sError += "\t" + ex.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + ex.Message, "LogEvent", "log"); } } } if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } throw new Exception(sError); } try { socket.Close(); } catch (Exception e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } } try { new Posteo(_ip, _puerto, _timeoutPos, _iGrabaLog, _iGrabaMsg, _sLlaveKEKIzquierda, _sLlaveKEKDerecha, _formato); } catch (Exception e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } } try { _respuesta = camposTramaPlano(isoMsgResp); } catch (Exception e) { sError += e.Message; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POS : " + e.Message, "LogEvent", "log"); } } if (this._iGrabaLog == 1) { UTILIDADES.mensaje("OUTPUT METODO: " + _respuesta, "LogTracer", "log"); } if (_respuesta.Length > 0) { return(_respuesta); } return(""); }
private void ejecucionPosteo() { try { int iLargoArchivo, iLargoBody; byte[] bArchivo, bBufferEnvio, bBufferRecive, bBody; ParserContext parserContext; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Valida Ruta de Posteo", "LogEvent", "log"); } String _sRuta = UTILIDADES.AppPath(true) + "Saf\\"; String [] files = Directory.GetFiles(_sRuta, "*.saf"); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Optiene Archivos de Posteo", "LogEvent", "log"); } Iso8583Message isoMsgReq, isoMsgResp; Socket socket; Hashtable ISO; String resultado; foreach (String sFile in files) { try { resultado = ""; isoMsgReq = new Iso8583Message(); isoMsgReq.Formatter = _formato; bArchivo = UTILIDADES.Hex2Byte(UTILIDADES.F3DESDencriptarBloque(UTILIDADES.leerArchivo("", sFile), _sLlaveIzq, _sLlaveDer)); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Leer y decifra archivo:" + sFile, "LogEvent", "log"); } iLargoArchivo = bArchivo.Length; parserContext = new ParserContext(iLargoArchivo); parserContext.Initialize(); parserContext.ResizeBuffer(iLargoArchivo); parserContext.Write(bArchivo); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Convierte ISO", "LogEvent", "log"); } isoMsgReq = (Iso8583Message)isoMsgReq.Formatter.Parse(ref parserContext); isoMsgReq.MessageTypeIdentifier = 400; bBufferEnvio = TransformaISOByte(isoMsgReq, "6000081000"); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Inicia Socket", "LogEvent", "log"); } socket = InicializaSocket(); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Inicia Socket Terminado", "LogEvent", "log"); } socket.Send(bBufferEnvio); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Envio de Buffer", "LogEvent", "log"); } if (this._iGrabaLog == 1) { UTILIDADES.mensaje("OUTPUT DLL P: " + isoMsgReq.ToString(), "LogTracer", "log"); } socket.ReceiveTimeout = 10000; if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Set TimeOut: 10000", "LogEvent", "log"); } bBufferRecive = new byte[socket.ReceiveBufferSize]; socket.Receive(bBufferRecive); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Recibe Buffer", "LogEvent", "log"); } ISO = TransaformaIso(bBufferRecive); bBody = (byte[])ISO["Body"]; iLargoBody = bBody.Length; parserContext = new ParserContext(iLargoBody); parserContext.Initialize(); parserContext.ResizeBuffer(iLargoBody); parserContext.Write(bBody); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Convierte Buffer en ISO", "LogEvent", "log"); } isoMsgResp = (Iso8583Message)isoMsgReq.Formatter.Parse(ref parserContext); if (this._iGrabaLog == 1) { UTILIDADES.mensaje("INPUT DLL P: " + isoMsgResp.ToString(), "LogTracer", "log"); } resultado = ((Field)isoMsgResp.Fields[39]).Value.ToString(); if ((resultado.CompareTo("91") != 0) && ((resultado.CompareTo("27") != 0))) { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Valida Respuesta ", "LogEvent", "log"); } File.Delete(sFile); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Elimina: " + sFile, "LogEvent", "log"); } } try { socket.Close(); if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("DEBUGP: " + "Cierra Socket ", "LogEvent", "log"); } } catch (Exception e) { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POSTEO: " + e.Message, "LogEvent", "log"); } } } catch (Exception e) { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POSTEO: " + e.Message, "LogEvent", "log"); } } } } catch (Exception e) { if (this._iGrabaMsg == 1) { UTILIDADES.mensaje("POSTEO: " + e.Message, "LogEvent", "log"); } } }