-1
I am doing an integration where the system uses socket connection, I send and receive messages to reach an end, and the messages comes in this format:
aplicacao="Teste"
retorno="1"
sequencial="191"
servico="iniciar"
versao="1.0.0"
My question is, is there any way to turn these messages into objects?
Edit1:
Call method:
private bool transacaoTEF(string formaPagamento, string valor, string parcelas, string tipoTransacao)
{
string coleta = string.Empty;
string resposta = string.Empty;
int s = TEF.Metodos.Sequencial();
int sa = 1;
valor = valor.Replace(",", ".");
if (!TEF.Metodos.Conectar()) return false;
Iniciar:
// INICIAR
resposta = TEF.Metodos.IniciaTransacao(s.ToString());
if (resposta.Contains("retorno=\"2\""))
{
s += 2;
goto Iniciar;
}
if (resposta.Contains("retorno=\"1\""))
{
s += 1;
resposta = TEF.Metodos.TipoTransacao(valor, s.ToString(), tipoTransacao);
}
while (true)
{
if (resposta.Contains("retorno=\"0\""))
{
if (resposta.Contains("automacao_coleta_tipo"))
{
using (ColetaTefForm form = new ColetaTefForm(resposta))
if (form.ShowDialog() == DialogResult.OK)
coleta = form.coleta;
sa = Convert.ToInt32(TEF.Metodos.RetornaValor(resposta, "automacao_coleta_sequencial"));
resposta = TEF.Metodos.AutomacaoColetaInformacao(sa.ToString(), coleta);
continue;
}
if (resposta.Contains("automacao_coleta_mensagem"))
{
MessageBox.Show(TEF.Metodos.RetornaValor(resposta, "automacao_coleta_mensagem"));
sa = Convert.ToInt32(TEF.Metodos.RetornaValor(resposta, "automacao_coleta_sequencial"));
resposta = TEF.Metodos.Processar(sa.ToString());
continue;
}
if (resposta.Contains("transacao_comprovante"))
{
s = Convert.ToInt32(TEF.Metodos.RetornaValor(resposta, "automacao_coleta_sequencial"));
TEF.Metodos.ConfirmarTransacao(s.ToString(), tipoTransacao);
TEF.Metodos.Finalizar((s + 1).ToString());
return true;
}
}
if (resposta.Contains("retorno=\"9\""))
{
MessageBox.Show(TEF.Metodos.RetornaValor(resposta, "mensagem"));
}
}
return false;
}
Class "Methods"
public class Metodos
{
private static TcpClient tcpClient;
private static NetworkStream networkStream;
public static bool Conectar()
{
try
{
string host = ConfigurationManager.AppSettings["host"];
string port = ConfigurationManager.AppSettings["port"];
tcpClient = new TcpClient();
tcpClient.Connect(host, Convert.ToInt32(port));
return true;
}
catch (Exception e)
{
MensagemErro(e);
return false;
}
}
public static int Sequencial()
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/sequencial.txt"))
return Convert.ToInt32(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "/sequencial.txt"));
else
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "/sequencial.txt").Dispose();
return 1;
}
}
catch (Exception e)
{
MensagemErro(e);
return 0;
}
}
public static void SalvarSequencial(string s)
{
try
{
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "/sequencial.txt", s);
}
catch (Exception e)
{
MensagemErro(e);
}
}
public static string IniciaTransacao(string sequencial)
{
return Retorno("aplicacao=\"ProComercio\" " +
"versao=\"1.0.0\" " +
"retorno=\"1\" " +
"sequencial=\"" + sequencial + "\" " +
"servico=\"iniciar\"");
}
public static string TipoTransacao(string valor, string sequencial, string tipoTransacao)
{
if (valor.Contains(","))
valor = valor.Replace(",", ".");
return Retorno("retorno=\"1\" " +
"sequencial=\"" + sequencial + "\" " +
"servico=\"executar\" " +
"transacao=\"" + tipoTransacao + "\" " +
"transacao_valor=\"" + valor + "\"");
}
public static string AutomacaoColetaInformacao(string sequencial, string informacao)
{
return Retorno("automacao_coleta_sequencial =\"" + sequencial + "\" " +
"automacao_coleta_retorno=\"0\" " +
"automacao_coleta_informacao=\"" + informacao + "\"");
}
public static string Processar(string sequencial)
{
return Retorno("automacao_coleta_sequencial =\"" + sequencial + "\" " +
"automacao_coleta_retorno=\"0\"");
}
public static string ConfirmarTransacao(string sequencial, string tipoTransacao)
{
return Retorno("sequencial =\"" + sequencial + "\" " +
"retorno=\"0\" " +
"servico=\"executar\" " +
"transacao=\"" + tipoTransacao + "\"");
}
public static string Finalizar(string sequencial)
{
return Retorno("retorno=\"1\" " +
"sequencial =\"" + sequencial + "\" " +
"servico=\"finalizar\"");
}
public static string RetornaValor(string resposta, string palavraChave)
{
string[] lista = resposta.Split(new[] { "\n" }, StringSplitOptions.None);
string linha = "";
foreach (string l in lista)
{
if (l.Contains(palavraChave + "=\""))
{
linha = l;
}
}
return linha.Split(new[] { "\"" }, StringSplitOptions.None)[1];
}
public static string ViaEstabelecimento(string nota)
{
int inicio = nota.LastIndexOf("transacao_comprovante_1via=\"");
int final = nota.IndexOf("transacao_comprovante_2via=\"");
string comprovante = nota.Substring(inicio, final - inicio);
return comprovante.Split(new[] { "\"" }, StringSplitOptions.None)[1];
}
public static string ViaCliente(string nota)
{
int inicio = nota.LastIndexOf("transacao_comprovante_2via=\"");
int final = nota.IndexOf("transacao_data");
string comprovante = nota.Substring(inicio, final - inicio);
return comprovante.Split(new[] { "\"" }, StringSplitOptions.None)[1];
}
public static string Retorno(string msg)
{
try
{
if (!string.IsNullOrEmpty(msg))
{
networkStream = tcpClient.GetStream();
byte[] sendBytes = Encoding.ASCII.GetBytes(msg);
networkStream.Write(sendBytes, 0, sendBytes.Length);
}
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
networkStream.Read(bytes, 0, Convert.ToInt32(tcpClient.ReceiveBufferSize));
string retorno = Encoding.ASCII.GetString(bytes);
return retorno;
}
catch (Exception e)
{
MensagemErro(e);
return null;
}
}
public static void MensagemErro(Exception e)
{
MessageBox.Show("Ocorreu um erro com o V$PagueClient!\n\n" + e, "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I’m using substring at the moment, but it’s not accurate, because some answers come like this: https://textuploader.com/153ev
Yes, it is possible, present your code
– Leandro Angelo
I just need a practical example with any string, the code is too long to run here
– Diego Berleis
Always comes in the same format the return?
– Gustavo Luciano
Yes @Gustavoluciano
– Diego Berleis
@Leandroangelo posted the code
– Diego Berleis
Why not instantiate the class and pass the attributes ?
– Joy Peter
I don’t quite understand @Joypeter, how can I do that? This data I get comes as string
– Diego Berleis
Creates a class with the return attributes and then instants passing the values to the respective attribute.
– Joy Peter
In this part I’m having difficulties @Joypeter, how can I do it?
– Diego Berleis