Asp.net 如何将Paypal购买按钮添加到aspx页面中的项目?

Asp.net 如何将Paypal购买按钮添加到aspx页面中的项目?,asp.net,paypal,Asp.net,Paypal,我是贝宝的新手。我在PayPal上得到了一个沙盒测试项目,并创建了一个 项目购买按钮,这是嵌入式html代码 现在,每当我在aspx页面中插入html代码时,它都不会重定向到paypal站点 可能是因为表单标签覆盖了html代码。以下是物品的paypal buy按钮的代码: <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"

我是贝宝的新手。我在PayPal上得到了一个沙盒测试项目,并创建了一个 项目购买按钮,这是嵌入式html代码

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

可能是因为表单标签覆盖了html代码。以下是物品的paypal buy按钮的代码:

<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服务器标记中,它就会将页面重定向到自身。

aspx页面就像一个巨大的HTML表单。您需要在PayPal按钮代码开始之前关闭ASPX表单

像这样:

<form name="default.aspx">
-- Page content
</form>
<!-- Close the form-->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
-- button code

--页面内容
--按钮代码

您还可以尝试将按钮创建为URL和指向站点上某些文本或图像的超链接-您仍然可以使用PayPal按钮图像。当你在PayPal中查看按钮代码时,上面应该有一个标签“E-mail”。单击该按钮,您将获得一个URL—如果您使用下拉菜单或文本字段创建按钮,则无法将按钮转换为URL

问题在于ASP.NET页面定义了一个表单,所有控件都放在该表单中(特别是在使用母版页时),HTML不允许嵌套表单标记

有几种方法可以解决此问题,包括使用所述的普通ASP图像按钮

您还可以使用本节中所述的锚链。但是,正如作者所指出的,用户可以保存页面源,编辑它(例如更改价格),然后重新加载并单击链接

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

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

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

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

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);
}
protectedvoid PayPalBtn\u点击(对象发送者,图像点击事件参数e)
{
字符串business=“”;
字符串itemName=“”;
双项金额=123.451;
字符串currencyCode=“GBP”;
StringBuilder ppHref=新建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(“¤cy_code=“+currencyCode”);
Response.Redirect(ppHref.ToString(),true);
}

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

这是一种黑客方法,但在paypal代码输入关闭表单标记之前(这将关闭asp页面表单)然后从paypal代码中删除结束表单标记,并允许.net页面结束表单标记关闭paypals表单。

我使用iframe为每个按钮执行此操作

<iframe height="27" marginheight="0" src="/PayPalButton.htm?button_id=ABCXYZSSSSS" frameborder="0" width="120" marginwidth="0" scrolling="no"></iframe>

以下是PayPalButton.htm中的代码

<html>
<head>
<title>PayPal</title>
<script type = "text/javascript">
    // function to get url parameter
    function getURLParameters(paramName) {
        var sURL = window.document.URL.toString();
        if (sURL.indexOf("?") > 0) {
            var arrParams = sURL.split("?");
            var arrURLParams = arrParams[1].split("&");
            var arrParamNames = new Array(arrURLParams.length);
            var arrParamValues = new Array(arrURLParams.length);
            var i = 0;
            for (i = 0; i < arrURLParams.length; i++) {
                var sParam = arrURLParams[i].split("=");
                arrParamNames[i] = sParam[0];
                if (sParam[1] != "")
                    arrParamValues[i] = unescape(sParam[1]);
                else
                    arrParamValues[i] = "No Value";
            }
            for (i = 0; i < arrURLParams.length; i++) {
                if (arrParamNames[i] == paramName) {
                    //alert("Param:"+arrParamValues[i]);
                    return arrParamValues[i];
                }
            }
            return "No Parameters Found";
        }
    }
    // function to get button ID from url 
    function payPalButtonCode() {
        var code = '<input value="_s-xclick" type="hidden" name="cmd" /> <input value="';
        code = code + getURLParameters('button_id');
        code = code + '" type="hidden" name="hosted_button_id" /> '
        document.write(code);
    }
    function payPalButtonQuantity() {
        var button_quantity_low = getURLParameters('button_quantity_low');
        var button_quantity_high = getURLParameters('button_quantity_high');
        var button_quantity_unit = getURLParameters('button_quantity_unit');
        var button_quantity_units = getURLParameters('button_quantity_units');
        var code = '';
        var i;
        if (button_quantity_low != 'No Parameters Found')
        {
            code = '<select name="quantity">';
            for ( i = button_quantity_low; i <= button_quantity_high; i++) {
                if (i > 1) {
                    code = code + String.format('<option value="{0}">{0} {1}</option>', i, button_quantity_units);
                }
                else {
                    code = code + String.format('<option value="{0}">{0} {1}</option>', i, button_quantity_unit);
                }
            }
            code = code + '</select>';
        }
        else
        {
            code = '';
        }
        document.write(code);
    }
    function payPalButtonType() {
        var code = '<input  alt="PayPal – The safer, easier way to pay online." src="'; 

        var button_type = getURLParameters('button_type');
        if (button_type=='buy_now'){
            code = code + 'https://www.paypalobjects.com/en_GB/i/btn/btn_buynow_LG.gif" type="image" name="submit" />';
        }
        else
        {
            //code = code + 'https://www.paypalobjects.com/en_GB/i/btn/btn_subscribe_SM.gif" type="image" name="submit" />';
            code = code + 'https://www.paypalobjects.com/en_GB/i/btn/btn_buynow_LG.gif" type="image" name="submit" />';
        }
        document.write(code);
    }
    String.format = function() {
        // The string containing the format items (e.g. "{0}")
        // will and always has to be the first argument.
        var theString = arguments[0];

        // start with the second argument (i = 1)
        for (var i = 1; i < arguments.length; i++) {
            // "gm" = RegEx options for Global search (more than one instance)
            // and for Multiline search
            var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
            theString = theString.replace(regEx, arguments[i]);
        }

        return theString;
    }
</script>
</head>
<body>
<form id="f1" method="post" action="https://www.paypal.com/cgi-bin/webscr" target="_top">

   <script type="text/javascript">payPalButtonCode();</script>  
    <script type="text/javascript">payPalButtonQuantity();</script> 
    <script type="text/javascript">payPalButtonType();</script>  

    <img alt="" style="border: 0px solid;" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" />
</form>
</body>
</html>

贝宝
//函数获取url参数
函数getURLParameters(paramName){
var sURL=window.document.URL.toString();
if(sURL.indexOf(“?”)大于0){
var arrParams=剩余分割(“?”);
var arrrurlparams=arrParams[1]。拆分(“&”);
var arrParamNames=新数组(arrrurlparms.length);
var arrParamValues=新数组(arrrurlparms.length);
var i=0;
对于(i=0;i”;
代码=代码+'https://www.paypalobjects.com/en_GB/i/btn/btn_buynow_LG.gif“type=“image”name=“提交”/>”;
}
文件。编写(代码);
}
String.format=函数(){
//包含格式项的字符串(例如“{0}”)
//将永远是第一位的
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3GWR6RV47BCVE" target="_top">
    <img src="https://www.paypalobjects.com/it_IT/IT/i/btn/btn_buynowCC_LG.gif" border="0" title="submit" alt="PayPal – The safer, easier way to pay online." />
</a>