Paypal IPN 侦听器不返回值

Posted

技术标签:

【中文标题】Paypal IPN 侦听器不返回值【英文标题】:Paypal IPN Listener not returning values 【发布时间】:2013-08-07 21:34:40 【问题描述】:

我正在尝试在 C# MVC 网站中创建一个 IPN 侦听器。在网上查看后,我设法在下面创建了 ActionResult。

侦听器收到“已验证”响应,但不幸的是,它似乎没有收到有关付款的任何其他信息。

    [HttpPost]
    public ActionResult IPN()
    
        var log = new LogMessage();
        log.LogMessageToFile("IPN recieved!");
        var formVals = new Dictionary<string, string>();
        formVals.Add("cmd", "_notify-validate");

        string response = GetPayPalResponse(formVals, true);

        if (response == "VERIFIED")
        
            log.LogMessageToFile("IPN VERIFIED!");
            //validate the order
            string sAmountPaid = Request.QueryString["amt"];
            string sPayment = ConfigurationManager.AppSettings["amount"].ToString();
            Decimal amountPaid = 0;
            Decimal Payment = 0;
            Decimal.TryParse(sAmountPaid, out amountPaid);
            Decimal.TryParse(sPayment, out Payment);

            if (Payment <= amountPaid)
            
                log.LogMessageToFile("IPN Correct amount");
                //process it
                try
                
                    string GUID = Request.QueryString["cm"];
                    string strGatewayResponse = Request.QueryString["tx"];
                    var data = new Datalayer();
                    data.AddPayment(GUID, amountPaid, strGatewayResponse, true);
                    log.LogMessageToFile("IPN Commplete");
                    return Redirect("/Payment/Success");
                
                catch
                
                    log.LogMessageToFile("IPN Error");
                    return Redirect("/Payment/Error");
                
            
            else
            
                log.LogMessageToFile("IPN Incorrect amount!");
                log.LogMessageToFile("IPN amount:" + Request.QueryString["payment_gross"]);
                log.LogMessageToFile("IPN GUID:" + Request.QueryString["custom"]);
                log.LogMessageToFile("IPN ID:" + Request.QueryString["txn_id"]);
                return Redirect("/Payment/Error");
            
        
        log.LogMessageToFile("IPN not verified!");
        return View("/Payment/Error");
    


 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】:

我必须做同样的事情,但在 Web 表单中。它应该同样适用,因为我们都在处理HttpWebRequest 对象。看看下面的代码 - 这是我的侦听器 URL (Listener.aspx.cs) 的 Page_Load() 方法。注意上面写着if (strResponse == "VERIFIED") 的部分,看看里面写着string[] responseArray = strRequest.Split('&amp;'); 的地方——这是你得到所有响应变量的地方。您将不得不稍微回溯到 strRequest 的定义,该定义用于获取值(由 & 号分隔)。

此外,请查看您的 PayPal 帐户上的 IPN 响应。响应将是一个由 & 分隔的长字符串 - 这将与 strRequest 的值相同。这是您提取有关付款的其他信息的字符串:

protected void Page_Load(object sender, EventArgs e)
    
        //string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        //string strLive = "https://www.paypal.com/cgi-bin/webscr";
        string url = ConfigurationManager.AppSettings["PayPalUrl"];
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;


        //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();

        if (strResponse == "VERIFIED")
        
            // strRequest is a long string delimited by '&'
            string[] responseArray = strRequest.Split('&');

            List<KeyValuePair<string, string>> lkvp = new List<KeyValuePair<string, string>>();

            string[] temp;

            // for each key value pair
            foreach (string i in responseArray)
            
                temp = i.Split('=');
                lkvp.Add(new KeyValuePair<string, string>(temp[0], temp[1]));
            

            // now we have a list of key value pairs
            string firstName = string.Empty;
            string lastName = string.Empty;
            string address = string.Empty;
            string city = string.Empty;
            string state = string.Empty;
            string zip = string.Empty;
            string payerEmail = string.Empty;
            string contactPhone = string.Empty;

            foreach (KeyValuePair<string, string> kvp in lkvp)
            
                switch (kvp.Key)
                
                    case "payer_email":
                        payerEmail = kvp.Value.Replace("%40", "@");
                        break;
                    case "first_name":
                        firstName = kvp.Value;
                        break;
                    case "last_name":
                        lastName = kvp.Value;
                        break;
                    case "address_city":
                        city = kvp.Value.Replace("+", " ");
                        break;
                    case "address_state":
                        state = kvp.Value.Replace("+", " ");
                        break;
                    case "address_street":
                        address = kvp.Value.Replace("+", " ");
                        break;
                    case "address_zip":
                        zip = kvp.Value;
                        break;
                    case "contact_phone":
                        contactPhone = kvp.Value;
                        break;
                    default:
                        break;
                
            

            string userName = payerEmail;
            string password = Membership.GeneratePassword(8, 0);

            MembershipCreateStatus status = new MembershipCreateStatus();
            MembershipUser newUser = Membership.CreateUser(userName, password, userName, null, null, true, out status);

            ProfileCommon pc = ProfileCommon.Create(userName) as ProfileCommon;
            pc.Address.PostalCode = zip;
            pc.Address.Address = address;
            pc.Address.City = city;
            pc.Address.State = state;
            pc.Personal.FirstName = firstName;
            pc.Personal.LastName = lastName;
            pc.Contacts.DayPhone = contactPhone;
            pc.Save();

            if (status == MembershipCreateStatus.Success)
            
                Roles.AddUserToRole(userName, "User");

                //send email to user indicating username and password
                SendEmailToUser(userName, password, firstName, lastName, payerEmail);
            

            // need to figure out a way to catch unwanted responses here... redirect somehow
        
        else if (strResponse == "INVALID")
        
            //log for manual investigation
        
        else
        
            //log response/ipn data for manual investigation
        
    

【讨论】:

以上是关于Paypal IPN 侦听器不返回值的主要内容,如果未能解决你的问题,请参考以下文章

需要 Paypal IPN 侦听器帮助

在 PayPal Sandbox 中没有收到对侦听器的 IPN 响应

Perl PayPal IPN 侦听器返回“500 SSL 握手失败”消息

Rails 监听器 Paypal IPN 总是返回 INVALID

Paypal IPN 后如何添加 GA 交易

Paypal IPN 侦听器正在使我们的网站崩溃