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.
-
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. -
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 theactionstring from your form. For instance, PayPal’sPostBackURLvalue is “https://www.paypal.com/us/cgi-bin/webscr”. Google’s will be “https://checkout.google.com/cws/v2/Merchant/[your 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" /> -
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).
-
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:buttonelement has an attribute calledOnClientClick. 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
RSS Feed
[...] – 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 |
Thanks – worked a treat!
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
Granada –
Your code didn’t post. Try again or email it to me at fixmysite.please[at]gmail.com
Hi Please Send me full details of integration of paypal also asp.net c# source code
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.
[...] 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 |
[...] 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 |
this was really helpful…thanks a lot….
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!
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!
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.
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.
i want integrate paypal for getting payment in my website.but i dontknow how to write code for this.can anyone help me?
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.
Thanks, Sue! Glad I could help
Simple and very nice.
Thanks a lot.
Hi,
Thanks a lot..its really a nice example.
Cheers!!!
Ujjwal B Soni
+919998971048
Thank you, this was incredibly helpful! I could never have accomplished this without your clear instructions.
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
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.
Thanks – this allowed me to move from a “nested” form approach to a cleaner solution by changing just a couple lines of code.
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.
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.
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.
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!!
Darn!! My prior comment should have said that the <asp:ImageButton tag is the way to go.
very useful!
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!
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
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!
Thanks, that is what I have done. I was hoping for some elegant solution, but I couldn’t find one.
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…
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.
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!
[...] 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 |
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.
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.