将购物车总变量传递给asp.net中的Paypal

时间:2013-04-03 11:29:58

标签: asp.net paypal

实施Paypal时,我设法将其设置为发送设定值,例如£25,但不是来自购物篮的变量,例如'GrandTotal'取决于顾客进入购物车的内容(我已经创建了自己的购物车。)

<form action="paypal.com/cgi-bin/webscr"; method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="you@youremail.com">
<input type="hidden" name="item_name" value="Item Name">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="amount" value="25.00"> 
<input type="image" src="paypal.com/en_US/i/btn/x-click-but01.gif"; name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> </form>

您知道如何发送变量而不是设定数量吗?以下代码是否接近解决方案?

<input type="hidden" name="amount" value="GrandTotal">

2 个答案:

答案 0 :(得分:0)

您可以通过在输入字段标记中添加runat =“server”来创建输入字段服务器端,并可以从.cs文件中设置它的值

field.value = GrandTotal;

您的aspx页码

<form name="_xclick" target="paypal" action="https://www.paypal.com/cgi-bin/webscr"
method="post" runat="server">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="you@youremail.com" />
<input type="hidden" name="item_name" value="Item Name" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="amount" id="amount" runat="server" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/view_cart.gif" border="0"
    name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="display" value="1">
</form>

您的代码隐藏在页面代码之后   protected void Page_Load(object sender,EventArgs e)     {

    if(!IsPostBack)
    {

        amount.Value = "25.25";
    }

}

答案 1 :(得分:0)

您无法在ASP.Net页面中插入form tag(除非iframe),因为ASP.Net只允许页面中有一个表单标记。

你想在代码背后隐藏这样的东西。它基本上收集用户输入的金额和数量,然后将其发回PayPal。

protected void PayButton_Click(object sender, EventArgs e) {

 string paypalUrl = IsTestMode ? 
   "https://www.sandbox.paypal.com/us/cgi-bin/webscr" : 
   "https://www.paypal.com/us/cgi-bin/webscr";

 var builder = new StringBuilder();

 builder.Append(paypalUrl);

 builder.AppendFormat("?cmd=_xclick&business={0}", 
    HttpUtility.UrlEncode(you@youremail.com));

 builder.Append("&lc=US&no_note=0&currency_code=GBP");

 builder.AppendFormat("&item_name={0}", 
    HttpUtility.UrlEncode(YourItemName));

 builder.AppendFormat("&amount={0}", AmountTextBox.Text);

 builder.AppendFormat("&return={0}", 
    HttpUtility.UrlEncode("http://mysite.cm/ReturnUrl"));

 builder.AppendFormat("&cancel_return={0}", 
    HttpUtility.UrlEncode("http://mysite.cm/CancelUrl"));

 builder.AppendFormat("&undefined_quantity={0}", QuantityTextBox.Text);

 HttpContext.Current.Response.Redirect(builder.ToString());

}