This article is a summary of how to set up this Instant Payment Notification (IPN) to get full control on my website along with my working code in C#.
I wanted to sell some PDA Apps out of my web site and use the PayPal Buy Now button, easy enough, after the transaction was completed by PayPal, an extra email is to be sent to the buyer with a link for the download. Of course, I needed some specific MIME types, although there are some processors that supposedly do this for a fee. I couldn't get a good answer from them regarding the MIME types and file updating policies.
So I decided to look into the PayPal developers site and they had some samples on how to set up this Instant Payment Notification (IPN). It was not so hard and I get the full control on my website. Here is the summary with my working code in C#, using a different backend is similar and just as simple.
To complete this, definitely the PayPal sandbox is needed so that the code can be debugged and it is free from the PayPal developers link. Create a buyer and a seller account and debug away.
In essence, when the Buy Now button is clicked, PayPal does its processing and at the end, it sends an email to both parties. For the case of fully automated selling electronic media, what is needed is an email with a link for the particular download to be sent to the buyer after this successful transaction. With IPN, this can be implemented on your website and it is very straightforward, all they need is a "listener". When a transaction for an item you have listed occurs, they send this listener a message of the transaction with all of the basic info like buyer/seller/item information as a set of URL query variables (e.g., payer_email=jdoe@mailcom&item_name=killer_App ...), the listener then replies back these parameters passed with an extra token and finally PayPal answers back with a VERIFIED message if it all went well or a failure message.
This is the link with the list of the URL query vars-like from PayPal.
The Listener in C#
Implementing this listener in C# is straightforward if your website hosting has support for .NET. The code below is a modification of the sample from PayPal with the addition of some basic function to carry out the email and of course how to send it after a successful interchange. To make it simpler, it is changed to script mode so all you need is just drop the page on your web site, no need for compiling the project and all of that plus scripting mode is faster. Set the URL of your listener on your PayPal profile as explained in the next section. You may need to do some extra processing once the whole process is verified like checking for certain parameters on your local DB, but those are just fine details.
Let's say we call this file ipn_pal.aspx:
<%@ Page Language="C#" %>
<%@ Import Namespace = "System"%>
<%@ Import Namespace = "System.IO"%>
<%@ Import Namespace = "System.Text" %>
<%@ Import Namespace = "System.Net" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Net.Mail" %>
<script Language="C#" Option="Explicit" runat="server">
void Send_download_link (string from, string to, string subject, string body)
SmtpClient client = new SmtpClient (smtpClient);
client.Send (from, to, subject, body);
catch (SmtpException ex)
debuggy.Text = "Send_download_link: " + ex.Message;
protected void Page_Load(object sender, EventArgs e)
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest;
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
StreamWriter streamOut =
new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
if (strResponse == "VERIFIED")
NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
user_email, "Your order",
"Thanks for your order this the download link ... blah blah blah" );
else if (strResponse == "INVALID")
<head runat="server" />
<asp:label id="debuggy" runat="server"/>
<h2> my test page</h2>
Load this first to check the syntax of your page
The sendmail here is the simplest. Your website setting may or may not allow this, but it is not a show stopper.
Enabling IPN on your PayPal and Pointing to the Listener
In your Paypal Account, go to Home->Profile. You will see in the Selling Preferences column a link for the Instant Payment Notification. Enable it and set the Notification URL (listener) to the path on your website, e.g., http://mycoolwebsite.com/ ipn_pal.aspx. That is it.
As you can see, it is straightforward. Of course, a bunch of tests should be carried out with the sandbox Seller/Buyer accounts to make sure the flow is proper. Also, more basic checks are needed, especially checking your account number and may be some extra processing and all of that can be hooked up to the "listener" page. For example, I needed to add the buyers to a mail list for sending them notifications.
- 29th May, 2010: Initial version