如何在aspx页面中添加Paypal购买按钮

时间:2013-06-05 07:01:42

标签: asp.net paypal

大家好,我是贝宝的新手。我有一个沙盒测试项目onpaypal并创建了一个 item购买按钮,内嵌html代码.. 现在每当我在aspx页面中插入html代码时,它都不会重定向到paypal网站。 也许是因为覆盖html代码的表单标签.. 这是项目的paypal购买按钮的代码

<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服务器标签上它就会将页面重定向到它自己..谢谢。

5 个答案:

答案 0 :(得分:13)

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

有几种解决方法,包括使用here所述的普通ASP图像按钮。

您还可以使用此blog中所述的锚链接。但是,如作者所述,用户可以保存页面源,对其进行编辑(例如更改价格),然后重新加载并点击链接。

事实上,任何将信息存储在网页源中的方法都有可能被滥用。因此,我喜欢的方法是使用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)使用按钮的Click事件在服务器端生成所需信息,然后将浏览器重定向到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);
}

免责声明:用户可能仍然可能滥用此方法(虽然现在有点困难),因此最好在发送货物之前检查已支付的费用。

答案 1 :(得分:3)

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中查看按钮代码时,其上方应该有一个标签为“电子邮件”的标签。单击该按钮即可获得URL - 如果您使用下拉菜单或文本字段创建按钮,则无法将按钮转换为URL。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我使用每个按钮的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>

答案 4 :(得分:0)

对于固定价格按钮,有一个非常简单的仅HTML 解决方法。只需复制贝宝提供的电子邮件链接,然后使用<a> ... </a>创建一个非常普通的链接,该链接作为内容具有通常会出现在<form>语句中的图像:

<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>

我今天一直在寻找解决方案,因此,即使该线程最近未处于活动状态,对于其他想要避免隐藏代码的人来说,这可能也很有用。

相关问题