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

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

问题描述

我是贝宝的新手.我在Paypal上收到了一个沙盒测试项目,并创建了一个内置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图像按钮和锚链接方法,但要在按钮click事件中的服务器上实现此方法:

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.

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

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