ASP.NET Paypal IPN返回验证但IPN无法发送

时间:2011-11-22 19:04:24

标签: c# asp.net paypal paypal-ipn

我会尽力直截了当。我目前正在使用PayPal IPN,之前从未见过这个问题。我使用过PayPal IPN,我的实现一直都是一样的。然而这次它产生了一些非常奇怪的结果。

我目前托管的是WinHost.com

使用的代码:

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
        }

    }

这里的想法是我将检查订单的状态,然后插入或不插入记录到数据库,但这段代码仍在测试中,所以没有任何正式的。

我遇到的问题是,当我通过沙箱运行并通过我的网站付款时,paypal会发出IPN请求。该条目被抛入数据库并且所有数据都被正确地发回,但是PayPal显示IPN帖子“失败”并且始终停留在“重试”。但是我在strResponse中得到了“VERIFIED”。这反过来导致每个事务最多8个记录。 paypal报告的错误是500 - 内部服务器错误。任何帮助都会被疯狂地欣赏,因为到目前为止这是一场为期2天的抨击马拉松比赛!

感谢您的帮助或解决方案!

P.S我几乎已经阅读了stackoverflow上的每个IPN问题,并且没有看到这样的内容。

1 个答案:

答案 0 :(得分:6)

如果PayPal报告500,您的控制器操作将失败。您需要调试该代码并查看失败的内容。如果你的控制器没有发回200,PayPal会继续尝试。

我总是这样做:

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

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

//您可能知道这一点,但对于其他人来说,这是一个示例流程

  1. 向PayPal付款/交易
  2. 为PayPal交易配置IPN地址,然后发布到IPN地址,即:http://yourdomain.com/IPN.aspx
  3. IPN.aspx处理IPN帖子并将PaypalPaymentHistory写入db。