Paypal IPN侦听器未返回值

本文关键字:返回值 侦听器 IPN Paypal | 更新日期: 2023-09-27 18:26:42

我正试图在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;
    }

我很可能错过了一些非常简单的东西。

任何帮助都将是惊人的。

Paypal IPN侦听器未返回值

我不得不做同样的事情,但在Web窗体中。它应该同样适用,因为我们都在处理HttpWebRequest对象。看看下面的代码-这是我的侦听器URL(listener.aspx.cs)的Page_Load()方法。注意上面写着if (strResponse == "VERIFIED")的部分,然后看看里面写着string[] responseArray = strRequest.Split('&');的部分-这是获得所有响应变量的地方。您将不得不稍微追溯到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
        }
    }