如何将 Paypal 购买按钮添加到 aspx 页面中的项目? [英] How to add Paypal buy buttons to items in aspx page?

查看:19
本文介绍了如何将 Paypal 购买按钮添加到 aspx 页面中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是贝宝的新手.我在paypal上有一个沙盒测试项目并创建了一个item 购买按钮,内嵌 html 代码.

I am a newbie to paypal. I got a sandbox test item onpaypal and created an item Buy button which is embedded html code.

现在每当我在 aspx 页面中插入 html 代码时,它都不会重定向到 paypal 站点.

Now whenever I insert the html code in the aspx page, it dosen't redirect to the paypal site.

也许是因为覆盖了html代码的表单标签.这是一个项目的贝宝购买按钮的代码:

Maybe because of the form tag that covers the html code. Here is the code for paypal buy button for an item:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="3GWR6RV47BCVE">
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

我在一个普通的 HTML 文件中尝试了这段代码,它奏效了.但是,一旦我将其放入 aspx 上的表单 runat 服务器标记中,它就会将页面重定向到自身.

I tried this code in a plain HTML file, and it worked. But as soon as I put it in a form runat server tag on aspx, it redirects the page to itself.

推荐答案

问题是 ASP.NET 页面定义了一个表单,其中放置了所有控件(特别是如果您使用的是母版页),而 HTML 不允许嵌套的表单标签.

The problem is that ASP.NET pages define a form within which all the controls are placed (especially if you are using a master page) and HTML does not allow nested form tags.

有几种方法可以解决这个问题,包括使用普通的 ASP 图像按钮,如此处.

There are several ways around this including using a normal ASP image button as described here.

您还可以使用此 博客.然而正如作者​​所指出的,用户可以保存页面源代码,对其进行编辑(例如更改价格),然后重新加载并单击链接.

You can also use an anchor link as described in this blog. However as noted by the author, the user can save the page source, edit it (e.g. change the price) and then reload it and click the link.

事实上,任何将信息存储在网页源中的方法都有可能被滥用.因此,我喜欢的方法是结合使用 ASP 图像按钮和锚链接方法,但要在按钮单击事件中的服务器上实现:

In fact any method that stores the information in the source of the webpage has potential to be abused. Therefore the approach I like, is to use a combination of an ASP image button and the anchor link approach but to implement this on the sever within the button click event:

1) 在您的 ASP 页面中定义一个图像按钮,您希望将 PayPal 按钮放在那里.您可以将 ImageURL 设置为 PayPal 提供的首选按钮类型.

1) In your ASP page define an image button where you want the PayPal button to go. You can set the ImageURL to the preferred button type provided by PayPal.

<asp:ImageButton
    ID="PayPalBtn"
    runat="server"
    ImageUrl="https://www.paypalobjects.com/en_GB/i/btn/btn_buynow_LG.gif"
    onclick="PayPalBtn_Click" />

2) 使用按钮的 Click 事件在服务器端生成所需信息,然后将浏览器重定向到 PayPal 站点.

2) Use the Click event of the button to generate the required information on the server side and then redirect the browser to the PayPal site.

protected void PayPalBtn_Click(object sender, ImageClickEventArgs e)
{
    string business = "<insert your paypal email or merchant id here>";
    string itemName = "<insert the item name here>";
    double itemAmount = 123.451;
    string currencyCode = "GBP";

    StringBuilder ppHref = new StringBuilder();

    ppHref.Append("https://www.paypal.com/cgi-bin/webscr?cmd=_xclick");
    ppHref.Append("&business=" + business);
    ppHref.Append("&item_name=" + itemName);
    ppHref.Append("&amount=" + itemAmount.ToString("#.00"));
    ppHref.Append("&currency_code=" + currencyCode);

    Response.Redirect(ppHref.ToString(), true);
}

免责声明:用户仍有可能滥用这种方法(尽管现在有点困难),因此最好在发货前检查已支付的金额.

Disclaimer: It may still be possible for users to abuse this approach (although it is now a bit harder) so it is always best to check what has been paid before dispatching goods.

这篇关于如何将 Paypal 购买按钮添加到 aspx 页面中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆