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 suppossedly 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
OK enough of this VB balloney, let's get real although C# is not real it is close
let's use it here to handle it
<%@ 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" %>
The sendmail here is the simplest your website setting may or may not allow this but 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 specially checking your account number and may be some extra processing and all of that can be hooked up to the "listener" page . e.g. I needed to add the buyers to a mail list for sending them notifications.