I am trying to process a response from PayPal on my page. I found this code from the PayPal documentation itself. When the response is valid, some parameters should be treated as txn_id
and payment_completed
. How can I do it? the code is as follows
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") { //UPDATE YOUR DATABASE //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt"); //txWriter.WriteLine(strResponse); //txWriter.Close(); //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") { //UPDATE YOUR DATABASE //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt"); //txWriter.WriteLine(strResponse); ////log for manual investigation //txWriter.Close(); } else { //UPDATE YOUR DATABASE //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt"); //txWriter.WriteLine("Invalid"); ////log response/ipn data for manual investigation //txWriter.Close(); } }
Give me some materials, please. Thanks
source share