IPN侦听器不工作MVC3

时间:2013-11-02 14:35:20

标签: paypal paypal-ipn

我的IPN列表工具无效。当我尝试使用IPN列表时错误显示如下“我们很抱歉,我们无法发送IPN。” 但我可以从浏览器访问IPN处理程序URL。

这是我的IPN处理程序代码。

  public ActionResult IPN()
    {
        LogMessage ("entering ipn action ");
        var formVals = new Dictionary<string, string>();
        formVals.Add("cmd", "_notify-validate");

        string response = GetPayPalResponse(formVals, true);
        LogMessage ("IPN Response received: " + response + " <-- That was response. . . ");

        if (response == "VALID")
        {
            LogMessage("Response Was Verified");
        }

        else
        {
            LogMessage("RESPONSE WAS NOT VERIFIED");
        }

        return Json("Sucess",JsonRequestBehavior.AllowGet);
    }

 string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox)
    {
        string paypalUrl = useSandbox
                                ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
                                : "https://www.paypal.com/cgi-bin/webscr";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

        byte[] param = Request.BinaryRead(Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);

        StringBuilder sb = new StringBuilder();
        sb.Append(strRequest);

        foreach (string key in formVals.Keys)
        {
            sb.AppendFormat("&{0}={1}", key, formVals[key]);
        }
        strRequest += sb.ToString();
        req.ContentLength = strRequest.Length;

        string response = "";
        using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
        {
            streamOut.Write(strRequest);
            streamOut.Close();
            using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                response = streamIn.ReadToEnd();
            }
        }
        return response;
    }

1 个答案:

答案 0 :(得分:1)

在我觉得可能是因为您的MVC应用程序可能在共享托管环境中发布之前我们遇到了同样的问题然后您必须按照一些步骤来使MVC RC应用程序工作 这是帮助我解决问题的博客。请检查此

http://helpnshareidea.blogspot.in/2013/11/mvc3-applications-in-windows-shared.html