Home arrow Ruby-on-Rails arrow Order and Payment Handling for an Ecommerce Website

Order and Payment Handling for an Ecommerce Website

In this second part of a five-part series on building the checkout and order processing parts of an ecommerce application in Ruby on Rails, you'll learn how to save order information, integrate with payment gateways, and more. 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 / 9
July 09, 2010
  1. · Order and Payment Handling for an Ecommerce Website
  2. · Creating the View
  3. · Saving the Order Information
  4. · Integrating with Payment Gateways

print this article

Order and Payment Handling for an Ecommerce Website
(Page 1 of 4 )

Creating the Controller and Integration Test

The next task is to create the controller for the Checkout user story. In a console, type the following command:

$ script/generate controller Checkout index place_order thank_you

  exists  app/controllers/
  exists  app/helpers/
  create  app/views/checkout
  exists  test/functional/
  create app/controllers/checkout_controller.rb
  create test/functional/checkout_controller_test.rb 
  create  app/helpers/checkout_helper.rb
  create  app/views/checkout/index.rhtml
  create app/views/checkout/place_order.rhtml
  create  app/views/checkout/thank_you.rhtml

Now, let’s continue by using TDD to implement the user story. We want to test two scenarios: one where the cart is empty and one where the cart contains books. We are going to call multiple controllers (theCartandCheckoutcontrollers) from our tests, so we’ll use an integration test instead of a functional test. Create the test with the following command:

$ script/generate integration checkout

  exists  test/integration/
  create  test/integration/checkout_test.rb

Let’s start with the first scenario. When the cart is empty, the user shouldn’t be able to perform a checkout. This can be verified by replacing thetest_truthmethod intest/integration/ checkout_test.rbwith the code shown here:

  def test_that_empty_cart_shows_error_message
get '/checkout'
assert_response :redirect
assert_redirected_to :controller => "catalog"
assert_equal "Your shopping cart is empty!➥
Please add something to it before proceeding to checkout.", flash[:notice]

This test verifies that when the cart is empty, we are redirected to the catalog page and an error message is displayed.

If you run the test now, it fails, because we haven’t implemented the controller yet. Openapp/controllers/checkout_controller.rbin your editor and change theindexmethod as follows:

  def index
@order = Order.new
@page_title = "Checkout"
if @cart.books.empty?
flash[:notice] = "Your shopping cart is empty!➥
Please add something to it before proceeding to checkout."
      redirect_to :controller => 'catalog' 

Also add theinitialize_cartfilter to the controller:

class CheckoutController < ApplicationController
before_filter :initialize_cart

  def index

Recall that this filter was implemented in Chapter 5. It initializes the cart, so that we can access it from theCheckoutcontroller and views. It also enables us to show the shopping cart to the right on the page (also implemented in Chapter 5).

Execute the test with the following command:

$ ruby test/integration/checkout_test.rb

You should see no errors. Perform a manual test by openinghttp://localhost:3000/checkout. Because the shopping cart is empty, you should be redirected to the catalog page, where the error message in Figure 9-1 is displayed.

Figure 9-1.   The catalog page displaying an empty cart error message

Next, we’ll add a test for the second scenario. When the shopping cart contains one or more items (books), we want to display a form with three sections: contact information, shipping address, and billing information. First, add the following fixtures and test totest/integration/checkout_test.rb:

  fixtures :authors, :publishers, :books

  def test_that_placing_an_order_works
    post '/cart/add', :id => 1
    get '/checkout'
    assert_response :success
    assert_tag :tag => 'legend', :content => 'Contact Information'
    assert_tag :tag => 'legend', :content => 'Shipping Address'
    assert_tag :tag => 'legend', :content => 'Billing Information'

The test begins by adding a book (defined in thebooksfixture file) to the cart by calling the/cart/addaction. Then it accesses the checkout page and verifies that the request is successful. It proceeds by checking that the page contains the three required sections. This is done by looking for threelegendtags having the following content:Contact Information,Shipping Address, andBilling Information.

Now that we have the new test in place, we could try to run it, but it would fail miserably when it tries to find the three sections.

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