Friday, April 22, 2011

Basic PayPal integration using a HTTP GET request

To add a simple PayPal button to your website you could use some code like this:
 // Variables
string business = "youremail@example.com";
string item_name = "Screwdriver set";
string item_number = "1054b";
decimal amount = 10.99m;

// Build the PayPal url
StringBuilder sb = new StringBuilder();
sb.Append("cmd=_xclick");
sb.Append("&business=" + HttpUtility.UrlEncode(business));
sb.Append("&no_shipping=1");
sb.Append("&currency_code=GBP");
sb.Append("&lc=GB");
sb.Append("&bn=PP-BuyNowBF");
sb.Append("&item_name=" + HttpUtility.UrlEncode(item_name));
sb.Append("&item_number=" + HttpUtility.UrlEncode(item_number));
sb.Append("&amount=" + HttpUtility.UrlEncode(amount.ToString()));

// Redirect to PayPal to make secure payment
Response.Redirect(@"https://www.paypal.com/cgi-bin/webscr?" + sb.ToString());
Depending on the country you are using PayPal in you may have to change the lc and currency_code lines.
Put this in a button click event on your page, customise the variables as required and you're all set!

3 comments:

Thanks for showing your interest
I will shortly get back to you