Home arrow Ruby-on-Rails arrow Page 2 - Checkout and Order Processing for an Ecommerce Website

Checkout and Order Processing for an Ecommerce Website

It's vital for an ecommerce website to get the checkout and ordering process correct. This five-part article series shows you how to use Ruby-on-Rails to manage it with very little difficulty. It's 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 / 4
July 08, 2010
  1. · Checkout and Order Processing for an Ecommerce Website
  2. · Implementing the Check Out User Story
  3. · Specifying the Associations
  4. · Adding Validations to the Model

print this article

Checkout and Order Processing for an Ecommerce Website - Implementing the Check Out User Story
(Page 2 of 4 )

Back in Chapter 5, we implemented the shopping cart for Emporium customers. Now we will complete the web shopping experience by adding the crucial last step: check out, including how to integrate with credit card payment gateways.

Creating the Models

We need a place where we can store the order information. We’ll use two tables, named orders andorder_items, which are similar to thecartsandcart_itemstables we created in Chapter 5. 

You could use single-table inheritance to store both the order items and cart items in the same table, but in this case, we want to clearly separate the two entities. For more information on single-table inheritance, see the API documentation forActiveRecord::Baseathttp://rubyonrails.org/api/classes/ActiveRecord/

Creating the Order Model

Begin by creating theOrdermodel and the associated migration:

$ script/generate model Order

  exists  app/models/
  exists  test/unit/
  exists  test/fixtures/
  create  app/models/order.rb
  create  test/unit/order_test.rb
  create  test/fixtures/orders.yml
  exists  db/migrate
  create  db/migrate/011_create_orders.rb
--------------------------------------------Opendb/migrate/011_create_orders.rband replace the contents with the following code:

class CreateOrders < ActiveRecord::Migration
  def self.up
create_table :orders do |t|
      # Contact Information
      t.column :email, :string 
      t.column :phone_number, :string
      # Shipping Address 
      t.column :ship_to_first_name, :string
      t.column :ship_to_last_name, :string
      t.column :ship_to_address, :string
      t.column :ship_to_city, :string
      t.column :ship_to_postal_code, :string
      t.column :ship_to_country, :string
      # Private parts
      t.column :customer_ip, :string
      t.column :status, :string
      t.column :error_message, :string
      t.column :created_at, :timestamp
      t.column :updated_at, :timestamp

  def self.down
    drop_table :orders

Columns with names that start withship_tomap directly to the shipping information section of the form we’ll create later in this chapter. Theemail andphone_numberfields map to the contact information section of the checkout form. We also want to store private data, including the customer’s IP address, so that it is possible to track, for example, credit card frauds.

Thestatus field is a string that indicates in which of the following states the order currently is:open,processed,closed, orfailed. Theopen status is used by default.processed is the status of an order for which George has charged the customer. Orders areclosedwhen George has verified that the payment has been approved, and after he has sent the books to the customer. If we receive an error message from the payment gateway, or something else fails in the order processing, we store it in theerror_messagefield and set the status tofailed.

Tip  If you want to keep an audit trail of all the changes that have been done to an order, you can use theacts_as_versionedplugin, which can be found athttp://ar-versioned.rubyforge.org/.

As usual, we drop the table when rolling back changes.

Creating the Order_Item Model

The books that have been ordered also must be stored somewhere, which will be in theorder_itemstable. Create the model and the migration with the following command:

$ script/generate model Order_Item

   exists  app/models/
   exists  test/unit/
   exists  test/fixtures/
   create  app/models/order_item.rb
 identical test/unit/order_item_test.rb
 identical test/fixtures/order_items.yml
   exists  db/migrate
   create db/migrate/012_create_order_items.rb
--------------------------------------------Opendb/migrate/012_create_order_items.rband replace the contents with the following code:

class CreateOrderItems < ActiveRecord::Migration
  def self.up
create_table :order_items do |t|
      t.column :book_id, :integer
      t.column :order_id, :integer
      t.column :price, :float
      t.column :amount, :integer
      t.column :created_at, :timestamp
      t.column :updated_at, :timestamp

  def self.down
    drop_table :order_items

We store the price of the ordered book and the amount of books ordered. We link each order item to a book and an order.

You can now run the migrations by executing the following:

$ rake db:migrate

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials