Home arrow Ruby-on-Rails arrow Page 2 - Checkout and Order Processing: the User Side

Checkout and Order Processing: the User Side

In this conclusion to a five-part article series on adding checkout and order processing capabilities to an ecommerce website with Ruby on Rails, you will learn how to add the close order user story, which involves adding the ability to calculate shipping costs and other details. This article is excerpted from chapter nine of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
July 22, 2010
  1. · Checkout and Order Processing: the User Side
  2. · Calculating Shipping Costs and Taxes
  3. · Calculating Taxes

print this article

Checkout and Order Processing: the User Side - Calculating Shipping Costs and Taxes
(Page 2 of 3 )

For calculating Federal Express (FedEx) and United Parcel Service (UPS) shipping costs, you can use the handy Shipping RubyGem (http://shipping.rubyforge.org/).  

Using the Shipping Gem

Youíll first need to register an account at FedEx (http://www.fedex.com) and UPS (http://www.ups.com).  

Next, install the Shipping gem by executing this command:

$ sudo gem install shipping --include-dependencies

Then save your account information inconfig/shipping.yml:

  fedex_url: https://gatewaybeta.fedex.com/GatewayDC
  fedex_account: <your FedEx account number>
fedex_meter: <your FedEx meter number>

  ups_account: <your UPS account number>
ups_user: <your UPS username>
ups_password: <your UPS password>

Note  You can obtain thefedex_meternumber by first registering for a FedEx account, and then calling theShipping::FedEx.registermethod with the following parameters:name,company,phone,email,address,city,state,zip,fedex_account, andfedex_url. You need to register to both the live and test servers. The URL to the live environment ishttps://gateway.fedex.com/GatewayDC.

Now letís test how much it costs to send an order weighing 2.0 pounds from Emporium (located in New York) to Durham, North Carolina. We chose UPS because itís easier to use than FedEx, which requires that we apply for a meter number. Save the code shown here intest/unit/test_shipping.rb:

require File.dirname(__FILE__) + '/../test_helper'
class PaymentTest < Test::Unit::TestCase
  def test_ups_shipping
params = {
      :zip => 27712,
      :state => "North Carolina",
      :sender_zip => 10001,
      :sender_state => "New York",
      :weight => 2,
      :prefs => '../../config/shipping.yml'
    ship = Shipping::UPS.new params
    assert ship.price > 5
    puts ship.price

Run the test by executing the following command:

$ ruby test/unit/test_shipping_rb

The output when we ran it was $8.59 (your result might be different).

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials