贝宝IPN不断失败,我在做什么错?

时间:2018-08-27 20:39:39

标签: asp.net paypal asp.net-mvc-5 paypal-sandbox paypal-ipn

自上周五以来,我一直在努力使此工作正常。 我正在使用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 个答案:

答案 0 :(得分:1)

问题解决了。 确保您的SSL在TLS版本上是1.2 贝宝说1.2或更高,我有1.3没用。降级到1.2可以达到目的