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!

Burn! kick it Shout it

9 comments :

Anonymous said...

Hi,
i'm trying to use this code without sucess so far..
i've got a form to send a mail and i want to merge the submit button to both send the mail and send to the paypal page (as if a "buy now" button was clicked)
so one button two actions..How i achieve this ?
my main form is on page.php with : (form name="forml" id="forml" action="send.php" method="post" onsubmit="return verif_champs()" )
my second page "send.php" send the mail, have you got any live example or a solution ?

rtpHarry said...

@Anonymous: You are talking about a PHP script. This site covers asp.net scripts. They are two different languages so you wont be able to use my code with your email script, sorry!

Nic Dunn said...

Hello basically im new to coding. Can you give me the full code with the button event link thing. Thanks

rtpHarry said...

@Nic Dunn: To make the button click code you would drop a button onto the page and then double click on it. If you are really new to this kind of coding I would suggest doing some basic tutorials first before attempting to integrate PayPal. The tutorials that I read when I first started was this series: http://www.asp.net/web-forms/data

I also read a book before that which taught me the underlying c# language which was (an earlier edition of):
http://www.murach.com/books/cs10/index.htm

cjb110 said...

Just a quick note, if your using .NET v4
then use System.Net.WebUtility.HtmlEncode and not HttpUtility.UrlEncode!

rtpHarry said...

@cjb110: HtmlEncode and UrlEncode are two different things. I am not sure that this is the correct advice.

SRocks said...

According to paypal documentation, they support only POST request to submit form data.

But, surprisingly sending data using GET, works too.

Is there any chance that paypal will continue to support this behavior in the future?

Anonymous said...

Hi.. how to get after payment URL

Anonymous said...

hy hi this code is not working for indian currencyplzz help me for the same.