Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Paypal IPN returning VERIFIED but IPN Fails to Send

I will try to get straight to the point. I am currently working with PayPal IPN and have never seen this issue before. I have used PayPal IPN and my implementations have always been the same. This time however it is producing some very weird results.

I am currently hosted with WinHost.com

Code Used:

public void MakeHttpPost()
    {

        ErrorLog log = new ErrorLog();
        //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(strSandbox);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = HttpContext.Current.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();
        log.error = strResponse;
        log.Insert();

        if (strResponse == "VERIFIED")
        {
            PaypalPaymentHistory PPH = new PaypalPaymentHistory();

            PPH.LastName = HttpContext.Current.Request["last_name"];
            PPH.FirstName = HttpContext.Current.Request["first_name"];
            PPH.State = HttpContext.Current.Request["address_state"];
            PPH.Zipcode = HttpContext.Current.Request["address_zip"];
            PPH.Address = HttpContext.Current.Request["address_street"];
            PPH.UserName = HttpContext.Current.Request["option_name2"];
            PPH.PaymentStatus = HttpContext.Current.Request["payment_status"];
            PPH.SelectedPackage = HttpContext.Current.Request["option_selection1"];
            PPH.PayerStatus = HttpContext.Current.Request["payer_status"];
            PPH.PaymentType = HttpContext.Current.Request["payment_type"];
            PPH.PayerEmail = HttpContext.Current.Request["payer_email"];
            PPH.ReceiverId = HttpContext.Current.Request["receiver_id"];
            PPH.TxnType = HttpContext.Current.Request["txn_type"];
            PPH.PaymentGross = HttpContext.Current.Request["payment_gross"];

            PPH.Insert();

        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
        }
        else
        {
            //log response/ipn data for manual investigation
        }

    }

The idea here is that I will check the status of the order and then insert or not insert the record to the database but this code is still in tests so nothing is official.

The problem I am having is that when I run through sandbox and make a payment via my site paypal sends out the IPN request. The entry gets thrown into the database and all the data is sent back correctly however PayPal is showing that the IPN Post "Failed" and is always stuck on "Retrying". However I am getting "VERIFIED" back in strResponse. This in turn is causing up to 8 records per transaction. The error that paypal is reporting is 500 - Internal Server Error. Any help would be insanely appreciated as this has been a 2 day head bashing marathon up to this point!

Thanks for any help or resolutions!

P.S I have read nearly every IPN question on stackoverflow and have seen nothing like this.

like image 367
jhartzell Avatar asked Nov 22 '11 19:11

jhartzell


1 Answers

Your controller action is failing if PayPal reports a 500. You need to debug that code and see what is failing. If your controller does not send a 200 back, PayPal will keep trying.

I always do this:

    public ActionResult IPN()
    {     
        //try catch log all my payment info

        //always return blank page so paypal gets a HTTP 200
        return View();
    }

//you may know this, but for others, here is an example process flow

  1. Payment/transaction made to PayPal
  2. IPN address is configured for PayPal transaction then posts to the IPN address ie: http://yourdomain.com/IPN.aspx
  3. IPN.aspx handles the IPN post and writes PaypalPaymentHistory to db.
like image 189
rick schott Avatar answered Oct 21 '22 13:10

rick schott