My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Documentation  

Ruby Sample Code for Google Checkout

This project is aimed at providing basic foundation for integrating your online store with the Google Checkout API using Ruby. The project was originally hosted at Rubyforge under the name Google4R - Checkout. The source tree of the project has been migrated to here and will be maintained here from now on.

Structure

/lib
  /google4r
    /checkout
      commands.rb                <--- Module implements the order process commands
      frontend.rb                <--- Frontend to interact with Google Checkout
      merchant_calculation.rb    <--- Module implements merchant calculation 
      noitfictions.rb            <--- Module implements different notifications
      shared.rb                  <--- Objects shared among different XML messages
      utils.rb                   <--- Utilities like HTML Form API signature
      xml_generation.rb          <--- Module parses and generates XML messages
    checkout.rb
  /test
    /integration                 <--- Integration tests
    /system                      <--- System tests
    /unit                        <--- Unit tests
    /xml                         <--- Test XML files
    frontend_configuration.rb    <--- Configuration file for the module
    test_helper.rb               <--- Helper for tests
  /var
    cacert.pem
  CHANGES                        <--- Change log
  LICENSE                        <--- MIT License
  Rakefile                       <--- Ruby makefile
  README                         <--- Read me first

Installation Instructions

  1. Install gem (instruction)
  2. In a shell (with admin access), run
    • gem install money
    • gem install mocha
    • gem install google4r-checkout or gem install google4r-checkout-1.x.x if you download the gem from this site.
  3. At this point, please read the README file to complete the installation.

Sample Code

require "google4r/checkout"

class Test
  def mypost
    # Use your own merchant ID and Key, set use_sandbox to false for production
    configuration = { :merchant_id => '1234567890987654', :merchant_key => 'abc_efghijklmn_opq', :use_sandbox => true }
    @frontend = Google4R::Checkout::Frontend.new(configuration)
    @frontend.tax_table_factory = TaxTableFactory.new
    checkout_command = @frontend.create_checkout_command
    # Adding an item to shopping cart
    checkout_command.shopping_cart.create_item do |item|      
      item.name = "Dry Food Pack"
      item.description = "A pack of highly nutritious..."
      item.unit_price = Money.new(3500, "USD") # $35.00      
      item.quantity = 1
    end
    # Create a flat rate shipping method
    checkout_command.create_shipping_method(Google4R::Checkout::FlatRateShipping) do |shipping_method|
      shipping_method.name = "UPS Standard 3 Day"
      shipping_method.price = Money.new(500, "USD")
      # Restrict to ship only to California
      shipping_method.create_allowed_area(Google4R::Checkout::UsStateArea) do |area|
        area.state = "CA"
      end
    end
    response = checkout_command.send_to_google_checkout    
    puts response.redirect_url    
  end
end

class TaxTableFactory
  def effective_tax_tables_at(time)
    # Tax table 1 will be used before Apr 09 2008
    if time < Time.parse("Wed Apr 09 08:56:03 CDT 2008") then
      table1 = Google4R::Checkout::TaxTable.new(false)
      table1.name = "Default Tax Table"
      table1.create_rule do |rule|
        # Set California tax to 8%
        rule.area = Google4R::Checkout::UsStateArea.new("CA")
        rule.rate = 0.08
      end
      [ table1 ]
    else
      table2 = TaxTable.new
      # ... set rules
      [ table2 ]
    end
  end
end

t = Test.new
t.mypost
Comment by sspweb01...@gmail.com, Sep 23, 2008

sub total need to be on sspweb0120070@gmail.com

Comment by Kingston...@gmail.com, Nov 30, 2010

More than just the sample code would be nice... the API I found online is really hard to follow. I just want to know how to add more things. Had to read through the code just to understand it. Kind of a waste of time in the end which is a shame.

Comment by espe...@gmail.com, Dec 15, 2010

This example is outdated. Please remove the time-check in the TaxTable?:

If time < Time.parse("Wed Apr 09 08:56:03 CDT 2008") then


Sign in to add a comment
Powered by Google Project Hosting