asp.net-mvc-4 - Paypal IPN 无法发送到我的 MVC 4 Controller 操作

标签 asp.net-mvc-4 paypal

我尝试从我的 MVC Controller 操作获取 IPN 消息,但 IPN 无法发送到我的操作 Controller

以下是我的 Controller 操作:

                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.Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(Param);
                strRequest = strRequest + "&cmd=_notify-validate";
                req.ContentLength = strRequest.Length;

                //for proxy
                //Dim proxy As New WebProxy(New System.Uri("http://url:port#"))
                //req.Proxy = proxy

                //Send the request to PayPal and get the response
                StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
                streamOut.Write(strRequest);
                streamOut.Close();
                StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
                string strResponse = streamIn.ReadToEnd();
                streamIn.Close();

                if (strResponse == "VERIFIED") {
                    //check the payment_status is 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
                } else if (strResponse == "INVALID") {
                    //log for manual investigation
                } else {
                    //Response wasn't VERIFIED or INVALID, log for manual investigation
                }

以下是我使用的paypal信息:

<add key="paypalAccount"    value="abc@abc.com" />
<add key="PayPalSubmitUrl" value="https://www.paypal.com/cgi-bin/webscr"/>
<add key="PDTToken" value="asdfwlfti2Rc_Kskwsdfc7uK26FmT1pdAkhSdLb8FvS2kQ-ZQp6VoF2SqYem"/>
<add key="FromMail" value="myat@abc.com"/>
<add key="return_Url" value="http://testing/Purchase/PayPalResult"/>
<add key="cancel_return" value="http://testing/Purchase/Purchase"/>
<add key="notify_url" value="http://testing/Purchase/PayPalPaymentNotification"/>
<add key="Test_Live" value="live"/>
<add key="BCC" value="myat@abc.com"/>

但在 paypal 中 IPN 消息仍在重试,我没有收到任何东西 我注意到 HttpContext.Request.ContentLength 始终为 0,paypal 的响应状态始终为 INVALID 那么请告诉我我该怎么做?

谢谢

最佳答案

这是我获取IPN消息的完整 Action

    public ActionResult PayPalPaymentNotification()
    {
        string strLog = "";
        string currentTime = "";


        currentTime = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year + "|" + DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString();

        strLog = "Insert into CPLog(Log,LogTime) values('Start IPN request','" + currentTime + "')";
        InsertData(strLog);
        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.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(Param);
        strRequest = strRequest + "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

        //for proxy
        //Dim proxy As New WebProxy(New System.Uri("http://url:port#"))
        //req.Proxy = proxy

        //Send the request to PayPal and get the response
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
        streamOut.Write(strRequest);
        streamOut.Close();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        streamIn.Close();

        if (strResponse == "VERIFIED")
        {
            //check the payment_status is 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
            currentTime = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year + "|" + DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString();

            strLog = "Insert into CPLog(Log,LogTime) values('Status - Verified','" + currentTime + "')";
            InsertData(strLog);
        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
            currentTime = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year + "|" + DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString();

            strLog = "Insert into CPLog(Log,LogTime) values('Status - Invalid','" + currentTime + "')";
            InsertData(strLog);
        }
        else
        {
            //Response wasn't VERIFIED or INVALID, log for manual investigation
        }
        currentTime = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year + "|" + DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString();

        strLog = "Insert into CPLog(Log,LogTime) values('Finish IPN Request','" + currentTime + "')";
        InsertData(strLog);
        return View();
    }

关于asp.net-mvc-4 - Paypal IPN 无法发送到我的 MVC 4 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204336/

相关文章:

javascript - 验证 FORM 内 DIV(可见向导步骤)内的 HTML 输入元素?

paypal - 延迟链式支​​付

iphone - 如何使用 PayPal API Xcode 上线

asp.net-mvc-4 - 使用 Ninject.Extensions.Conventions 在 MVC4 WebApi 中自动绑定(bind)

c# - 确定 PayPal IPN 使用哪种编码?

php - PayPal PHP SDK - 不返回 GET 变量 'success'

PHP mySQL 和 Paypal 结帐程序

asp.net-mvc-4 - 为什么我的脚本包只在服务器上工作

javascript - 类型 ="image"的输入按钮在 IE 11 中无法与 href 配合使用

c# - 注册抛出 'Inheritance security rules violated while overriding member'