[C#]Processing IPN

Joined
Jan 27, 2008
Messages
541
Reaction score
12
I have a C# web server that im trying to integrate paypal's IPN into. I have viewed the asp.net examples hoping that it would help with C#. Unfortunately C# doesnt have the "Request" object built into it, and when i use the system.web dll it comes back as a null reference exception. Does anyone happen to have a example using C# outside of asp.net's C#?
 
I have a C# web server that im trying to integrate paypal's IPN into. I have viewed the asp.net examples hoping that it would help with C#. Unfortunately C# doesnt have the "Request" object built into it, and when i use the system.web dll it comes back as a null reference exception. Does anyone happen to have a example using C# outside of asp.net's C#?

As far as I know IPN works as following:

PayPal makes a POST to IANA — Example domains
You send post back to paypal to verify, and paypal returns FAIL or VALID.
If valid you use post from paypal if fail, you log the action blah blah blah.

So in your case you would have to eaither make it listen on some port and act kinda like a dynamic webserver (or whatever its called, tornado is an example).
 
Yeah, i have done this successfully in PHP, paypal sends the POST to the web server, and im trying to get that POST data so i can send it back, but im not sure on how to receive it all in C#
 
Yeah, i have done this successfully in PHP, paypal sends the POST to the web server, and im trying to get that POST data so i can send it back, but im not sure on how to receive it all in C#

I woould look into how to make simple C# webserver then use code from there to implement your C# Paypal IPN listener.
But ask yourself. Do you really wanna go thrue (for the sake of god, can't remember how to spellthat word correctly) all that?
 
gooooooooooooooogle
Code:
// ASP .NET C#

using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;

public partial class csIPNexample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
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);

//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;

//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;

//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();

if (strResponse == "VERIFIED")
{
//check the payment_status is Completed
//check that txn_id has not been previously processed
//check that receiver_email is your Primary PayPal email
//check that payment_amount/payment_currency are correct
//process payment
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}
}
read the full return stream to get the post data
 
Last edited:
Back