Freelance Web Development in Malta


Debugging .NET and Paypal IPN on your local machine

So basically it’s been a long time since I have written a blog post since I was very busy developing which will soon be official and which will hopefully be a successful website where users will buy tickets online. Whilst I was developing this website I realized that once I thought about writing a blog post which can be very useful to developers who are developing websites which will allow users to send money through PayPal. This blog post concerns mainly the ability to debug your own code when serving PayPal IPN responses.

As a summary the outcomes of this blog post are to

  1. Understand how to debug .NET code when using PayPal IPN using normal breakpoints, which you are familiar with, using Visual Studio on your local machine.

To follow this blog post you should have these prerequisites

  1. WebMatrix (a free Microoft tool which can host websites) which will act as our IIS. This can be downloaded from here.
  2. Have access to your internal router to route port 80 traffic to your development machine
  3. Ideally have a dyndns or no-ip account
  4. Switch off Windows Firewall to ensure that traffic is not blocked to your development machine (Do not forget to turn this on again after your fix your code issues :) )

So  let me start by giving you a basic understanding of what PayPal Instant Payment Notification(IPN) is. In essense PayPal IPN is one the techniques adopted by PayPal to notify your website that a specific PayPal payment has in actual fact been effected. This can be used for several scenarios such as the one explained underneath

  1. A business has created its own website whereby users can buy products from his/her website
  2. Users buy products from this website
  3. The business has decided to use PayPal to accept payments for products bought
  4. Money is transferred from the buyer PayPal account to the business owner PayPal account when a user checks out items in his shopping cart
  5. When a user confirms a payment through PayPal after checking out items the business owner is notified and the products are then delivered to the buyer

To meet the above requirements the developer needs to create some code which will serve PayPal reponses to ensure that a payment has been affected and thus finalizing an order. This is one of the most important parts of any application since you do not wish to end up of having a user sending payments from his PayPal account, and then not receiving his orders. Thus, as a developer your need to ensure that this code works well. That is why debugging is very important to ensure that you are performing correct operations when serving PayPal IPN responses.

When using PayPal IPN it is unfortunately a nightmare when it comes to debugging your own .NET code. This is due to the fact that PayPal IPN does not send responses to your own local machine but rather to well known URLs. So let’s see how you can actually debug your code using your Visual Studio and placing normal breakpoints on your local machine to ensure that everything works as expected before publishing your own website.

The first step you should take is to host your own website using WebMatrix. Open WebMatrix, click on Open Site and then select Open Folder As Site and choose the folder where your website files are located. A new window like the one shown in Figure 1 will be shown.

Figure 1 - WebMatrix

Figure 1 – WebMatrix

You should now click on Settings so to specifiy an address for your website which can be accessed from outside. You can also type in your external IP which your ISP providers. You can get your external IP from here. In my case I am using an internal port number of 9999 to serve incoming requests (Figure 2). Whenever you change the address of your website you should restart your site using the small icon in WebMatrix.

Figure 2 - Changing URL in WebMatrix

Figure 2 – Changing URL in WebMatrix

Now open your Router administration console so that we can forward incoming traffic to your development machine. At this point you should know your internal IP of your own development machine. In Figure 3 you can see my Router configuration window. Open the Forwarding window of your Router administration console. Unfortunately all routers have their own specific window so I cannot tell you which link you should click on in your Router configuration window to open this window. However if you take a look at Figure 3 you will note that my internal development machine IP is I am forwarding all requests coming from port 80 (HTTP port) to port number 9999 on my development machine, which is my local website. Thus in Figure 3

  1. The Service Port are the Incoming requests coming from Port 80
  2. The Internal Port is the port which I have used in WebMatrix to serve incoming requests
  3. The IP Address is the local IP of my development machine
Figure 3 - Port Forwarding in Router

Figure 3 – Port Forwarding in Router

Once you did all the above settings you should now open your website using the URL specified in WebMatrix. In my case I can type or else you can type your external IP which your ISP allocated to your machine. Your website should now open. If this did not happen ensure that

  1. You do not have Windows firewall turned on
  2. You have correctly set WebMatrix and correctly inputted the correct address inside the settings window
  3. You have correctly set up port forwarding on your router

Once your website is correctly shown from outside then we can move to the next step. Make sure you have a developer account set up in PayPal. You can create one here. Log in to PayPal sandbox environment and open up the PayPal IPN simulator from here.

Now open Visual Studio. Run your application and set a breakpoint on the code which is handling PayPal IPN, as shown in Figure 4. In Visual Studio click on Debug and then click on Attach to Process. You should find a process called IISExpress.exe. Double click on it so you attach the debugger to IIS Express.

Figure 4 - IPN Breakpoint

Figure 4 – IPN Breakpoint

Now open the PayPal IPN window and type the address of your local development machine which serves the PayPal IPN. For example in my case in Figure 5 I’m typing and type in any data your ASP.NET application is reading from PayPal IPN. Basically in this PayPal IPN window you can type the Gross amount (mc_gross field in IPN) which represents the total amount the user has paid, and the InvoiceId (invoice field in IPN) which represents the Invoice the user is paying.

Figure 5 - IPN Simulator

Figure 5 – IPN Simulator

Finally click on Send IPN in PayPal IPN simulator and you should see that the Visual Studio stops on your breakpoint and you can trace all your code using Visual Studio and hopefully ensure that everything works well in your PayPal IPN .NET code.

I hope that you found this blog post interesting…… :)

Leave a Reply

Your email address will not be published. Required fields are marked *


HTML tags are not allowed.