IPN - Instant Payment Notification with C#
November, 2nd 2011 | .NET
Instant Payment Notification, better known as IPN, is a PayPal service that can send notifications to our server after a transaction.
By integrating a payment system offered by PayPal, in fact, we are not able to know in real time the result of the economic transaction.
For this reason, PayPal sends an asynchronous call that allows us, via parameters, to identify the transaction and, for example, change the status of an order from "Confirmed" to "Paid".
It is possible to set the address of the handler in your profile settings or via the "notify_url" parameter.
The first step is to collect all the parameters sent by PayPal, adding the string "cmd = _notify-validated":
string request = "cmd=_notify-validate";
foreach (string key in context.Request.Form.Keys)
{
request += "&" + key + "=" + context.Request.Form[key];
}
You then need to post this string to the PayPal server:
WebClient webClient = new WebClient();
byte[] response = webClient.UploadData("https://www.paypal.com/cgi-bin/webscr", UTF8Encoding.UTF8.GetBytes(request));
string result = UTF8Encoding.UTF8.GetString(response);
At this point we have available, as well as the parameters previously used, the result. It will be VERIFIED if the operation is successful, INVALID otherwise.