PayPal IPN Notes

I did finally get bored around boxing day so got back into the rails development, fortunately, with IPN now working. So here’s how I did it!

After checking through the application logfile and reading PayPal’s own IPN Notes I managed to get to the bottom of things.

Turns out my account was set so multi-currency transactions wouldn’t automatically be accepted. Consequently, transactions were being processed but recorded as “Pending” until the business user logged into PayPal. This was all as a result of me having not changed the the currency of the transaction to that of my account it was being processed through.

One other thing I noticed—the time between completing a transaction and then receiving the IPN callback was anywhere between 1 and 7 minutes – highlighting the importance of mocks/stubs I guess :)

Before we go any further, it’s important to note that for PayPal to work you must have a publicly accesible test machine – PayPals test servers attempt the callback using the URL you give – if you use localhost or some other private/firewalled machine it won’t work.

So, here’s how to get things started:

1. Signup for a PayPal sandbox account at PayPal’s Developer site.
2. Login to the PayPal sandbox, and create 2 accounts:
1. User account – this is for the ‘customer’
2. Business account – this is for the shop
3. Use the email facility in the sandbox to click on the 2 verification links sent for the 2 previous accounts.
4. Login to the business account through PayPal’s sandbox. Then go to your profile.
1. Enable IPN notifications – in the Selling Preferences column, click the “IPN Preferences” link. Enter the details and save.
5. Install the PayPal gem from RubyForge by doing a gem install paypal (along with all dependencies).
6. Add a PayPal IPN method to your controller as per the PayPal gem docs.
7. Knock together a simple order form, again you can use the docs from the PayPal gem. Make sure you set :only_path => false. Also make sure you set the currency of the transaction when using the paypal\_setup helper:

<%= paypal\_setup @order.id, Money.new(@cart.total_price \* 100, 'GBP')
For some reason the Money class wrongly interprets the “15.00” literal to “0.15”. This meant that inside my IPN processing method it was always a failure since we check they paid the full amount.

Hopefully this ought to be enough to get you up and running!

In short it’s been a pretty fun couple of days getting things working, I’ve had to dabble with some other interesting areas such as confirmation mailing and custom routing which I’ll be sure to cover in some upcoming posts. Then all I have to get working is the Flash integration!