【问题标题】:How to add Paypal buy buttons to items in aspx page?如何将 Paypal 购买按钮添加到 aspx 页面中的项目?
【发布时间】:2013-05-31 18:34:54
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: asp.net paypal


    【解决方案1】:

    问题在于 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);
    }
    

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

    【讨论】:

      【解决方案2】:

      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。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我提取了一些代码,但我不记得从哪里来的!
        【解决方案5】:

        对于固定价格按钮,有一个非常简单的html-only解决方法。只需复制贝宝提供的电子邮件链接,并使用&lt;a&gt; ... &lt;/a&gt; 创建一个非常普通的链接,其内容具有通常出现在&lt;form&gt; 语句中的图像:

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

        我今天一直在寻找解决方案,所以即使这个线程最近没有活跃,也许这对想要避免代码隐藏的其他人有用。

        【讨论】:

          猜你喜欢
          • 2014-07-27
          • 2015-11-15
          • 2013-05-29
          • 2013-06-14
          • 1970-01-01
          • 2014-04-22
          • 2015-08-27
          • 1970-01-01
          • 2022-08-04
          相关资源
          最近更新 更多