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).
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/).
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: