c# - PayPal 支付标准从移动设备返回 GET 而不是 POST,因此无法验证记录支付

标签 c# asp.net paypal payment-gateway

几年前我已经集成了 PayPal 支付标准来获取支付。

我的应用程序在 Asp.Net 中。目前处于沙盒模式。

我的网站上有一个“立即付款”按钮,其中包含指向 PayPal 网站的 PostBackURL 以及所有必需的参数。当用户点击按钮时,它会重定向到 PayPal,他们的用户可以通过他的帐户或借记卡/信用卡付款。在成功的交易中,用户被发送回我的应用程序。当用户返回我的应用程序时,我会在 Request.Form 集合中获得各种参数,例如“payment_status”。我验证响应并相应地显示成功/失败消息。

当用户使用桌面浏览器时,上述流程运行良好。

但是当用户使用移动设备并使用移动浏览器时。用户登陆 PayPal 移动友好页面。用户用他的帐户付款。然后显示成功消息。但是当用户被重定向到我的应用程序时,我没有在 Request.Form 集合中获得任何值。因此,我无法验证来自 PayPal 的响应。

此外,我还了解到,在桌面浏览器上,PayPal 通过 POST 方法向我的网站返回响应,因此 Request.Form 包含数据。

而对于移动浏览器,PayPal 通过 GET 方法返回响应,因此 Request.Form 不包含任何数据。

为什么 PayPal 通过 GET 返回响应?在这种情况下,即使在 querystring 中也没有数据,那么我该如何验证响应,是否支付成功?

我已阅读文档,它说没有其他针对移动 PayPal 支付标准的特定设置。

我不想迁移到快速结帐或任何其他配置。

此外,我已经搜索了很多与 SO 相关的线程,但没有找到满足我需要的任何合适的解决方案,因此作为一个新问题提出。

最佳答案

我在我的 Web API Asp.NET 项目中为 PayPal 做了这样的工作..希望它对你有帮助:

[AllowAnonymous]
        [HttpPost]
        [Route("api/PayPal/IPN")]
        [ResponseType(typeof(OrderPayPalDTO))]
        public async Task<IHttpActionResult> PayPalIPN()
        {
            try
            {
                decimal tot;

                var data = this.Request.Content.ReadAsStringAsync().Result;
                if (data == null) return BadRequest();

                // Parse the query string variables into a NameValueCollection.
                NameValueCollection qscoll = HttpUtility.ParseQueryString(data);
                PayPalViewModel payPalModel = new PayPalViewModel();

                var payPal = payPalModel.ToPayPalVM(qscoll); //HRE IS A EXTENSION METHOD TO MAP to a CLASS

                if (payPal == null) return InternalServerError(new Exception());

                //Try cast total from paypal
                if (!decimal.TryParse(payPal.mc_gross, out tot)) return InternalServerError(new Exception(Constant.Error.PAYMENT_ERROR_TOTAL_CAST));




                // If status is Ok /or Completed
                if (payPal.payment_status.Equals(Constant.PaymentStatus.PAYED) || payPal.payment_status.Equals(Constant.PaymentStatus.COMPLETED))
                {
                    // update payment
                    bool ok = await this.UpdatePayment(order, user);
                    if (!ok) return InternalServerError(new Exception(Constant.Error.PAYMENT_ERROR_UPDATE));
                }

                return Ok(order);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return (Constant.CONFIGURATION_GLOBALS.IS_DEVELOPMENT_MODE)
                  ? InternalServerError(ex)
                  : InternalServerError(new Exception(Constant.Error.ERROR_GENERIC_500));
            }
        }

还有我的映射器和类 PayPal ViewModel

  public class PayPalViewModel
    {
        public string mc_gross { get; set; }
        public string custom { get; set; }
        public string payment_status { get; set; }

        public string payment_type { get; set; }
        public string mc_currency { get; set; }

        public string payer_id { get; set; }
        public DateTime payment_date { get; set; }
        public string payment_gross { get; set; }

        /// <summary>
        /// ToPayPalVM From NameValueCollection
        /// </summary>
        /// <returns></returns>
        public PayPalViewModel ToPayPalVM(NameValueCollection qscoll)
        {
            if (qscoll == null) return null;
            DateTime date = DateTime.Now;
            string mcGross = qscoll["mc_gross"];
            string paymentType = qscoll["payment_type"];
            string mcCurrency = qscoll["mc_currency"];
            string paymentStatus = qscoll["payment_status"];
            string payerId = qscoll["payer_id"];
            string paymentDate = qscoll["payment_date"];
            string paymentGross = qscoll["payment_gross"];
            string cust = qscoll["custom"];

            var datePay = DateTime.TryParse(paymentDate, out date);

            return new PayPalViewModel
            {
                mc_gross = mcGross,
                custom = cust,
                payment_status = paymentStatus,
                payment_type = paymentType,
                mc_currency = mcCurrency,
                payer_id = payerId,
                payment_gross = paymentGross,
                payment_date = (datePay) ? date : DateTime.Now
            };
        }
    }

关于c# - PayPal 支付标准从移动设备返回 GET 而不是 POST,因此无法验证记录支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312414/

相关文章:

c# - 检查用户名是否已存在于数据库中

c# - 创建一个集中的异常日志系统

c# - 处理您的请求时发生错误。 MVC 4

c# - 如何在回发后执行按钮单击以打开模式窗口

asp.net - WCF 说它超过了最大查询字符串值,但实际上没有

php - Paypal IPN 监听器代码挂起/无响应

android - BraintreeBrowserSwitchActivity 丢失错误

c# - 事件记录 IPAddress 并不总能解析

c# - 嵌套循环中的循环优化 c#

Android 链接到外部商店