c# - PayPal 即时付款通知 (IPN) 不工作 ASP.NET C#

标签 c# asp.net paypal paypal-ipn

我正在尝试让 IPN 为我的网站工作,但直到现在都没有成功。

我已经修改了所有 PayPal 文档,并且设置了所有必需的配置以使 IPN 正常工作。
这是我到目前为止所做的:
1.创建开发账号;
2. 将我的网站付款偏好更新为自动返回:开;
3. 在我的个人资料属性中打开即时付款通知;
4. 创建了一个测试支付选项事件:

protected void btCheckout_Click(object sender, EventArgs e)
{
    var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);

    queryString["cmd"] = "_cart";
    queryString["business"] = "xpto-facilitator@xpto.com";
    queryString["upload"] = "1";

    queryString["item_name_1"] = "My Cart Item 1";
    queryString["quantity_1"] = "1";
    queryString["amount_1"] = "100.00";

    queryString["shopping_url"] = "http://xpto.com/Client/Checkout";
    queryString["return"] = "http://xpto.com/Client/Checkout";
    queryString["notify_url"] = "http://xpto.com/CheckoutResult.aspx";

    Response.Redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?" + queryString.ToString());
}


5. 创建一个 IPN 页面:

protected void Page_Load(object sender, EventArgs e)
{
    //Post back to either sandbox or live
    string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);

    //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")
    {
        //log for debugging purposes
        LogManager.Error("CheckoutResult VERIFIED:" + strResponse);
    }
    else if (strResponse == "INVALID")
    {
        //log for debugging purposes
        LogManager.Error("CheckoutResult INVALID: " + strResponse);
    }
    else
    {  
        //log for debugging purposes
        LogManager.Error("CheckoutResult something else: " + strResponse);
    }
}
  1. 我设法继续结帐并成功支付了元素;
  2. 我的 IPN 页面没有执行,因为我没有任何记录;
  3. 使用即时支付通知 (IPN) 模拟器测试了我的处理程序,我总是收到以下错误:

We could not send an IPN due to an HTTP error: 401: Unauthorized


我究竟做错了什么?我错过了什么吗?

最佳答案

您为 IPN 指定的 notify_url 设置为需要登录的端点。

queryString["notify_url"] = "http://xpto.com/CheckoutResult.aspx";

此 URL 必须设置为可以接受和处理来自 IPN 服务的 POST 请求而无需任何登录的端点(我似乎找不到任何专门提到此要求的文档)。这就是为什么必须验证您的监听器收到的 IPN(您看起来做得正确)的主要原因,因为在验证之前,无法完全相信收到的请求是由 PayPal 发送的。

关于c# - PayPal 即时付款通知 (IPN) 不工作 ASP.NET C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559565/

相关文章:

c# - 使用 Fluent 限制对 Azure KeyVault 的访问

c# - .NET 或 MySql 或其他解决方案,每天进行数百万次查找(以停止重复)

asp.net - Paypal 退款销售 REST API 返回 : Remote server returned an error: (404) Not Found

asp.net - 在 session 中存储购物车

ruby-on-rails - 自适应 Paypal

paypal - 对 Paypal REST API、NVP 和授予权限感到困惑

c# - WebAPI + AngularJS 中基于 DateTime 的动态 HTTP 请求

C# ListView ItemSelectionChanged Event Multi Select 仅获取最后选择的项目

c# - Asp.net 从控件中检索内容

asp.net - 在 .NET 4.7.2 下使用 VB.NET 在 WebForms 中进行构造函数注入(inject)