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 = "";
        string item_name = "Screwdriver set";
        string item_number = "1054b";
        decimal amount = 10.99m;
        // Build the PayPal url
        StringBuilder sb = new StringBuilder();
        sb.Append("&business=" + HttpUtility.UrlEncode(business));
        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(@"" + 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


Anonymous said...

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

I also read a book before that which taught me the underlying c# language which was (an earlier edition of):

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