Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting 400 Bad request when try to payment through the pay pal.
Here i got the transaction id but also i did not get the success response
How can i solve this error please me.

My code is:

C#
public void Getresponse()
       {
           try
           {

               ServicePointManager.Expect100Continue = true;
               ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
               ServicePointManager.DefaultConnectionLimit = 9999;

               string authToken = ConfigurationManager.AppSettings["PaypalToken"].ToString();
               string txToken = Request.QueryString["tx"];
               Label1.Text = txToken;
               string query = "cmd=_notify-synch&tx=" + txToken + "&at=" + authToken;
               int pid = Convert.ToInt32(Request.QueryString["Pid"]);

               //Post back to either sandbox or live
               string strPaypalLive = ConfigurationManager.AppSettings["PaypalLive"].ToString();
               HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strPaypalLive);
               var encoding = new UTF8Encoding();
               //Set values for the request back
               req.Method = "POST";
               req.ContentType = "application/x-www-form-urlencoded";
               req.ContentLength = query.Length;

               //Send the request to PayPal and get the response

               StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
               streamOut.Write(query);
               streamOut.Close();
               StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
               string strResponse = streamIn.ReadToEnd();
               streamIn.Close();

               Dictionary<string, string> results = new Dictionary<string, string>();
               Label3.Text = strResponse;
               if (strResponse != "")
               {
                   Label2.Text = strResponse;
                   StringReader reader = new StringReader(strResponse);
                   string line = reader.ReadLine();

                   if (line == "SUCCESS")
                   {
                       Label2.Text = "Inside Success !";
                       ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('Hello')", true);
                       using (OwlEducationEntities context = new OwlEducationEntities())
                       {
                           if (Request.QueryString["Custom"] != null)
                           {
                               ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert("+txToken+")", true);

                               string QryString = Request.QueryString["Custom"].ToString();
                               GRI_Package pkg = (from t in context.GRI_Package where t.PackageID == 3 select t).SingleOrDefault();
                               pkg.TransectionID = txToken;
                               pkg.IsPaid = true;
                               context.SaveChanges();
                           }
                       }
                       //if (Request.QueryString["Custom"] != null)
                       //{
                       //    string QryString = Request.QueryString["Custom"].ToString();
                       //}

                   }
                   else if (line == "FAIL")
                   {
                       Label3.Text = "Fail"+line;
                       // Log for manual investigation
                       //Response.Write("Unable to retrive transaction detail");
                   }
               }
               else
               {
                   //unknown error
                   Label3.Text = "Error";
                   Response.Write("ERROR");
               }
           }
           catch (Exception Ex)
           {
               Label3.Text ="catch"+ Ex.Message;
               Exceptions.LogException(Ex);
           }

}

What I have tried:

i tried this code for getting response

StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(query);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
Posted
Comments
ZurdoDev 23-Feb-16 8:06am    
Contact Paypal support.

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