Home arrow Ruby-on-Rails arrow Page 4 - Online Order Processing: Using PayPal
RUBY-ON-RAILS

Online Order Processing: Using PayPal


In this third part of a five-part series on building the checkout and order processing components of an ecommerce website with Ruby-on-Rails, we'll focus on integrating PayPal into the application's capabilities. 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 / 7
July 15, 2010
TABLE OF CONTENTS:
  1. · Online Order Processing: Using PayPal
  2. · Creating a Dummy Bank Account and Credit Card
  3. · Creating API Credentials
  4. · Setting Up PayPal Transactions

print this article
SEARCH DEVARTICLES

Online Order Processing: Using PayPal - Setting Up PayPal Transactions
(Page 4 of 4 )

We can now start using the test account. First, change theprocessmethod in theOrdermodel (app/models/order.rb) as shown here:

  def process
    if closed? raise "Order is closed"
    begin
      process_with_active_merchant
   
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’ll call the payment gateway from theprocessmethod. If there’s an exception in the order-processing code, we set the order status tofailedand log the error message to the standard log. The main logic is located in theprocess_with_active_merchantmethod, which should be added toapp/models/order.rb:

  def process_with_active_merchant
    Base.gateway_mode = :test

    gateway = PaypalGateway.new(
      :login     => 'business_account_login',
      :password  => 'business_account_password',
      :cert_path => File.join(File.dirname(__FILE__), "../../config/paypal")
   
)
    gateway.connection.wiredump_dev = STDERR

    creditcard = CreditCard.new(
      :type                => card_type,
      :number              => card_number,
      :verification_value  => card_verification_value,
      :month               => card_expiration_month,
      :year                => card_expiration_year,
      :first_name          => ship_to_first_name,
      :last_name           => ship_to_last_name
   
)
   
# Buyer information
   
params = {
      :order_id => self.id,
      :email => email,
      :address => { :address1 => ship_to_address,
                   
:city => ship_to_city,
                    :country => ship_to_country,
                    :zip => ship_to_postal_code
                 
} ,
      :description => 'Books',
      :ip => customer_ip
    }

    response = gateway.purchase(total, creditcard, params)

    if response.success?
      self.status = 'processed'
    else
      self.error_message = response.message
      self.status = 'failed'
    end
  end

We are setting the gateway mode totest, because we want to use the Sandbox instead of the live environment.

We set thewiredump_devparameter to true by usinggateway.connection.wiredump_dev = STDERR. This prints out the HTTP traffic to the console, which helps debug the traffic between the server and the gateway.

Note that PayPal expects thecountryfield to contain the country code, not the country name, which is the case at the moment. We can fix this by hard-coding the list of countries and codes in the view (app/views/checkout/index.rhtml), as shown here:

  <select name="order[ship_to_country]">
    <option value="FI">Finland</option>
    <option value="NO">Norway</option>
    <option value="SE">Sweden</option>
    <option value="DK">Denmark</option>
  </select>


Note  Another option for getting the country codes is to use the TZInfo library (http://tzinfo.rubyforge.org/), as explained athttp://rails.techno-weenie.net/ tip/2006/6/5/country_select_with_country_codes.


We can now test that transactions are sent to PayPal, by adding a few books to the shopping cart and placing an order. Then log in to the PayPal Sandbox (www.sandbox.paypal.com/) with the dummy account you created earlier. You should see the order on the History page, as shown in Figure 9-9.


Figure 9-9.  The PayPal History page displaying transactions

Note that the signup and verification e-mail messages are not sent to the e-mail address you specify on the signup forms. To view the messages, go to the Email page in PayPal Developer Central, shown in Figure 9-10, which is accessible after you log in.


Figure 9-10.  The Email page displaying payment notifications 

Please check back tomorrow for the continuation of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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