Active Data Online

Article: ASP.NET and PayPal's Instant Payment Notification (IPN)

PayPal IPN is an example of business to business system communication via HTTP. Essentially, IPN enables your system to receive real time notification of a payment from PayPal.

The primary requirement for IPN to work is to accept a HTTP post from PayPal containing information on a transaction, and then to return the same HTTP post back to PayPal (as a receipt) with an additional element appended.

Receiving information is done using the form collection: Request.Form. Posting back programmatically is the part that this article covers.

In the world of ASP and COM, the standard approach was to use MSXML to send HTTP posts programmatically. While this approach is still available for a .NET based system because all COM components can be accessed via .NET COM interop, it is strictly not a pure .NET solution.

In the world of .NET, the desired functionality can be achieved using the classes System.Net.HttpWebRequest and System.Net.HttpWebResponse. These classes enable you to interact directly with the HTTP protocol.

To listen for IPN's, you will have to create an .aspx page with the following code somewhere within the page load event. The code prepares a HTTP post and sends it to the target URL and then waits for a HTTP response, receives the response and assigns it to a variable, stringResult. Sending a web request and receiving a response using HTTP is akin to a browser fetching a web page from a server on the Internet.

It should be said that while this code example is written in C#, the classes and logic used can be transferred across to any language (VB.NET, C++.NET, Perl.NET, etc) that can run as managed code within the .NET framework.


// A post is essentially a string delimited in a special way
string stringPost = Request.Form.ToString(); ...

// Accept the form elements (e.g., Request.Form.Get("txn_id")) and store them in local variables.
...
HttpWebRequest httpWebRequest
   = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = stringPost.Length + 21; // &cmd=_notify-validate is 21 chars long
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter streamWriter = null;
streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
stringPost = stringPost + "&cmd=_notify-validate";
streamWriter.Write(stringPost);
streamWriter.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
   stringResult = streamReader.ReadToEnd();
   streamReader.Close();
}


Once you have replied to PayPal acknowledging receipt of the transaction information, your system can validate and store the information that PayPal has sent. Moreover, it can send email notification to the customer upon determining that all is well.

With ASP.NET, integrating with PayPal's IPN simple and efficient.


This article is provided courtesy of Active Data Online Pty Ltd, www.activedataonline.com.au

Date: January 2003




Copyright Active Data Online Pty Ltd  ABN 83 095 152 453  All Rights Reserved