Sending form to Pagseguro

Asked

Viewed 496 times

1

I have this form

@using (Html.BeginForm("PacoteAdd", "Pacote"))
        {
        <input type="hidden" name="email_cobranca" value="*****@hotmail.com" />
        <input type="hidden" name="tipo" value="CBR" />
        <input type="hidden" name="moeda" value="BRL" />
        <input type="hidden" name="item_id" value="@item.PacoteID" />
        <input type="hidden" name="item_descr" value="@item.Titulo" />
        <input type="hidden" name="item_quant" value="1" />
        <input type="hidden" name="item_valor" value="@item.valor" />
        <input type="image" name="submit" src="https://p.simg.uol.com.br/out/pagseguro/i/botoes/pagamentos/99x61-comprar-assina.gif"
        alt="Pague com PagSeguro - é rápido, grátis e seguro!" />
}

and this code in the controller

[HttpPost]
public void PacoteAdd(FormCollection fomr)
{
     string email_cobranca = Request.Form["email_cobranca"];
     string tipo = Request.Form["tipo"];
     string moeda = Request.Form["moeda"];
     string item_id = Request.Form["item_id"];
     string item_descr = Request.Form["item_descr"];
     string item_quant = Request.Form["item_quant"];
     string item_valor = Request.Form["item_valor"];
}

and would need to send this form on the link

https://sandbox.pagseguro.uol.com.br/checkout/checkout.jhtml

because the original code of the button would be this below

<form target="pagseguro" method="post" action="https://sandbox.pagseguro.uol.com.br/checkout/checkout.jhtml">
      <input type="hidden" name="email_cobranca" value="*****@hotmail.com" />
      <input type="hidden" name="tipo" value="CBR" />
      <input type="hidden" name="moeda" value="BRL" />
      <input type="hidden" name="item_id" value="@item.PacoteID" />
      <input type="hidden" name="item_descr" value="@item.Titulo" />
      <input type="hidden" name="item_quant" value="1" />
      <input type="hidden" name="item_valor" value="@item.valor" />
      <input type="image" name="submit" src="https://p.simg.uol.com.br/out/pagseguro/i/botoes/pagamentos/99x61-comprar-assina.gif" alt="Pague com PagSeguro - é rápido, grátis e seguro!" />
</form>

what you should use to send?

1 answer

1


Only include external url in Html.BeginForm:

@Html.BeginForm(null, null, FormMethod.Post, new {@action="https://sandbox.pagseguro.uol.com.br/checkout/checkout.jhtml"}
)
{
... aqui vão os inputs e botão submit
}

To post data from the controller you can use the HttpClient:

var parametros = new List<KeyValuePair<string, string>>();
parametros.Add(new KeyValuePair<string, string>("email_cobranc", "*****@hotmail.com"));
// Adicionar os outros parâmetros

var content = new System.Net.Http.FormUrlEncodedContent(parametros);
var cliente = new System.Net.Http.HttpClient { BaseAddress = new Uri("https://sandbox.pagseguro.uol.com.br") };

var response = cliente.PostAsync("/checkout/checkout.jhtml", content).Result;
if (response.IsSuccessStatusCode)
{
    // aqui você trata o retorno
}

You may need to add a line to avoid certificate errors since your link is https.

  • i need the form to be sent from the controller, because I need to perform an action on the controller before sending the form.

  • see the answer, edited with in example how you can make the request in the controller

Browser other questions tagged

You are not signed in. Login or sign up in order to post.