designerNProgrammer
designerNProgrammer

Reputation: 2701

How to add Paypal buy buttons to items in aspx page?

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

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

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>

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.

Upvotes: 3

Views: 8913

Answers (5)

1NN
1NN

Reputation: 101

For fixed-price buttons, there's a VERY easy, html-only workaround. Just copy the email-link provided by paypal, and create a very normal link using <a> ... </a>, which as content has the image that would normally appear in the <form> statement:

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

I've been searching for a solution today, so even if this thread hasn't been active lately, maybe this can be useful to someone else who wants to avoid code-behind.

Upvotes: 0

Richard Briggs
Richard Briggs

Reputation: 25

I did it using an iframe for each button

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

Here is the code inside 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>

Upvotes: 0

Mike
Mike

Reputation: 11

This is a hack way of doing it, but before the paypal code enter a closing form tag (This will close the asp pages form) then remove the closing form tag from the paypal code and allow the end of .net page end form tag to close the paypals form..

Upvotes: 1

Ben
Ben

Reputation: 3391

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.

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.

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

Upvotes: 13

Gerzie
Gerzie

Reputation: 2340

An ASPX page is like a giant HTML form. You need to close the ASPX form before the PayPal button code starts.

Like this:

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

You can also try creating the button as a URL and hyperlink to some text or an image on your site - you can still use the PayPal button image. When you're viewing the button code within PayPal there should be a tab above it labeled "E-mail". Click that and you'll get a URL - if you're creating buttons with a drop-down menu or text field you cannot turn the button into a URL.

Upvotes: 3

Related Questions