How to redirect user to paypal with parameters from C #

If I have a simple form like this, I can use it to redirect the user to paypal for payment:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" ...
    <input type="hidden" name="custom" value="<%= someVariable %>" />
    <input type="submit" value="ok" />
</form>

How should I do some actions before redirecting the user to paypal, I need to do a normal one
asp, which will buy some actions before redirecting the user to paypal.
My question is how can I do this from C # code-behind?
It is important that I can pass all the necessary hidden fields, such as ' custom' from the form above.

+3
source share
3 answers

Traditional ASP.Net does not allow the use of a form tag .

PayPal .

protected void BuyButton_Click(object sender, EventArgs e)
{
   string url = TestMode ? 
      "https://www.sandbox.paypal.com/us/cgi-bin/webscr" : 
      "https://www.paypal.com/us/cgi-bin/webscr";

   var builder = new StringBuilder();
   builder.Append(url);
   builder.AppendFormat("?cmd=_xclick&business={0}", HttpUtility.UrlEncode(Email));
   builder.Append("&lc=US&no_note=0&currency_code=USD");
   builder.AppendFormat("&item_name={0}", HttpUtility.UrlEncode(ItemName));
   builder.AppendFormat("&invoice={0}", TransactionId);
   builder.AppendFormat("&amount={0}", Amount);
   builder.AppendFormat("&return={0}", HttpUtility.UrlEncode(ReturnUrl));
   builder.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(CancelUrl));
   builder.AppendFormat("&undefined_quantity={0}", Quantity);
   builder.AppendFormat("&item_number={0}", HttpUtility.UrlEncode(ItemNumber));

   Response.Redirect(builder.ToString());
}

PayPal Standard Fields - HTML- PayPal Payments

+2

#. -

 protected void Pay()
 {
    RemotePost myremotepost = new RemotePost();
    myremotepost.Url = "https://www.paypal.com/cgi-bin/webscr";
    myremotepost.Add("business", "merchantemail@gmail.com");
    myremotepost.Add("cmd", "_xclick");
    myremotepost.Add("item_name", "Hot Sauce-12oz Bottle");
    myremotepost.Add("amount","5.95");
    myremotepost.Add("currency_code","USD");
    myremotepost.Post();
 }

public class RemotePost
{
    private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
    public string Url = "";
    public string Method = "post";
    public string FormName = "form1";

    public void Add(string name, string value)
    {
        Inputs.Add(name, value);
    }

    public void Post()
    {
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Write("<html><head>");
        System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
        System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
        for (int i = 0; i < Inputs.Keys.Count; i++)
        {
            System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
        }
        System.Web.HttpContext.Current.Response.Write("</form>");
        System.Web.HttpContext.Current.Response.Write("</body></html>");
        System.Web.HttpContext.Current.Response.End();
    }
}

!

0

You can simply create a long URL and put it all in the URL parameters. Since you are working with C #, I would recommend using an API. In this case, I would look at Express Checkout .

-1
source

All Articles