Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have been facing a problem. I have a website that has a shopping cart integrated. At payment I am taking the customer to paypal website and after successfull payment, paypal IPN updates my database with the success status, the issue is paypal IPN is not been able to do its processing and not been able to update the database. The code I am using is the one provided for IPN, I have no idea where I am going wrong, here is the code:
C#
protected void Page_Load(object sender, EventArgs e)
       {
           //Parse transaction detail from Paypal
           byte[] param = Request.BinaryRead(Request.ContentLength);
           string strRequest = Encoding.ASCII.GetString(param);
           Dictionary<string, string> nvs = new Dictionary<string, string>();
           string[] items = strRequest.Split("&".ToCharArray());
           foreach (string item in items)
           {
               string[] parts = item.Split("=".ToCharArray());
               nvs.Add(parts[0], HttpUtility.UrlDecode(parts[1]));
           }
           //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(strLive);
           //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
           //Hook up 1&1 proxy server
           WebProxy proxy = new WebProxy("ntproxyus.lxa.perfora.net:3128",false);
           //WebProxy proxy = new WebProxy(new Uri("http://ntproxy.1and1.com:3128"));
           req.Proxy = proxy;
           //Send transaction detail with the flag back to PayPal
           StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
           streamOut.Write(strRequest);
           streamOut.Close();
           //Get validation status from PayPal
           StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
           string strResponse = streamIn.ReadToEnd();
           streamIn.Close();
           if (strResponse == "VERIFIED")
           {
               //check the payment_status is Completed
               if (nvs["payment_status"] == "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
                   DataLayer dl = new DataLayer();
                   dl.executeQuery("Update database table");
               }
           }
           else if (strResponse == "INVALID")
           {
               //log for manual investigation
           }
           else
           {
               //log response/ipn data for manual investigation
           }
       }

Thanks in Advance,
Asfand
Posted
Updated 22-Nov-10 4:18am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900