.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: How to Integrate Both Google Checkout and PayPal In 3 Steps


Trying to get Google Checkout and PayPal to work with a custom .NET site or solution has been a problem for as long as they’ve been around. If you don’t know what you’re doing, it can be a daunting task, and sometimes even if you do know what you’re doing! Well, I am going to take the guesswork out of this chore once and for all, as I have recently discovered the ultimate way to integrate these payment gateways into your website.

If you’re saying, “Wait a second, haven’t you already written an article on this?” you are correct. However, the solution I present to you here is infinitely simpler and requires fewer moving parts. The only reason I didn’t take the previous article down is because it does present interesting information about nesting MasterPages.

The Solution

In their documentation, PayPal and Google Checkout both mention that you must put their code into your site exactly as they have it, using the HTML forms they way they have structured them. This is not true. There are only a few bits of information that are required, and so long as they are present in an acceptable fashion, your pass-off to these gateways will work smoothly and flawlessly.

  1. Get rid of your form tags. That’s right. Lose the form tags. You will only need your <form runat="server"> tag, no more nested HTML forms. W3C compliancy, here we come! However, don’t lose the information contained in your old <form> tag attributes. You’ll still need some of that.

  2. Create image buttons for Google Checkout and PayPal. While they don’t have to be image buttons (for functionality’s sake), both Google and PayPal will get upset if you have standard buttons instead of their images as checkout buttons. It is in this button where the magic happens – the button has a property called PostBackURL. This is where you will put the action string from your form. For instance, PayPal’s PostBackURL value is “https://www.paypal.com/us/cgi-bin/webscr&#8221;. Google’s will be “https://checkout.google.com/cws/v2/Merchant/%5Byour merchant number]/checkoutForm”. You can put both Google’s and PayPal’s buttons next to each other. Here is some sample code:
    <asp:imagebutton id="btnSubmitPaypal" runat="server" imageurl="/images/checkout-paypal.gif" alternatetext="Purchase Using Paypal" postbackurl="https://www.paypal.com/us/cgi-bin/webscr" />

  3. Create your hidden inputs to transfer information to PayPal and Google Checkout (from the same page, no extra postbacks!). However you dynamically create your hidden input tags, keep doing it. Just keep this in mind: Google Checkout and PayPal have no tags in common except one, the “item_name_x” (where “x” is a number) tag. If this is the same for both Google Checkout and PayPal, leave it alone and only have one! If you must have two (as I do), make sure you put Google’s tags before PayPal’s. This is important because Google reads the first tags that match what it’s looking for and ignores everything else. PayPal will take the last tag that matches its requirements.

    I mention this because Google Checkout has an “item_description_x” attribute that will take additional information about the item where PayPal does not, meaning that PayPal users have to cram all the info about their product into the “item_name_x” tag. Thus, if you change the order, Google will display double information (PayPal’s “item_name_x” plus their own “item_description_x” tag) and PayPal will display only what you want to transfer to Google Checkout (the last “item_name_x” tag).

  4. BONUS: Hook up Google Analytics. For those advanced users who want to hook up their Google Analytics, you still can using this method. The asp:button element has an attribute called OnClientClick. Put the following as the value for this attribute in your Google Checkout image button: setUrchinInputCode();

For those looking for some sample code on how to dynamically generate your input tags, simply create a literal somewhere on the page and populate it with the following function:

Public Function CreateCheckoutTag(ByVal att As String, ByVal value As String) As String
		Dim input As String = "<input type=""hidden"" name=""" & att & """ value=""" & CleanText(value) & """ />"
		Return input
	End Function

Where the CleanText(value) function cleans any possible special characters from the value. Not necessary, but recommended.And that’s it! Three or four easy steps and you’re ready to use both Google Checkout and PayPal Website Payments on your website! I hope this saves someone 60 or 70 hours of their life, as it would have mine had I known about it three years ago 🙂

kick it on DotNetKicks.com

Advertisements

February 27, 2008 - Posted by | ASP.NET, Google, MasterPages, PayPal, Tips & Tricks, UpdatePanel

52 Comments »

  1. […] – There is a much better way to integrate PayPal and Google Checkout. Please see my new article, ASP.NET: How To Integrate Both Google Checkout and PayPal In 3 Steps. The article on the page you are currently viewing should be used as a reference on how to nest […]

    Pingback by ASP.NET, PayPal, and Google Checkout: The Solution! « .NET Discussion | February 27, 2008 | Reply

  2. Thanks – worked a treat!

    Comment by Gregor | March 4, 2008 | Reply

  3. Hi,
    I have setup the postback url of the asp button to Paypal’s url.
    But when I click on the Asp button, the page just reloads but is never redirected to paypal.
    Any idea why?

    Here is my code

    Comment by Granada | March 12, 2008 | Reply

  4. Granada –

    Your code didn’t post. Try again or email it to me at fixmysite.please[at]gmail.com

    Comment by Some.Net(Guy) | March 12, 2008 | Reply

  5. Hi Please Send me full details of integration of paypal also asp.net c# source code

    Comment by Milap | April 2, 2008 | Reply

  6. Milap –

    If you have a shopping cart or have created your own, follow these instructions:

    https://www.paypal.com/IntegrationCenter/ic_standard_home.html#CartUpload

    coupled with my instructions here and you should have no problems integrating PayPal. That’s all I did.

    Comment by Some.Net(Guy) | April 2, 2008 | Reply

  7. […] the way, I found a really good blog post on How To Integrate Google Checkout and Paypal In 3 Steps. Boy I need […]

    Pingback by One Week Without Search - Day 1 | rumblepup | April 22, 2008 | Reply

  8. […] ASP.NET: How to Integrate Both Google Checkout and PayPal In 3 Steps « .NET Discussion (tags: googlecheckout paypalcheckout .net asp.net) […]

    Pingback by links for 2008-04-23 - stevienova.com | April 23, 2008 | Reply

  9. this was really helpful…thanks a lot….

    Comment by crazy.net | April 25, 2008 | Reply

  10. Thanks for the article – works great. I too spent a number of hours working on this which I thought would be simple – had done it really easily under classic ASP, and didn’t think it would be more difficult in .NET!!

    Question for you though – I want to keep track of my own order details before handing off to Paypal (so I have a form where the user enters name, address etc which I then write to the database) and JUST use Paypal for credit card authorization. I have a solution whereby I have the form of info the user fills in, on submit of that the codebehind writes all data to the database, does a redirect to a “stepping stone” page which builds the form for Paypal (including the asp:imagebutton named myButton) and then uses a Page.ClientScript.RegisterStartupScript to automatically do a myButton.click when the page is loaded – in essence the user thinks they jump directly to Paypal when they click Submit back on the first page, but it actually takes two page jumps to get there. Is there no way I can do that in one page instead of two? Can you intercept an event in server-side code before the form submits to the postbackurl? I am assuming not (as the browser can only submit the form once to one server) but just wondering if you knew of a slick way?

    Thanks!

    Comment by Nathan | May 18, 2008 | Reply

  11. Nathan –

    I too would like to do what you’re suggesting, but unfortunately I haven’t come up with a way, and I really haven’t tried too hard to find one simply because it doesn’t REALLY matter what’s in someone’s cart when they go to PayPal/Google, it’s what’s in their cart after they’ve bought something 🙂 Of course it could be useful for determining abandoned cart statistics, but I don’t really care about that. If you set up PDT or IPN, PayPal will do all the work for you post-transaction. PDT isn’t reliable, since not everyone will go back to your site, and I never really got around to IPN, so I pretty much just wait for the email to hit my inbox before I process an order.

    I think for what you’re doing, your “stepping stone” page is probably fine, considering the user experience won’t differ. Thanks for the comment, and if you come across your “slick way” be sure to let me know!

    Comment by Some.Net(Guy) | May 18, 2008 | Reply

  12. Do you have a more detail instuction for step 3? I am still getting stuck on passing the google hidden field in my OnClick button.

    Comment by Tommy | May 18, 2008 | Reply

  13. Tommy –

    Checkout this link from Google on how to use their variables for submission:

    http://code.google.com/apis/checkout/developer/Google_Checkout_Basic_HTML_Overview.html

    Just remember that if you follow my guidelines in this article, you will disregard their FORM structure and use the same hidden inputs as shown in this link.

    Comment by Some.Net(Guy) | May 18, 2008 | Reply

  14. i want integrate paypal for getting payment in my website.but i dontknow how to write code for this.can anyone help me?

    Comment by prabhu | May 21, 2008 | Reply

  15. Thank you so much; thanks to your great instructions and insight I got this implemented in no time – you’re a life saver (and major TIME saver)! Nice site too, btw – I appreciate you taking the time to help others out.

    Comment by sue | September 7, 2008 | Reply

  16. Thanks, Sue! Glad I could help 🙂

    Comment by Some.Net(Guy) | September 8, 2008 | Reply

  17. Simple and very nice.

    Thanks a lot.

    Comment by David | September 24, 2008 | Reply

  18. Hi,

    Thanks a lot..its really a nice example.

    Cheers!!!

    Ujjwal B Soni
    +919998971048

    Comment by Ujjwal B Soni | October 1, 2008 | Reply

  19. Thank you, this was incredibly helpful! I could never have accomplished this without your clear instructions.

    Comment by Lynn | October 11, 2008 | Reply

  20. Ok wait – I found this solution using javascript that is SOOOOOOO brilliantly simple compared to any of this MasterPage and Ghostform lunacy that I can’t believe this isn’t the top search returned on Google for this issue:
    http://www.nerdymusings.com/LPMArticle.asp?ID=29

    Basically you just need to remove the form tags from the Paypal HTML and then replace the image button with an A link around an image. In the A link you have javascript that resets “theForm” which is ASP.NET’s main form to point to Paypal instead of back to your page.


    David

    Comment by David | October 11, 2008 | Reply

  21. Well you posted this same comment on my other post, so clearly you must have read this one… again I’ll stress that your solution uses hacked Javascript. This solution uses .NET built in functionality. Plus it’s easier to control on the server side.

    Comment by Some.Net(Guy) | October 13, 2008 | Reply

  22. Thanks – this allowed me to move from a “nested” form approach to a cleaner solution by changing just a couple lines of code.

    Comment by Techman | October 27, 2008 | Reply

  23. Thanks for the article–I’m about 99% there, but stuck on this one problem.

    I have a single shopping page for my small company where all my products are one one page. I only have about 8 products, and each one has its own PayPal “Add to Cart” button. It seems that no matter what I do, PayPal always thinks that I’m adding the very *last* item to my shopping cart instead of the one I clicked.

    Any thoughts? Thanks.

    Comment by Michael Paul | November 16, 2008 | Reply

    • Hi This is Jignesh Patel from India

      What i suggest is, instead of adding paypal button on each item, collect the no of products selected by user first and then when user click on “checkout” then redirect your page to Paypal.

      Comment by Jignesh | June 11, 2009 | Reply

  24. Michael –

    Have you checked the code for all your “Add To Cart” buttons? I would guess that they’re all the same, and all reference your last item. Let me know if that’s not the problem.

    Comment by Some.Net(Guy) | November 16, 2008 | Reply

  25. Absolutely awesome!!
    Took me less than 10 minutes to get it working.
    I only need (so far) a Google button, but the <asp:ImageUrl tag is the way to go!!
    I already had the input tags and just left them in place!
    Thank you so much!!

    Comment by BruceShining | November 27, 2008 | Reply

  26. Darn!! My prior comment should have said that the <asp:ImageButton tag is the way to go.

    Comment by BruceShining | November 27, 2008 | Reply

  27. very useful!

    Comment by lee | December 1, 2008 | Reply

  28. Wow, great fix I must say. I started doing the Google recommended way of installing a new dll and a whole bunch of junk when I thought, “there must be another way.”

    Here it is. Thanks a bunch!

    Comment by Brian Manning | March 3, 2009 | Reply

  29. I have run into a problem using this method to create PayPal AddToCart and ViewCart buttons.
    If you have more than one button per page, they don’t all work correctly. All buttons on the page perform the operation expected for the last button. It makes sence, since all of the hidden values are in a single form element. Each buttons values overwrites those of the form before it.
    If you have a view cart button at the end all of the buttons on the page will become view cart buttons.
    Does anyone have an elegant solution to this?
    Thanks

    Comment by Mike Berger | March 30, 2009 | Reply

  30. I would say the only way to do what you’re trying to do is with nested forms. Create your values in separate forms with separate submit buttons. Either that or just separate your “Add to cart” and “Checkout” buttons on to different pages.

    Good luck!

    Comment by Some.Net(Guy) | March 30, 2009 | Reply

  31. Thanks, that is what I have done. I was hoping for some elegant solution, but I couldn’t find one.

    Comment by Mike Berger | March 31, 2009 | Reply

  32. While there may be an elegant solution for many things, sometimes you just have to bite the bullet and write that nasty code 🙂 And unfortunately, usually when you’re trying to integrate with a system like PayPal…

    Comment by Some.Net(Guy) | March 31, 2009 | Reply

  33. Hi,

    I have a page where i have single buy now asp button, but have five products on that page.
    Depending on which product the user has selected , i want to pass on that information to paypal,
    how do i do that?
    Pls mail me the integration code of how i can send the specific product detail on single asp.net buy now button.
    Let me know.

    Comment by Vishnu Nath | April 22, 2009 | Reply

  34. Vishnu –

    Because PayPal is POST based, without reloading the page when a user makes a selection to remake your hidden inputs, there really is no way. What you could do though is put your items and PayPal buttons in separate UpdatePanels and when the user makes a selection, it refreshes the UpdatePanel of your PayPal button with the correct inputs.

    Good luck!

    Comment by Some.Net(Guy) | April 22, 2009 | Reply

  35. […] page. Fortunately there was many articles written about this topic and I found very useful article How to Integrate Both Google Checkout and PayPal In 3 Steps which offers simple solution using Button.PostBackUrl Property which is new in .NET 2.0. These guys […]

    Pingback by ernestovo zákoutí : PayPal library | June 15, 2009 | Reply

  36. Your using an image control with a postback url attribute. But basically you are posting the entire form to paypal or google or ?. This is a bit crazy. This is a huge security risk and would not be acceptable in a business. What hidden fields are in the form, who else is getting free private information.

    Comment by wiglebot | June 15, 2009 | Reply

  37. You are correct, I am posting a form to PayPal and Google. What security risk are you concerned about? Someone may know what items you’re buying? It’s not like you’re transmitting credit card info… all that is done on PayPal’s and Google’s servers. If there’s a security risk in transmitting info to PayPal and Google, you should talk to them about how they receive info, but for the 3 years that we’ve been running this system (at a BUSINESS! GASP) we’ve never had an identity theft problem.

    Comment by Some.Net(Guy) | June 15, 2009 | Reply

  38. i want to integrate google checkout in my website. now before running the site live i want to test it using sandbox feature.so i have used the post back url postbackurl=”https://sandbox.google.com/cws/v2/Merchant/923008103544766/checkoutForm” /> but when i click the button it generates an error-the page u have requested is not available..can u pls mali me the code snippet to test my site

    Comment by sumit | December 12, 2009 | Reply

  39. Hi,

    i’m very new to google checkout. i placed google checkout button on the webpage & placed some sample code as given in Google Checkout Sample Code for .NET. and i have setup code within in web.config file. but when click on gcheckout button the page just reloads but is never redirected to gcheckout. here i haven’t setup postbackurl property for the gcheckoutbutton. but when i setup postbackurl property i got an error saying “Oops!
    Application has sent Google a shopping cart with errors in it. We’ll contact them to ask that they fix this problem. As this could be a temporary issue, you can go back in your browser to try checking out again.”
    Here should i mention postbackurl or not.

    i had read the entire google artcle how to integrate gcheckout with .net, hope i didn’t understand it clearly
    plz explain me wt are the steps need to be taken to integrate gcheckout to .net

    thanking you in advance

    Comment by k sravanthi | December 30, 2009 | Reply

    • K –

      The fact that you’re getting to Google Checkout (GCO) when you’re using the PostBackUrl property means that it’s working! What’s not working is your form elements. You’re missing something that GCO requires. Make sure your form elements are in the correct format (and try a program like Fiddler to make sure your POST elements are correct). Let me know what happens!

      Good luck!

      Comment by Some.Net(Guy) | December 30, 2009 | Reply

  40. I don’t know if this works for Google checkout as I am implementing an eCommerce site using just paypal with the paypal shopping cart but I think I have found what seems to be a better solution in my opinion. You can simply add the paypal variables as arguments to the end of the URL in a link. This means you aren’t posting the whole page to Paypal, just the variables needed. Also, you can have as many Paypal buttons on a page as you want that all behave independently since you don’t need to use inputs or forms for any of them. I’m using this on my dynamic product pages pulled from a database (using a DAL & BLL) inside a FormView so it’s getting the item_name, item_number, & amount from the database. I’ve got a link to the shopping cart on the master page so it is accessible from anywhere on the site. Here is an example from my product page, WIP. Let me know what you think.

    <a style="border:0px;" href="https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_cart&business=#############&lc=US&currency_code=USD&button_subtype=products&rm=1&add=1&item_name=&item_number=&amount=” alt=”Add to Paypal Cart”>

    Comment by Ben | January 7, 2010 | Reply

    • Ben –

      I wouldn’t post it if it didn’t absolutely work! I’ve been running this system with every ecommerce site I’ve built since GCO came out and it works great! One of the benefits of the way I’m doing it (and weaknesses of your method) is that I can add as much info as I need to pass, pretty much unlimited, whereas at some point your method will reach a URL that’s just too long. Don’t get me wrong, it’s definitely viable, it’s just not the best way. But hey, whatever works for you. At least you came here and had to think a bit 🙂

      Comment by Some.Net(Guy) | January 7, 2010 | Reply

  41. does anyone perhaps no way I can get a working sample of an implementation of this (either Google or PayPal)
    without at the same time shows the source code or may keep available for download.
    any help would be appreciated – thanks.

    Comment by bespoke web development | April 21, 2010 | Reply

  42. sorry can I rephrase that (I use the station and it completely misunderstood my previous):

    does anyone perhaps know of a place where I can see a working sample of an implementation of this (either Google or PayPal)
    and at the same time see the source code behind it or where it’s been made available for download.

    any help would be appreciated – thanks.

    Comment by bespoke web development | April 21, 2010 | Reply

  43. I give up – voice dictation simply does not work well!

    Comment by bespoke web development | April 21, 2010 | Reply

    • Andy –

      I’ve implemented this on http://www.columbussupply.com. To view it, simply add an item to your cart and then view your cart. Then view the source and you’ll see all the hidden fields. Hope that helps!

      Comment by Some.Net(Guy) | April 21, 2010 | Reply

  44. Excellent post! I’ve been trying to integrate to both PayPal and DataTrans using a POST rather than a GET and that little piece of information about the PostBackUrl on a button has saved me hours. Thank you.

    Comment by Appetere Web Solutions | June 9, 2010 | Reply

  45. Use MultiView with different views to hold the hidden data for different buttons. That way you can have as many buttons on a page as you want.

    Comment by Glenn | June 12, 2010 | Reply

    • this wouldn’t work because you need all the data on the page at the same time. you don’t know what button a user is going to hit, so you need that data available for all of them.

      Comment by Some.Net(Guy) | June 14, 2010 | Reply

  46. please show me how to pay my clients using my own web application through paypal.

    Comment by lesedi | September 25, 2010 | Reply

  47. I have setup the postback url of the asp button to Paypal’s url.
    But when I click on the Asp button, the page just reloads but is never redirected to paypal.
    Any idea why?

    Comment by Lemonade Diet | August 17, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: