Home arrow Ruby-on-Rails arrow Page 3 - Order and Payment Handling for an Ecommerce Website
RUBY-ON-RAILS

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Order and Payment Handling for an Ecommerce Website - Saving the Order Information
(Page 3 of 4 )

We are now confident that the checkout form and the two new ActiveRecord models work as we intended, so letís continue with the next part of the checkout page implementation: the place_order action. This action is triggered when the user has filled out the form and clicks the Place Order button.  

Updating the Integration Test

First, we change the integration test (test/integration/checkout_test.rb) we created earlier as shown here:

  def test_placing_order
    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'

    post '/checkout/place_order', :order => {
      # Contact Information
      :email => 'abce@gmail.com',
      :phone_number => '3498438943843',
      # Shipping Address
      :ship_to_first_name => 'Hallon',
      :ship_to_last_name => 'Saft',
      :ship_to_address => 'Street',
      :ship_to_city => 'City', 
      :ship_to_postal_code => 'Code',
      :ship_to_country => 'Iceland',
      # Billing Information
      :card_type => 'Visa',
      :card_number => '4007000000027',
      :card_expiration_month => '1',
      :card_expiration_year => '2009',
      :card_verification_value => '333',
   
}

    assert_response :redirect
    assert_redirected_to '/checkout/thank_you'
  end

This will place an order for one book and check that the order process was successful, which is indicated by a redirect to the page where we show a thank you message to the customer.

Adding the place_order Action

Next, we add theplace_orderaction to thecheckout controller (app/controllers/ checkout_controller.rb):

  def place_order
    @page_title = "Checkout"
    @order = Order.new(params[:order])
    @order.customer_ip = request.remote_ip
    populate_order

    if @order.save
      if @order.process
        flash[:notice] = 'Your order has been submitted,➥
and will be processed immediately.'
        session[:order_id] = @order.id
        # Empty the cart
        @cart.cart_items.destroy_all
        redirect_to :action => 'thank_you'
      
else
        flash[:notice] = "Error while placing order.➥
'#{@order.error_message}'"
        render :action => 'index'
      end
    else
      render :action => 'index'
    end
  end

Theplace_orderaction calls two methods,populate_orderandorder.process, which we havenít created yet. Thepopulate_ordermethod (added incheckout_controller.rb) simply copies the books from the shopping cart to the order:

  private

  def populate_order
    for cart_item in @cart.cart_items
      order_item = OrderItem.new(
        :book_id => cart_item.book_id,
        :price => cart_item.price,
        :amount => cart_item.amount
      )
      @order.order_items << order_item
    end
  end

The following code for theprocessmethod should be added to theOrdermodel (app/models/order.rb):

  private

  def process
    result = true
    #
    # TODO Charge the customer by calling the payment gateway
    #
    self.status = 'processed'
    save!
    result
 
end

processis where we charge the customer for the total amount of the order, and this is where weíll put the payment gateway integration code.

You can run the integration test by executing the following command:

$ ruby test/integration/checkout_test.rb

Although we havenít implemented the thank you page yet, the test will pass. Openapp/views/checkout/thank_you.rhtmland add the following code to it:

For future references use invoice number <%= session[:order_id] %>

Next, change thethank_youaction in theCheckout controller (app/controllers/ checkout_controller.rb) as follows:

  def thank_you
   
@page_title = 'Thank You!'
 
end

To test the thank you page, add a couple of books to the shopping cart and click the checkout link. Then fill out the checkout form and place the order. You should now see the thank you page, as shown in Figure 9-4.


Figure 9-4.  The thank you page 


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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