Setting up PayPal Instant Payment Notification(IPN) with C# – CodeProject

Article Link :

<%@ 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="JavaScript">
//Some JavaScript you may need goes here

<script Language="C#" Option="Explicit"  runat="server">

void Send_download_link (string from,  string to, string subject, string body)   
   {  // Construct a new e-mail message 
      SmtpClient client = new SmtpClient (smtpClient);
      client.Send (from, to, subject, body);
   catch (SmtpException ex)
      debuggy.Text = "Send_download_link: " + ex.Message;

} // --- end of Send_download_link --

protected void Page_Load(object sender, EventArgs e)

	//Post back to either sandbox or live
	string strSandbox = "";
	string strLive = "";
	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);
	string strResponse_copy = strRequest;  //Save a copy of the initial info sent by PayPal
	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);
	StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
	string strResponse = streamIn.ReadToEnd();

	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

	        // pull the values passed on the initial message from PayPal

		  NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
		  string user_email = these_argies["payer_email"];
		  string pay_stat = these_argies["payment_status"];
                  //.  more args as needed look at the list from paypal IPN doc

                if(pay_stat.Equals("Completed") )
			Send_download_link ("",  user_email, "Your order","Thanks for your order this the downnload link ... blah blah blah" );

		// more checks needed here specially your account number and related stuff
	else if (strResponse == "INVALID")
		//log for manual investigation
		//log response/ipn data for manual investigation
}  // --- end of page load --


<head runat="server" />
<title>IPN PayPal</title>
<asp:label id="debuggy" runat="server"/>
<h2> my test page</h2>
Load this first to check the syntax of your page
Thanks to Author: becker666

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s