Paypal IPN 一直失败,我做错了啥?

Posted

技术标签:

【中文标题】Paypal IPN 一直失败,我做错了啥?【英文标题】:Paypal IPN keeps failing, what am I doing wrong?Paypal IPN 一直失败,我做错了什么? 【发布时间】:2019-02-02 10:13:35 【问题描述】:

自上周五以来,我一直在努力让这件事发挥作用。 我正在使用 C# asp.net 4.5 项目,Paypal 示例代码只有 MVC 和 .net CORE 示例,这两个我都不太熟悉。

我几乎没有做过 MVC 编程。所以我决定硬着头皮建了一个单独的 MVC 项目并添加了一个控制器,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace paypal_ipn.Controllers

    public class IPNlistenerController : Controller
    
        // GET: IPNlistener
        public ActionResult Index()
        
            return Receive();
        

        [HttpPost]
        public HttpStatusCodeResult Receive()
        
            //Store the IPN received from PayPal
            LogRequest(Request);

            //Fire and forget verification task
            Task.Run(() => VerifyTask(Request));

            //Reply back a 200 code
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        

        private void VerifyTask(HttpRequestBase ipnRequest)
        
            ShantMailer.SendEmail("my@email.com", "IPN MVC called", "yup");
            var verificationResponse = string.Empty;

            //Post back to either sandbox or live
            string strSandbox = "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr";
            string strLive = "https://ipnpb.paypal.com/cgi-bin/webscr";
            string strRequest= string.Empty;
            try
            
                var verificationRequest = (HttpWebRequest)WebRequest.Create(strSandbox);
                //Set values for the verification request
                verificationRequest.Method = "POST";
                verificationRequest.ContentType = "application/x-www-form-urlencoded";
                var param = Request.BinaryRead(ipnRequest.ContentLength);
                strRequest = Encoding.ASCII.GetString(param);

                //Add cmd=_notify-validate to the payload
                strRequest = "cmd=_notify-validate&" + strRequest;
                verificationRequest.ContentLength = strRequest.Length;

                //Attach payload to the verification request
                var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
                streamOut.Write(strRequest);
                streamOut.Close();

                //Send the request to PayPal and get the response
                var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
                verificationResponse = streamIn.ReadToEnd();
                streamIn.Close();

            
            catch (Exception ex)
            
                string nvcs = "Name Value Pairs: <br/>";
                try
                
                    NameValueCollection these_argies = HttpUtility.ParseQueryString(strRequest);
                    foreach (string s in these_argies)
                        foreach (string v in these_argies.GetValues(s))
                            nvcs += s + ": " + v + "<br/>";
                
                catch  
                ShantMailer.SendError(ex.Message + "<br/><br/>" + ex.InnerException + "<br/><br/>" + ex.StackTrace + "<br/><br/>" + nvcs + "<br/><br/>Request: " + strRequest + "<br/><br/>Response: " + verificationResponse);

                //Capture exception for manual investigation
            

            ProcessVerificationResponse(verificationResponse, strRequest);
        


        private void LogRequest(HttpRequestBase request)
        
            // Persist the request values into a database or temporary data store
        

        private void ProcessVerificationResponse(string verificationResponse, string strRequest)
        
            if (verificationResponse.Equals("VERIFIED"))
            
                ShantMailer.SendEmail("my@email.com", "IPN", strRequest + " <br><br><br> " + verificationResponse);
                // check that Payment_status=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 (verificationResponse.Equals("INVALID"))
            
                ShantMailer.SendEmail("my@email.com", "IPN", strRequest + " <br><br><br> " + verificationResponse);
                //Log for manual investigation
            
            else
            
                //Log error
            
        
    

My solution explorer looks like this

现在,当我从浏览器调用该页面时,我会在 Chrome 开发者控制台(网络选项卡)中看到 HttpResponse 200

如您所见,我什至在我的代码中加入了检查点,用于向我发送电子邮件通知以进行调试。当我自己在浏览器中加载页面时,我收到了“IPN MVC Called”电子邮件。但是当我把它交给 IPN 模拟器时,Paypal 一直说“没有发送 IPN,也没有验证握手。请检查您的信息。”

Paypal 在他们的文档中并不一致,一些页面说使用“https://ipnpb.paypal.com/cgi-bin/webscr”,而在他们的代码示例中它是“https://www.paypal.com/cgi-bin/webscr”

或者对于沙弓“https://ipnpb.sandbox.paypal.com/cgi-bin/webscr”与他们的代码示例“https://www.sandbox.paypal.com/cgi-bin/webscr”

无论如何,我已经尝试了所有可能的组合,但仍然不确定出了什么问题。

这是我的听众所在的地方:https://www.shantwebdesign.com/paypal_ipn/IPNlistener/Index/

非常感谢任何帮助或领导。 谢谢

【问题讨论】:

【参考方案1】:

问题解决了。 确保您的 SSL 在 TLS 版本是 1.2 Paypal 说 1.2 或更高版本,我有 1.3 没有用。降级到 1.2 成功了

【讨论】:

以上是关于Paypal IPN 一直失败,我做错了啥?的主要内容,如果未能解决你的问题,请参考以下文章

单对象 json 解析 - 我做错了啥?

django - Ajax 实时搜索,我做错了啥?

Xcode 中的 else if 或 or 语句。不知道我做错了啥

违反 PRIMARY KEY 约束“PK_Address”。等等......我做错了啥?

无法使用 Travis-CI 运行可执行文件-不确定我做错了啥

sectionName TableView - 我做错了啥?