c# - 使用空数据验证 IPN

标签 c# asp.net-mvc paypal paypal-ipn paypal-adaptive-payments

我会尽量弄清楚这一点。

我使用集成的 PayPal 自适应支付和实现 IPN,使用 ASP.NET MVC 和 C#。我尝试了 IPN 模拟器,一切正常。我接到电话并处理了数据。问题是在使用我创建的测试帐户进行付款时。付款成功,我收到“已验证”,但没有关于付款人或交易的数据,或任何东西。在我的 PayPal 帐户中,所有交易均成功。我可以发布我的代码,但我看不出这会有什么帮助。问题是当从模拟器发送 IPN 时它工作得很好,但是当沙箱在成功支付后发送 IPN 时它不能正常工作。

最佳答案

很难准确判断您的问题可能是什么。我从未使用过他们的测试工具,但这里有一些代码适用于 Sandbox 和生产版本。请注意,原始 POST 数据实际上具有交易信息。就我而言,我只关心备忘录字段中的数据。 MVC 绑定(bind)确保其被填充。

您可能还想检查一件事...登录 PayPal 后,您可以查看 IPN 历史记录并查看发送的每条消息的内容。也许这就是问题所在。

    public class IPNController : Controller
    {
        private readonly ILogger _logger;
        private readonly IPaymentManager _paymentManager;
        private readonly IIdentityManager _identityManager;

        public IPNController(ILogger logger, IPaymentManager paymentManager, IIdentityManager identityManager)
        {
            _logger = logger;
            _paymentManager = paymentManager;
            _identityManager = identityManager;
        }

        [HttpPost]
        public HttpStatusCodeResult Receive(PayPalCheckoutInfo info)
        {
            //Fire and forget verification task
            Task.Run(() => VerifyTask(Request, info.Memo));

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

        private void VerifyTask(HttpRequestBase ipnRequest, string memo)
        {
            try
            {
                var verificationRequest = (HttpWebRequest)WebRequest.Create(Application.PayPalIPNUrl);

                //Set values for the verification request
                verificationRequest.Method = "POST";
                verificationRequest.ContentType = "application/x-www-form-urlencoded";
                var param = Request.BinaryRead(ipnRequest.ContentLength);
                var 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());
                var verificationResponse = streamIn.ReadToEnd();
                streamIn.Close();

                var transactionIdentifier = memo.Split(':')[1].Trim();

                //_logger.Info($"strRequest: {strRequest}");
                //_logger.Info($"verificationResponse: {verificationResponse}");

                // We receive 2 messages from PayPal.  Only complete this for one...
                if (verificationResponse.Equals("VERIFIED"))
                {
                    if (strRequest.Contains("payment_type=instant"))
                    {
                        _paymentManager.CompleteTransaction(transactionIdentifier);
                        _logger.Info($"IPNController.VerifyTask.  Payment marked as 'Paid'. transactionIdentifier={transactionIdentifier}");
                    }
                }
                else
                {
                    _logger.Warn($"IPNController.VerifyTask.  A non-verified request was received.  transactionIdentifier={transactionIdentifier}");
                }
            }
            catch (Exception ex)
            {
                _logger.Error("IPNController.VerifyTask", ex);
            }
        }
    }

    public class PayPalCheckoutInfo
    {
        public string Memo { get; set; }

        //mc_gross=6.15
        //protection_eligibility=Ineligible
        //payer_id=ZJ93C8BT7HYE4
        //tax=0.00
        //payment_date=21:09:26 Jan 26, 2016 PST
        //payment_status=Completed
        //charset=windows-1252
        //first_name=Sandbox
        //mc_fee=0.48
        //notify_version=3.8
        //custom=
        //payer_status=verified
        //business=developer+application @trytn.com
        //quantity= 0
        //verify_sign = A8RQ0F8gkUzMctcqZ4r9aZzwD7JUA2ltLngw8Dny8kkzavsf9M8bRfZ3
        // payer_email = developer + merchant@trytn.com
        //memo= Trytn
        //txn_id = 52W35468KJ348570R
        //payment_type= instant
        //payer_business_name = Sandbox Merchant's Test Store
        //last_name= Merchant
        //receiver_email = developer + application@trytn.com
        //payment_fee= 0.48
        //receiver_id = VMLFKLT4VDZQL
        //txn_type = web_accept
        //item_name =
        //mc_currency = USD
        //item_number =
        //residence_country = US
        //test_ipn = 1
        //transaction_subject =
        //payment_gross = 6.15
        //ipn_track_id = 245bfe354148e
    }

原始来源:https://github.com/paypal/ipn-code-samples/pull/31/files

关于c# - 使用空数据验证 IPN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576043/

相关文章:

c# - BundleTable.EnableOptimizations 不工作 asp mvc

C# <exception> 标签不显示

jQuery 模板是 jQuery 核心库的一部分吗?

paypal - 2013 年使用 paypal 进行自定义结帐的正确方法?

php - 带有 REST PHP 的 PayPal 返回错误 400

c# - SocketIoClientDotNet - Socket.IO 客户端库有时会发出两个连续的事件,有时不会

c# - 异步等待调用不返回

c# - MVC 复杂模型与继承、嵌套 View 模型和部分 View 绑定(bind)

c# - 实现 asp.net mvc 聊天全双工

c# - PayPal Error while Make Payment Using card 10752 错误消息