Thursday, September 29, 2011

IPN

In pageload (.cs file) write the following code to receive the ipn string and save the fields in database :

//Depending on your testing change sandbox and live paypal links below.

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);
string strResponse_copy = strRequest;
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();
NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
string pay_stat2 = these_argies["auth_amount"];
string pay_stat3 = these_argies["payment_date"];
string pay_stat4 = these_argies["payment_gross"];
string pay_stat5 = these_argies["mc_currency"];
string pay_stat6 = these_argies["mc_grosss"];


if (strResponse == "VERIFIED")
{
//check the payment_status is Completed auth_amount,payment_date,payment_gross,payment_gross_x,payment_status ,mc_currency ,mc_gross,
//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"];
//////lblamount.Text = user_email.ToString();
//////string pay_stat = these_argies["payment_status"];
//////lblamount2.Text = pay_stat.ToString();
//////////OleDbCommand MAcmd;

//System.Data.OleDb.OleDbConnection conn = new
//System.Data.OleDb.OleDbConnection();
//// TODO: Modify the connection string and include any
//// additional required properties for your database.
//conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
// @"Data source= location";

//conn.Open();
//// Insert code to process data.
//MAcmd = conn.CreateCommand();
//MAcmd.CommandText = " write the insert query";
//MAcmd.ExecuteNonQuery();

//.
//. more args as needed look at the list from paypal IPN doc
//.




////conn.Close();
// more checks needed here specially your account number and related stuff
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}

}


If you write any error in your code you will g
et error message while sending the ipn from paypal.  

No comments:

Post a Comment