【问题标题】:PayPal IPN Error贝宝 IPN 错误
【发布时间】:2014-08-25 06:19:05
【问题描述】:

当我通过 IPN 模拟器运行以下代码时,模拟器说它可以工作,但是我的页面收到了无效响应。

我做了一些阅读,paypal 说它使用字符集 UTF-8,但是当我更改为该模拟器失败时,我没有收到任何响应。

代码在页面加载时运行

string postUrl = ConfigurationManager.AppSettings["PayPalSubmitUrlSandBox"];
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);

    //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);
    string ipnPost = strRequest;
    strRequest = "cmd=_notify-validate&" + strRequest;
    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")
    {
       //removed this because I don't think it is what is causing the trouble }


        }
        else if (strResponse == "INVALID")
        {
       //removed this because I don't think it is what is causing the trouble


        }
        else
        {
       //removed this because I don't think it is what is causing the trouble
        }

【问题讨论】:

    标签: c# asp.net paypal-ipn


    【解决方案1】:

    想通了...不太确定与以前的代码相比有什么变化。我刚刚在教程上找到了代码并做了一些小的修改。

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Drawing.Imaging;
    using System.Drawing;
    using System.Globalization;
    using System.Net;
    using System.Text;
    using System.Collections.Generic;
    using System.Web.Configuration;
    using System.Web.UI.HtmlControls;
    using System.Drawing.Imaging;
    using System.Drawing;
    
    
    public partial class paypal_IPNHandler : System.Web.UI.Page
    {
        string payerEmail;
        string paymentStatus;
        string receiverEmail;
        string amount;
        /// <summary>
        /// Process an incoming Instant Payment Notification (IPN)
        /// from PayPal, at conclusion of a received payment from a
        /// customer
        /// </summary>
        /// 
        protected void Page_Load(object sender, EventArgs e)
    {
      // receive PayPal ipn data
    
      // extract ipn data into a string
      byte[] param = Request.BinaryRead(Request.ContentLength);
      string strRequest = Encoding.ASCII.GetString(param);
    
      // append PayPal verification code to end of string
      strRequest = "cmd=_notify-validate&" + strRequest;
    
      // create an HttpRequest channel to perform handshake with PayPal
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://www.paypal.com/cgi-bin/webscr");
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = strRequest.Length;
    
      // send data back to PayPal to request verification
      StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
      streamOut.Write(strRequest);
      streamOut.Close();
    
      // receive response from PayPal
      StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
      string strResponse = streamIn.ReadToEnd();
      streamIn.Close();
    
      // if PayPal response is successful / verified
      if (strResponse.Equals("VERIFIED"))
      {
        // paypal has verified the data, it is safe for us to perform processing now
    
        // extract the form fields expected: buyer and seller email, payment status, amount
        payerEmail = Request.Form["payer_email"];
        paymentStatus = Request.Form["payment_status"];
        receiverEmail = Request.Form["receiver_email"];
        amount = Request.Form["mc_gross"];
    
    
      }
        // else
        else
        {
          // payment not complete yet, may be undergoing additional verification or processing
    
          // do nothing - wait for paypal to send another IPN when payment is complete
        }
      String insertConnString = System.Configuration.ConfigurationManager.ConnectionStrings["BeachConnectionString"].ConnectionString;
      using (SqlConnection insertcon = new SqlConnection(insertConnString))
      using (SqlCommand cmdinsert = insertcon.CreateCommand())
      {
          string insertprofile = "INSERT INTO Test (Status, Text) VALUES (@Status, @Text)";
          cmdinsert.CommandText = insertprofile;
          cmdinsert.Parameters.Add("@Status", SqlDbType.VarChar).Value = strResponse.ToString();
          cmdinsert.Parameters.Add("@Text", SqlDbType.VarChar).Value = strRequest.ToString();
          cmdinsert.Connection.Open();
          cmdinsert.ExecuteNonQuery();
      }
      }       
    }
    

    【讨论】:

      【解决方案2】:

      尝试将您的代码更改为以下内容:

      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;
      

      另外,在进行测试之前,请确保您已登录沙盒...

      【讨论】:

      • 不走运。没有错误消息很难排除故障,但由于贝宝发布到我的页面,我无法收到它们。
      • 您在 paypal admin 中输入了返回 URL,并确保不使用 PDT 和 IPN?
      • 发现它使用了另一个教程中的一些代码......但是看起来几乎一样
      猜你喜欢
      • 2023-04-02
      • 2023-04-10
      • 2013-06-12
      • 2016-05-27
      • 2015-06-16
      • 2016-05-09
      • 2023-03-11
      • 2014-04-24
      • 2012-08-19
      相关资源
      最近更新 更多