Home arrow Ruby-on-Rails arrow Checkout and Order Processing: the Administrative Side

Checkout and Order Processing: the Administrative Side

In this fourth part of a five-part series that shows you how to add checkout and order processing capabilities to an ecommerce application based on Ruby-on-Rails, you'll learn how to integrate a PayPal alternative, build the administrative user interface, 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 / 5
July 16, 2010
  1. · Checkout and Order Processing: the Administrative Side
  2. · Using the Payment Gem
  3. · Implementing the Administrator User Stories
  4. · Implementing the View Order User Story

print this article

Checkout and Order Processing: the Administrative Side
(Page 1 of 4 )

 Integrating with Authorize.Net

Authorize.Net is an alternative to PayPal. To be able to test Authorize.Net, you’ll first need to apply for a test account. This account works in the same way as a real account, except that no one is billed. This means you can play around with the code, without the fear of losing money.

To apply for a test account, go to thehttp://developer.authorize.net/testaccount/page and fill out the form. You should receive an e-mail message with the test account information.

Note  For more information about the Authorize.Net payment integration API that these libraries implement, refer to the Advanced Integration Method (AIM) documentation athttp://www.authorize.net/ support/AIM_guide.pdf.

We are now ready to use Active Merchant for billing the customer. The first step is to include Active Merchant in theOrdermodel, by adding the following code to the first line inapp/models/order.rb:

include ActiveMerchant::Billing

The code for the Active Merchant version of theprocessmethod (process_with_active_merchant) is shown in Listing 9-3. Add it toapp/models/order.rb.

Listing 9-3. The process_with_active_merchant Method

  def process_with_active_merchant
creditcard = ActiveMerchant::Billing::CreditCard.new({
      :type => card_type,
      :number => card_number,
      :month => card_expiration_month,
      :year => card_expiration_year,
      :first_name => ship_to_first_name,
      :last_name => ship_to_last_name

    if creditcard.valid?
gateway = AuthorizedNetGateway.new({
        :login => "your login",
        :password => "your password"
options = {
        :card_code => card_verification_value,
        :name => ship_to_first_name + " " + ship_to_last_name,
        :address => ship_to_address,
        :city => ship_to_city,
        :zip => ship_to_postal_code,
        :country => ship_to_country,

        :email => email,
        :phone => phone_number,
        :customer_ip => customer_ip
      response = gateway.purchase(total, creditcard, options)

      if response.success?
        self.status = 'processed'
        self.status = 'failed'
        self.error_message = response.message
      self.status = 'failed'
      self.error_message = 'Invalid credit card'

We first create a credit card object and check that it is valid, using the Active Merchant API. Then we call thepurchasemethod on the gateway, passing in the total amount of the order, the credit card information, and the options, including contact information and shipping address.

Lastly, we check the response from the gateway, and set the status toprocessed. If the gateway returns an error, we set the status tofailedand store the error message in theerror_messagefield.

To see if it works, add some books to the shopping cart and check out. Then go tohttps://test.authorize.net/and log in with the credentials you received from Authorize.Net. You should see the transaction listed on the Unsettled Transactions page, as shown in Figure 9-11.

Figure 9-11.  The Authorize.Net Unsettled Transactions page

Click the transaction ID shown in the first column on the left side of the Unsettled Transactions page. You will see the Transaction Detail page, as shown in Figure 9-12. On this page, you should see the contact information and shipping information that the user entered on your site.

Figure 9-12.  Authorize.Net Transaction Detail page

Later, when the funds are transferred by the financial institution from the customer to your bank account, you can find the transaction listed on the Authorize.Net Transactions page.

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