«

»

Jul 26

Adding PayPal donate button to ASP.NET page, the easy way

I’ve been looking all over the Internets for a simple straightforward way of adding a donate button to my ASP.NET page but all I could find were bloated plugins or simple GET forms. Since I had quite a huge parameter called “encrypted” in my PayPal button generated code, sending it in the URL might not be such a good idea, as many browsers are limited in that area. In my case the “encrypted” value was around 2500 bytes.

I also considered that PayPal might have a good reason to need the form sent via POST so I finally decided to build the form dynamically in JavaScript.

Don’t forget to paste in the value of the “encrypted” field from your generated code on PayPal. Here’s the JS code:

function Donate()
{
	var myform = document.createElement("form");
	myform.action = "https://www.paypal.com/cgi-bin/webscr";
	myform.method = "post";
	myform.target = "_blank";

	var cmd = document.createElement("input");
	cmd.name = "cmd";
	cmd.type = "hidden";
	cmd.value = "_s-xclick";

	var encrypted = document.createElement("input");
	encrypted.type = "hidden";
	encrypted.name = "encrypted";
	encrypted.value = "";

	var image = document.createElement("input");
	image.type = "image";
	image.src = "https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif";
	image.border = "0";
	image.name = "submit";
	image.alt="PayPal - The safer, easier way to pay online!";

	//Most probably this can be skipped, but I left it in here since it was present in the generated code
	var pixel = document.createElement("image");
	pixel.border = "0";
	pixel.alt="";
	pixel.src = "https://www.paypalobjects.com/en_US/i/scr/pixel.gif";
	pixel.width = "1";
	pixel.height = "1";

	myform.appendChild(cmd);
	myform.appendChild(encrypted);
	myform.appendChild(image);
	myform.appendChild(pixel);

	myform.submit();
}

And here is the image you need to place anywhere on the ASP.NET page:

<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif"�
border="0"�
alt="PayPal - The safer, easier way to pay online!"�
onclick="javascript:Donate();"�
style="cursor:pointer;"/>

Good Luck!

About the author

Stefan Goilav

1 comment

  1. Janis Baldwin

    I REALLY hoped this would work but I pasted the code in my aspx page exactly as is. The Donate button displayed but nothing happened when clicked. I tried this in my Visual Studio 2010 web development environment (C#) using a master page. Thoughts?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>