Google Checkout API Tutorial for Perl - Part 1

The Google Checkout API is a very powerful tool for integrating an ordering system into an e-commerce website. The advantages it offers sellers are:

  • Great integration with AdWords - plus get a big Google Checkout icon next to your ad!
  • Fast, secure online checkout with no setup fees.
  • Free processing of transactions until 2008.
  • Free protection from fraud.
  • It’s not PayPal*.

* You may/may not think this is an advantage…

However, Google’s documentation doesn’t necessarily make it easy for someone with limited knowledge of PHP/ASP/Perl, etc to integrate the checkout using their “Level 2 integration” via their XML API, which allows you to integrate GCO with your own order processing system.

Part 1 will show you how to get a basic testing setup of Google Checkout running on your website using Perl.
Part 2 will show how to set up the Callback URL so that you receive notifications every time a new order is placed on your site.
Part 3 will explore in depth how to integrate these steps with your MySQL database.
Part 4 will pull it all together and show how to integrate this into a fully functioning website and create a basic back-end administration and order processing system.

This tutorial assumes that you have at least a basic knowledge of Perl, MySQL, and a server that is running Perl 5.8.8.

* Before continuing with this tutorial, I recommend that you print off and at least scan through the Google Checkout XML API documentation. It’s not the most user-friendly thing to read, but it is an essential reference point. For Perl development, you should also read the POD for the GCO modules.

Step 1

Sign up for a Sandbox Merchant Account

Go to:

http://sandbox.google.com/checkout/sell/

and click ‘Sign up now’ to register a sandbox testing account. You don’t have to enter 100% accurate information about your business here - this account is only for testing purposes, and you’ll want to create another one for your live/production site.

When you have created your sandbox account, return to the URL above, and sign in using your new username and password.

Click “Settings” > “Integration”, and note down your sandbox Google merchant ID and your Google merchant key. As per Google’s instructions, never share your merchant ID or key with anyone outside your organization.

Step 2

Google have supplied some fantastically useful Perl modules for interfacing with the Google Checkout API. You should download the latest distribution of these from the URL below before continuing:

http://search.cpan.org/dist/Google-Checkout/

Step 3

When you have downloaded and extracted the files from the Google-Checkout-x.x.x.tar.gz file, you will be presented with 4 folders:

  • conf - this folder contains the configuration file, which you will need to edit.
  • examples - this folder contains some perl code examples that utilise some of the perl modules included in the lib folder.
  • lib - this folder contains the all-important perl modules.
  • t - you can ignore this folder.

It is worth browsing around the ‘examples’ folder to familiarise yourself with the sample code - however, none of it works “out of the box”, so don’t upload these files and expect them to work ;-)

Editing the conf file:

Open the ‘GCOSystemGlobal.conf’ file in any text editor, then follow the steps below:

  1. Replace XXX with your merchant ID you noted down in step 1:MERCHANT_ID = XXX
  2. Replace XXX with your merchant key you noted down in step 1:MERCHANT_KEY = XXX
  3. The BASE_GCO_SERVER value is set to the sandbox server by default, so this can be left as it is for now.
  4. If you do not use USD to accept payment, find your 3 letter ISO currency code here to get the correct code (ie GBP for pounds sterling) to change the CURRENCY_SUPPORTED value to.

Step 4

You are now ready to upload the essential files to your server. Create a folder in your base directory (ie so the files are not visible to the public), to upload your configuration file to as follows:

/home/[user]/GCO/conf/GCOSystemGlobal.conf

Where [user] is the name of your website’s user account. This ensures your merchant details are kept secure and private.

Next, upload all contents of the ‘lib’ folder to a base directory such as:

/home/[user]/lib/

Additional libraries:

You will need to make sure that the following libraries are also installed on your server, if they are not already:

Date::Manip
XML::Writer

You are now ready to begin integrating Google Checkout into your website!

Step 5

The first thing you’ll want to do in setting up your checkout is to create a basic HTML form that posts certain parameters to a perl script, which then processes them and creates a Google shopping basket for the user.

For the purposes of this example, I’ll be working from the fictional URL www.organicsem.com/checkout/

Create a new HTML file called ‘index.html’, and paste in the following code between the <body> tags:

HTML:
  1. <h4>Green Widgets</h4>
  2. <form action=“checkout.cgi” method=“post”> Name:
  3. <input id=“name” name=“name” value=“Green Widget” type=“text” /> Description:
  4. <input id=“desc” name=“desc” value=“A widget that is green.” type=“text” /> Price:
  5. <input id=“price” name=“price” value=“3.24″ type=“text” /> Quantity:
  6. <input id=“qty” name=“qty” value=“1″ type=“text” /> Item ID:
  7. <input id=“item_id” name=“item_id” value=“WDG001″ type=“text” /> Private Info:
  8. <input id=“private” name=“private” value=“USR301″ type=“text” /> <input type=“submit” /> </form>

Then upload this to your checkout folder (ie www.organicsem.com/checkout/).

Creating checkout.cgi

Next, we need to create the checkout.cgi perl script that this form posts the information to - this is the first challenging part of the process…

First, we create the shebang line, call the standard CGI library, and create an if statement so that the script only runs if it is called from the form:

PERL:
  1. #!/usr/bin/perl -wT
  2. use strict;
  3. use CGI qw(:standard);
  4. if (param()) {

Next, we declare the newly uploaded GCO modules we’re going to use:

PERL:
  1. use lib qw(/home/[user]/lib);
  2. use Google::Checkout::General::GCO;
  3. use Google::Checkout::General::MerchantItem;
  4. use Google::Checkout::General::MerchantCheckoutFlow;
  5. use Google::Checkout::General::DigitalContent;
  6. use Google::Checkout::General::ShoppingCart;
  7. use Google::Checkout::General::Util qw/is_gco_error/;

This loads all of the library files that will be needed for this script. Next, we create a new GCO object, and tell it the path of our config file:

PERL:
  1. my $gco = Google::Checkout::General::GCO->new(config_path => ‘/home/[user]/GCO/conf/GCOSystemGlobal.conf’);

We then pass the form parameters into the variables needed to create our checkout:

PERL:
  1. my $name = param(“name”);
  2. my $desc = param(“desc”);
  3. my $price = param(“price”);
  4. my $qty = param(“qty”);
  5. my $private = param(“private”);
  6. my $item_id = param(“item_id”);

Next, we can create a ‘checkout flow’ and pass certain parameters into this, such as an edit cart URL:

PERL:
  1. my $checkout_flow = Google::Checkout::General::MerchantCheckoutFlow->new(
  2. edit_cart_url         => “http://www.organicsem.com/cart/”,
  3. continue_shopping_url => “http://www.organicsem.com/continue/”
  4. );

Next we create the cart, passing in the expiration date of the cart, any private data needed, and the checkout flow object:

PERL:
  1. my $cart = Google::Checkout::General::ShoppingCart->new(
  2. expiration    => “+1 month”,
  3. private       => “Merchant private data”,
  4. checkout_flow => $checkout_flow);

We can now create the items to be bought - for the puposes of this basket, the digital product will be delivered via email (ie a serial number, download link, etc):

PERL:
  1. my $item01 = Google::Checkout::General::DigitalContent->new(
  2. name        => $name,
  3. description => $desc,
  4. price       => $price,
  5. quantity    => $qty,
  6. private     => $private,
  7. delivery_method => Google::Checkout::General::DigitalContent::EMAIL_DELIVERY,
  8. merchant_item_id => $item_id
  9. );

Then we add the item(s) to the cart:

PERL:
  1. $cart->add_item($item01);

Next we send the cart to Google, and immediately receive a ‘response’ from Google which is loaded into the $response variable:

PERL:
  1. my $response = $gco->checkout($cart);
  2. die $response if is_gco_error $response;

Provided all goes well, we will receive a URL as the response from Google, where we will send our customer. Now we create a CGI object, and send the user to this URL:

PERL:
  1. my $cgi = new CGI;
  2. print $cgi->redirect( -uri => $response );

All that’s left to do in the script now is to close the if statement that was opened near the start of the script:

PERL:
  1. }

So bringing that all together, this gives us the following script:

PERL:
  1. #!/usr/bin/perl -wT
  2. use strict;
  3. use CGI qw(:standard);
  4.  
  5. if (param()) {
  6. use lib qw(/home/[user]/lib);
  7. use Google::Checkout::General::GCO;
  8. use Google::Checkout::General::MerchantItem;
  9. use Google::Checkout::General::MerchantCheckoutFlow;
  10. use Google::Checkout::General::DigitalContent;
  11. use Google::Checkout::General::ShoppingCart;
  12. use Google::Checkout::General::Util qw/is_gco_error/;
  13.  
  14. my $gco = Google::Checkout::General::GCO->new(config_path => ‘/home/[user]/GCO/conf/GCOSystemGlobal.conf’);
  15.  
  16. my $name = param(“name”);
  17. my $desc = param(“desc”);
  18. my $price = param(“price”);
  19. $price =~ s/-//g;
  20. my $qty = param(“qty”);
  21. $qty =~ s/-//g;
  22. my $private = param(“private”);
  23. my $item_id = param(“item_id”);
  24.  
  25. my $checkout_flow = Google::Checkout::General::MerchantCheckoutFlow->new(
  26. edit_cart_url         => “http://www.organicsem.com/cart/”,
  27. continue_shopping_url => “http://www.organicsem.com/continue/”
  28. );
  29.  
  30. my $cart = Google::Checkout::General::ShoppingCart->new(
  31. expiration    => “+1 month”,
  32. private       => “Merchant private data”,
  33. checkout_flow => $checkout_flow);
  34.  
  35. my $item01 = Google::Checkout::General::DigitalContent->new(
  36. name        => $name,
  37. description => $desc,
  38. price       => $price,
  39. quantity    => $qty,
  40. private     => $private,
  41. delivery_method => Google::Checkout::General::DigitalContent::EMAIL_DELIVERY,
  42. merchant_item_id => $item_id
  43. );
  44.  
  45. $cart->add_item($item01);
  46.  
  47. # Send the cart to google checkout and receive a destination URL
  48. my $response = $gco->checkout($cart);
  49.  
  50. die $response if is_gco_error $response;
  51.  
  52. # Redirect the user to their checkout page
  53. my $cgi = new CGI;
  54. print $cgi->redirect( -uri => $response );
  55. }

You can now upload your checkout.cgi file to your web server (remember to set permissions to 755 for checkout.cgi), in the same directory as your form.

You should now have a fully working testing setup for Google Checkout! I suggest you experiment with sending different items and parameters to Google Checkout from the form page to test that each parameter is working properly.

In order to test a full purchase cycle, it’s worth using a different Google account to buy with, in a different browser. For example, if you are using a@b.com in Firefox as your merchant account, use b@c.com in Internet Explorer to test purchasing items. This will avoid confusing seller and buyer accounts when testing your setup.

You can find test credit card numbers for Google Checkout’s Sandbox here:

http://code.google.com/apis/checkout/developer/index.html#integration_overview

!!!Don’t forget to set permissions on your checkout.cgi page to 755!!!

Stay tuned for Part 2, where we will be covering the not-so-easy issue of how to set up your ‘Callback URL’…


Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • bodytext
  • Sphinn
  • del.icio.us
  • Google
  • StumbleUpon
  • Technorati
  • E-mail this story to a friend!

Tags: , , , ,

3 Responses to “Google Checkout API Tutorial for Perl - Part 1”

  1. Rune Says:

    Is it possible to do subscription management on the google checkout?
    i.e. a recurring monthly fee on someones credit card like it is with Paypal?
    Is it possible to top up when their balance or credit on something is running low?

  2. John Feher Says:

    Great tutorial! This script is exactly what I am trying to do.

    I am unable to get a response that I can read. Firefox wants to open a file
    associated with cgi. When I check in notepad, I get unreadable text.

    IE displays an HTTP 500 server error.

    The sample API sample files run from the command line, and generate a readble url, but when I put them on the server as cgi they produce the same problem.

    I know I am missing something, but can’t quite figure it out. Any advice
    would be appreciated.

    John
    http://www.photobooks.us/checkout/test.htm

    I’m running activestate perl 5.8.8 and apache 2.2.4 win32

    I’ve already solved the date-manip issue with adding TZ to the environment variables.

  3. rob Says:

    I’d try a couple of things:

    1. Check that your server is set up to run CGI files outside of the /cgi-bin/ folder
    2. Check that the permissions for the CGI script are set to 755
    3. Try using the CGI::Carp module by adding the line ‘use CGI::Carp qw(fatalsToBrowser)’ near the top of your script - this should output most fatal errors to your browser

    Hope that helps!

Leave a Reply