Pages

Tuesday, May 19, 2015

Locally debugging Paypal's Instant Notification Payments via ngrok

I was working on a personal project and needed to accomplish some specific requirements when clients purchased an item from the website. Now, Paypal has something called Instant Payment Notification (IPN) which is a mechanism by which Paypal makes an Http Post to any url you choose, and in that post it sends you some vital information about the financial transaction (more information on Paypal's IPN here).

The problem that I was having was that I was not able to locally debug the code that I had written for the URL that would receipt Paypal's Http Post, and the reason is that in your Paypal's business account, you are not allowed to enter a "localhost" type of url. So when you go to Paypal, log in and go to Profile -> My Selling Tools -> Instant Payment Notifications, you can then specify the URL where Paypal will post the requests and this URL is not allowed to be localhost.

I found myself logging information in txt files and running the code on a real website on the internet which was tedious and time consuming, until I found ngrok! ngrok allows you to expose a URL on the internet which is really pointing to your localhost. It is a tunnel. Now, obviously your machine has to be on, and ngrok also has to be running for the whole magic to work.

So, what you do is you run ngrok via command prompt, it generates an internet URL and you use that URL in the Instant Payment Notification settings of your Paypal account. Then you can make Paypal transactions with fake money (development mode) and have it point to that URL generated by ngrok, which will invoke your localhost URL site and voilá, you'll be able to locally debug your code.

First, you run your website locally to see the URL that it auto generates:

You notice the port being used. In this case 52506.

Then you proceed to run ngrok via command prompt pointing it to port 52506 in my case:


Once you hit Enter, ngrok generates an internet URL for you:


ngrok generated http://454dcca5.ngrok.com/ (remember that just happened to be my particular case)
Now if I open a web browser and type that URL I end up seeing the exact same thing I see when I visit my local website at http://localhost:52506

From there it is just a piece of cake. You go to the Instant Payment Notificaiton settings in Paypal, and enter that URL generated by ngrok and you are done! You can start locally debugging your code with actual Http Posts generated by Paypal itself during your testing.

No comments:

Post a Comment