I have a platform that makes purchases via PagSeguro form. The problem is that I'm only getting the POST in which the StatusTransacao
is as "Awaiting Payment", ie in the case of a ticket generated. If the purchase was approved, I do not receive POST and on the PagSeguro platform I see a 500 error.
Below is my code:
[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");
}
}