2
I have a platform that makes purchases via Pagseguro form. The problem is that I’m only receiving the POST in which the StatusTransacao
is as "Waiting for Payment", ie in the case of a billet generated. If the purchase was approved, I do not receive POST and on the platform of Pagseguro visualize an error 500.
Follow my code below:
[HttpPost]
public ActionResult RetornoPagamento(FormCollection collection)
{
string Token = "";
string Pagina = "https://pagseguro.uol.com.br/pagseguro-ws/checkout/NPI.jhtml";
string Dados = System.Web.HttpContext.Current.Request.Form.ToString() + "&Comando=validar" + "&Token=" + Token;
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Pagina);
req.Method = "POST";
req.ContentLength = Dados.Length;
req.ContentType = "application/x-www-form-urlencoded";
System.IO.StreamWriter stOut = new System.IO.StreamWriter(req.GetRequestStream(), System.Text.Encoding.GetEncoding("ISO-8859-1"));
stOut.Write(Dados);
stOut.Close();
System.IO.StreamReader stIn = new System.IO.StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("ISO-8859-1"));
string Result = stIn.ReadToEnd();
stIn.Close();
if (Result == "VERIFICADO")
{
SqlConnection MinhaConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["BancoDados"].ConnectionString);
MinhaConexao.Open();
string query = "";
CursoAlunoAplicacao bdCursoAluno;
bdCursoAluno = CursoAlunoAplicacaoConstrutor.CursoAlunoAplicacaoEF();
var VerificarCursoAluno = bdCursoAluno.ListarTodos().Where(x => x.Transacao == collection["TransacaoID"]);
if (VerificarCursoAluno.Count() >= 1)
{
if (collection["StatusTransacao"] == "Aprovado")
{
query = "UPDATE CursoRapido_CursoAluno SET Pagamento = 1 WHERE Transacao = '" + collection["TransacaoID"] + "'";
}
}
else
{
if (collection["StatusTransacao"] == "Aprovado")
{
query = "INSERT INTO CursoRapido_CursoAluno (Pagamento,Aluno_ID,Cursos_ID,Transacao) VALUES (1, '" + collection["Referencia"] + "'," + collection["ProdID_1"] + ",'" + collection["TransacaoID"] + "')";
}
else
{
query = "INSERT INTO CursoRapido_CursoAluno (Pagamento,Aluno_ID,Cursos_ID,Transacao) VALUES (0, '" + collection["Referencia"] + "'," + collection["ProdID_1"] + ",'" + collection["TransacaoID"] + "')";
}
}
SqlCommand comando = new SqlCommand(query, MinhaConexao);
comando.ExecuteNonQuery();
MinhaConexao.Close();
}
return View();
}
public ActionResult RetornoPagamento()
{
string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
var ID_Transacao = url.Substring(url.IndexOf("ID_Transacao="));
ID_Transacao = ID_Transacao.Replace("ID_Transacao=", "");
ID_Transacao = ID_Transacao.Replace("-", "");
CursoAlunoAplicacao bdCursoAluno;
bdCursoAluno = CursoAlunoAplicacaoConstrutor.CursoAlunoAplicacaoEF();
if (bdCursoAluno.ListarTodos().Where(x => x.Transacao == ID_Transacao).Count() > 0)
{
var IDAtual = System.Web.HttpContext.Current.User.Identity.Name;
var CursoComprado = bdCursoAluno.ListarTodos().Where(x => x.Aluno.ID == int.Parse(IDAtual)).LastOrDefault();
string Parametro = IDAtual + "-" + CursoComprado.Cursos.ID;
return RedirectToAction("CursoDetalhe", new { id = Parametro });
}
else
{
return RedirectToAction("AguardandoPagamento");
}
}
Why not uses the Pagseguro package?
– Leonel Sanches da Silva
Because as it is a simple application and even without cart, I thought it would be easier so
– Rafael Barbosa
Apparently it is, but I can see that you have no way to make the mistake. See this answer to learn how to use the package.
– Leonel Sanches da Silva
I visualized the link you gave me but did not find there the way to work with the return. Sending is ok, is working in my current code. I just don’t understand why the POST error 500 when trying to access my Feedback action
– Rafael Barbosa
I can’t remember, but I think it’s in the object
payment
. I’ll check for you when I can– Leonel Sanches da Silva
Okay, I’ll be waiting. Thank you.
– Rafael Barbosa
@Rafaelbarbosa just to make sure you understand how Pagseguro works, the POST it sends to your server is server-to-server communication, it doesn’t work on the "client" side. Have you tried the Pagseguro test environment?
– Guilherme Nascimento
@Guilhermenascimento I understand this, so I have my Return method with [Httppost], to receive the post confirmation POST and execute my query. The problem is that I only receive POST at the time the purchase is made via boleto.
– Rafael Barbosa
@Rafaelbarbosa was just to be sure :) tell me are you using the test environment? I’ll see if I can test your code today
– Guilherme Nascimento
@Guilhermenascimento Ah yes. So, to be honest I used the test environment until I received something positive, which in the case was the interaction I got when I generated boleto, after that I continued in stubbornness from the point that my code was communicating with Pagseguro.
– Rafael Barbosa