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).
Order and Payment Handling for an Ecommerce Website - Integrating with Payment Gateways (Page 4 of 4 )
You can think of a payment gateway as a kind of proxy that handles the tricky details of credit card transactions for merchants. You send the payment details to the gateway, and the gateway forwards the details to the financial institution. The payment is then processed (with a possible delay) by the financial institution, after which the result is communicated back to the gateway, and then to you or your application.
We’ll first show you how to use Active Merchant, a payment abstraction library, to integrate with PayPal, the leading online payment solution that enables merchants to manage credit card transactions online. Later, we’ll also demonstrate how to integrate with Authorize.Net, another leading payment gateway, including how to do this with the Payment gem as an alternative to Active Merchant.
Note Only US-based businesses are allowed to use PayPal’s Website Payments Pro and Authorize.Net. For alternative payment gateways, see the Active Merchant homepage athttp://home.leetsoft.com/am. And remember, using payment gateways is not the only option. Some banks allow you to add a simple form to your site, which the customer can use for transferring money directly to your bank account.
Installing the Active Merchant Plugin
Active Merchant (http://home.leetsoft.com/am) is a Rails plugin, written by Tobias Lütke and various contributors, that allows you to integrate with various payment gateways, including: Moneris, Authorize.Net, TrustCommerce, PsiGate, and PayPal’s Website Payments Pro. It’s currently used by Shopify (http://shopify.com) in production.
The command checks out the latest version of the plugin and places it in thevendor/ plugins/active_merchantdirectory.
Active Merchant uses the SOAP protocol to communicate with PayPal. This means you’ll need to installsoap4r, an implementation of SOAP 1.1. Download the latest version fromhttp://dev.ctor.org/soap4r, extract the package to a folder of your choice, and then execute the following command:
$ sudo ruby install.rb
Note At the time of writing, Active Merchant requiredsoap4rversion 1.5.5 or greater to work. To retrieve the latest version, executesvn checkout http://dev.ctor.org/svn/soap4r/trunk soap4r.
soap4ruses thehttp-access2library to communicate with the PayPal servers, so download the latest stable release of this library fromhttp://dev.ctor.org/http-access2/, extract the package, and install it with the following command:
$ sudo ruby install.rb
That’s it. If you have the server running, restart it for the changes to take effect.
Please check back next week for the third part of this series.
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.