Posterous theme by Cory Watilo

PayPal IPN using Rails 3.1 and Active Merchant

Instant Payment Notification (IPN) is PayPal's message service that sends a notification when a transaction takes place. When IPN is enabled in a seller's account,  the sellergetThere are bunch of tutorials online using Active Merchant gem for implementing IPN. However, most of these tutorials need some modification to be up and running. I created an IPN example using Rails 3.1, hosted on Heroku. Feel free to download the code and integrate it in your rails applications.

The heroku demo link is http://ipnexample1.herokuapp.com 

 

There have been some issues with getting the active_merchant gem to work with Rails 3.1. Please install active_merchant as a plugin. You will find the instructions here (https://github.com/Shopify/active_merchant). The biggest gotcha, that took me a while to figure out is that, this plugin depends on active_utils from Shopify. You need to include that in your gem file. This will save you a few headaches.

 

Once this is done, initialize active merchant in config/intializers. When you set ActiveMerchant::Billing:: Base.mode = :test, you are ensuring that you will be using sandbox version for testing.

 

Active merchant provides a great helper to generate a PayPal submit button. To handle, IPN, you need to do two things - First specify the action in the view that needs to get triggered when notification happens. Enable this in your PayPal seller account (sandbox in this case). This setting is actually hidden under Profile/more options/IPN settings. Note that  if you are using localhost, it is hard to test the IPN as PayPal cannot send IPN to localhost or any flavor of that (lvh.me, smakckaho.st, etc). In the notify action in your controller, you can handle the necessary backend operations like saving to the database, etc.

 

Also, when you create a pre-configured sandbox account, create a pro account if you want to test IPN. I found that even using a seller account does not send me IPN responses. I needed a pro account to receive notifications. This might be a sandbox issue, I am not sure. 

 

You can download the entire code from my git repo - https://github.com/saranyan/PayPal-Code-Examples