Home arrow Ruby-on-Rails arrow Page 2 - Checkout and Order Processing: the Administrative Side
RUBY-ON-RAILS

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

Checkout and Order Processing: the Administrative Side - Using the Payment Gem
(Page 2 of 4 )

The Payment gem (developed by Lucas Carlson) is an alternative to Active Merchant that can also be used to integrate with Authorize.Net. It is a bit easier to use than Active Merchant, but it doesn’t support multiple gateways. The project is also not being as actively developed as Active Merchant; the last release was in June 2005.

Downloading and installing the Payment gem is easy. Simply execute the following command:

$ sudo gem install payment

Before you can use the Payment gem, you need to create a configuration file that holds the login name and password to Authorize.Net. Save the following inconfig/payment.yml:

username: <Your login>
transaction_key: <Your password>

Next, add therequireline to theOrdermodel (app/models/order.rb):

  require 'payment/authorize_net'
  include ActiveMerchant::Billing 

  class Order < ActiveRecord::Base

Then change theprocessmethod as follows:

  def process
    begin
     
process_with_payment_gem
   
rescue => e
      logger.error("Order #{id} failed with error message #{e}")
      self.error_message = 'Error while processing order'
      self.status = 'failed'
   
end
    save!
    self.status == 'processed'
  end

We changed only one line, which allows us to easily switch back to use Active Merchant. As usual, the code for theprocess_with_payment_gemmethod, shown in Listing 9-4, should be added toapp/models/order.rb.

Listing 9-4. The process_with_payment_gem Method

def process_with_payment_gem
 
transaction = Payment::AuthorizeNet.new(
    :prefs       => "#{RAILS_ROOT}/config/payment.yml",
    :login => 'your login',
    :password => 'your password',
    :url => 'https://test.authorize.net/gateway/transact.dll',
    :amount => total,
    :card_number => card_number,
    :expiration  => "#{card_expiration_month}/#{card_expiration_year}",
    :first_name  => ship_to_first_name,
    :last_name   => ship_to_last_name,
    :ship_to_last_name => ship_to_last_name,
    :ship_to_first_name => ship_to_first_name,
    :ship_to_address => ship_to_address, :ship_to_city => ship_to_city,
    :ship_to_zip => ship_to_postal_code,
    :ship_to_country => ship_to_country,
    :customer_ip => customer_ip,
    :invoice_num => id
   )
 
begin
    transaction.submit
    logger.debug(
      "Card processed successfully.
       
Response codes:
          authorization: #{transaction.authorization}
          result code: #{transaction.result_code}
          avs code: #{transaction.avs_code}
          transaction id: #{transaction.transaction_id}
          md5: #{transaction.md5}
          cvv2 response: #{transaction.cvv2_response}
          cavv response: #{transaction.cavv_response}"
  
)
   self.status = 'processed'
 rescue => e
       self.error_message = transaction.error_message
       self.status = 'failed'
     end
  end

Note that we are specifying the test URL that we received from Authorize.Net here.

We now have all code in place, so perform a manual test by shopping for some books and placing the order on the checkout page. The result should be the same as with Active Merchant: you should see the order on the Authorize.Net Unsettled Transactions 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