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).
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
if response.success? self.status = 'processed' else self.status = 'failed' self.error_message = response.message end else self.status = 'failed' self.error_message = 'Invalid credit card' end end
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.
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.