如何使用c#中的参数将用户重定向到paypal

如果我有这样的简单forms,我可以使用它将用户重定向到paypal完成付款:

<input type="hidden" ... <input type="hidden" name="custom" value="" />

因为我必须在将用户重定向到paypal之前做一些操作,我需要正常
asp按钮,在将用户重定向到paypal之前会进行一些操作。
我的问题是如何从c#代码隐藏中做到这一点?
重要的是,我可以从上面的表单传递所有必需的隐藏字段,如“ custom ”。

传统的ASP.Net不允许使用多个表单标记。

以下示例代码将允许您从后面的代码将表单发布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标准字段的更多信息 – PayPal付款标准的HTML变量

您可以动态创建表单并使用C#发布它。 以下代码做到了 –

  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(""); System.Web.HttpContext.Current.Response.Write(string.Format("", FormName)); System.Web.HttpContext.Current.Response.Write(string.Format("
", FormName, Method, Url)); for (int i = 0; i < Inputs.Keys.Count; i++) { System.Web.HttpContext.Current.Response.Write(string.Format("", Inputs.Keys[i], Inputs[Inputs.Keys[i]])); } System.Web.HttpContext.Current.Response.Write("
"); System.Web.HttpContext.Current.Response.Write(""); System.Web.HttpContext.Current.Response.End(); } }

经过全面测试和工作!

您可以构建一个长URL并将所有内容作为URL参数放在那里。 但是,由于您正在使用C#,我建议您使用该API。 在这种情况下,我会看看快速结账 。