Paypal IPN in Rails with Active Merchant
Active Merchant makes it extremely simple to use Paypal IPN. Here is a simple guide for getting IPN up and running.
Sign up for a Paypal sandbox account
Paypal provides a sandbox environment that mimics their production environment, with the exception that it doesn't actually process the transactions. This is extremely useful for development and testing. It allows you to create multiple fake accounts and generate bank accounts and credit cards. More information can be found on Paypal's Testing Instant Payment Notification page.
Unfortunately, I've signed up for two different developer accounts and I've had trouble logging in with both of them. I've tried resetting my password, but I still can't log in. Fortunately, I already have my sandbox accounts set up and don't really have a need for it (except to write this guide).
Create a Personal account and add a credit card
After you sign up for your developer account, create a personal sandbox account and add a credit card.
Create a Business account and add a checking
Next, create a business sandbox account and add a checking account.
Install the money gem
sudo gem install money
Install the Active Merchant plugin
script/plugin install http://activemerchant.googlecode.com/svn/trunk/active_merchant
Create a form that submits to Paypal
ActiveMerchant::Billing::Integrations in a controller to add Active Merchant's helpers.
In the view, use
payment\_service\_for to create a form that submits to Paypal to process the payment.
The code above refers to the constant
PAYPAL\_ACCOUNT, which I define in
environment.rb. I also set Active Merchant to use test mode, which directs it to use Paypal's sandbox:
Create an action that processes the IPN
After the above form submits to Paypal and the user makes a payment, Paypal will post data about the transaction to your server. Set up an action to receive the post:
Depending on the model for your application, this action will obviously look different. The important part is that you pass the raw post data from the request to
Paypal::Notification.new, and call
notify.acknowledge to connect back to Paypal to verify the data.
Lastly, log into the business account that you created above, go to "Instant Payment Notification Preferences" in your profile, and set the URL that Paypal should post back to after payments. (Note: this needs to be a publicly accessible URL.)