Having put together a couple websites (Redstone Studios and Althea) that integrate with Paypal for providing checkout capabilities, I was very interested to look at Google Checkout when it was released this past week. My experience with the Paypal integration was not so good - while I eventually got it to work, it took a ton of experimentation, searching for documentation, and guessing why things weren't working (i.e. no meaningful error messages). Luckily, Google Checkout doesn't seem to suffer from these problems - I had it up and running in no time and it seems very solid for what it does. What does it do? Well, it's basically a checkout system where you can pass in a cart, users can pay by credit card, and then the money gets deposited in your bank account. Google charges 2% of the transaction amount plus twenty cents. The two percent covers the percent Google has to pay the credit card companies, and the twenty cents is some extra cash for Google. If you use Google Adwords, you get a free credit on Google Checkout - for every 1 dollar spent on Adwords you get 10 dollars of sales with no costs.
Google provides sample PHP code for getting the integration going, but unfortunately the code is PHP4 only, and it is ugly ugly ugly code. I decided to put together a simple class that allows simple integration with Google Checkout. The class is PHP5 only because it uses PHP5's DOM extension.
To use Google Checkout you need to create a Google Checkout account. From there, you can get your merchant id and merchant key, both of which are needed to communicate with Google Checkout. The Google Checkout API works by POSTING XML over HTTP. While Google's API is very flexible, I've chosen to implement a subset that I think would be useful for many people. Also, I've only implemented this class to support the creation of the cart and submission to Google, I haven't included any functionality for greater integration. This greater integration would involve creating web services that Google could call to update the website on changes to the order status, shipping, etc. The way I've set this up, all of that is skipped, and store owner would receive orders via email or Google's Checkout interface.
Limitations:
- No support for cart expiration
- No support for merchant calculations for tax/shipping/gift certificates as these require HTTPS and I don't have it and don't care to use it
- No support for requesting the buyers phone number because I didn't notice that field until just now!
- No support for shipping restrictions - i.e. shipping option 1 is only available in these zip codes/states/etc
- No support for complicated tax rules. Tax can be setup on a state by state basis. One alternative tax rule 'taxfree' is setup to allow certain items to be tax free. No support for other alternative tax rules.
- Currently doesn't support customization of the form and checkout image
In order to work around some of these limitations, I've provided a method called setDefaultXML which allows the developer to specify a default XML file to be used. The class will then fill in the items, do the encryption, and generate the form and checkout image. This allows much greater flexibility and support for all of Google's options, when needed.
Here's a simple example of how I might use it:
-
require('GoogleCheckout.php');
-
-
$merchant_id = 'XXXXXXXXXXXXXXX';
-
$merchant_key = 'XXXXXXXXXXXXXXXXXXXXXX';
-
-
$google = new Gregphoto_Service_Google_Checkout($merchant_id,$merchant_key);
-
$google->setMode('sandbox');
-
$google->addItem('Doolittle','Doolitle CD by The Pixies (1989)','11.98',1);
-
$google->addItem('PSCREEN18','18 inch Pizza Screen','7.99',3);
-
$google->addItem('MYWIDGET01','New tax free widget - Student Use Only','149.21',1,'','taxfree');
-
$google->addItem('NDISC01','New Customer Discount','-10',1);
-
$google->editCartUrl = 'http://www.gregphoto.net/index.php?action=editcart&source=google';
-
$google->continueShoppingUrl = 'http://www.gregphoto.net';
-
$google->setShipping('UPS Ground',19.99);
-
$google->setShipping('UPS Next Day',27.99);
-
$google->setShipping('Instore Pickup',4.99,'pickup');
-
$google->setStateTax('CA',.0875);
-
$google->setStateTax('IL',.0525);
-
-
$google->printXML();
Now, for what's going on:
- Line 1 includes the class
- Lines 3 and 4 setup variables for the merchant id and merchant key
- Line 6 instantiates the class, passing in the merchant id and key
- Line 7 sets the class to use the Google Checkout Sandbox - this is a testing sandbox where you can setup fake accounts to use while creating your site. Paypal offerers a much richer sandbox functionality.
- Lines 8-11 add new line items to the cart, setting there name, description, price, quantity, notes, and special tax rules
- Lines 12 and 13 setup the URLs the user should be sent to if the choose to edit the cart or continue shopping
- Lines 14-16 setup various shipping options including their prices
- Lines 17 and 18 setup taxes for the users in specific states. Google Checkout supports much richer rules around taxation, but I kept it simple on a state-by-state level.
- Line 20 outputs the HTML form and checkout image that will send the cart to Google Checkout and bring the user to the Google Checkout page
- Line 21 shows the XML cart, for debugging purposes
View this example running on my site
View the API of Gregphoto_Service_Google_Checkout
Download the class and API docs
View Google's Develper Guide for Google Checkout
July 14th, 2006 at 1:00 pm
Would you be interested in helping my integrate google checkout into a freelance job site that I am working on?
July 14th, 2006 at 1:08 pm
Kyle - unfortunately I don’t have the time…if the integration is on a PHP5 system, you’re welcome to use my code…if it is on a php4 system, you can go ahead and use the code provided by Google. Good Luck!
August 9th, 2006 at 4:43 pm
I was hired by my aunt to build her website for her and I need a payment processing solution, so I found GoogleCheckout at the perfect time. (I was just about to try to link the website with Verisign)…
That’s when I found your code for this process. I am still in school, and literally 3 months ago, I had ZERO experience in PHP or MySQL. I was still able to use your class and in about 15 minutes had GoogleCheckout ready to go on the site.
It’s still in the sandbox mode but in the next few days the site should actually be live.
Thank you very much for the work you’ve done on this class, it was the first time I ever used someone else’s object code in order to accomplish something!
August 9th, 2006 at 4:47 pm
@John - glad you were able to get it working and good luck with the site…I haven’t actually tried this in production - I’ve only used the sandbox, but I assume it should work just the same…
August 11th, 2006 at 7:21 pm
Your class is cool. Nice clean code, easy to integrate. Thank you.
September 27th, 2006 at 1:26 am
I’m pretty new to this level of PHP and I was wondering if this kind of solution would be effective for developing an ecommerce solution integrated totally into the layout of my existing site.
Does the google checkout API’s and the code you’ve offered allow me to keep the user on my site the whole time? Is that even possible?
Thanks
September 27th, 2006 at 8:24 am
@Gian - as far as I am aware….it is not possible to keep the user on your site completely - at some point they need to come go to Google’s checkout site. Eventually, however, you are returned back to the page of your choice…
October 6th, 2006 at 7:11 am
I made some (what I hope are enhancements) to this code. Please feel free to include in your code if you like.
I added merchant-private-data (I send my php session id)
I added shipping-restrictions (both allowed and excluded)
when you call addItem, I use your options array.
$options[’allowed-areas’]
$options[’excluded-areas’]
example
$options[’allowed-areas’][’zips’][] = ‘12345′ => will allow this zip code
$options[’excluded-areas’][’states’][] = ‘HI’; => will exclude this state
http://www.bowlingball.com/diffs/GoogleCheckout.diff
October 6th, 2006 at 7:13 am
I’m sorry, I forgot to mention how to use the above
$google = new Gregphoto_Service_Google_Checkout($merchant_id,$merchant_key,PUT-PRIVATE-DATA-HERE);
$google->setShipping($DESCRIPTION, $AMOUNT, ‘flat’, $options); # $options is the array from above
http://www.bowlingball.com
October 31st, 2006 at 11:28 pm
Great post….
I Would like know if its possible to sell articles or charge for postings on a website using Google Checkout API? If so, then which steps from the code should be skipped….
Thanks..
November 7th, 2006 at 4:30 am
Hi Greg,
I found your sample code via google search. Becuase i am using php 5 with google checkout payment process. Thanks for your coding. I have some clarification about the refund process? are you able to answer my question?
My question is :
Is it possible to refund to an order as partial (i.e total order amount is $20, but i want to refund only $10).
Are you handle process this in your coding?
Great work!!. Thanks a lot.