.NETCore(微信、支付宝支付异步回调通知)

  public ActionResult WxPayNotify()
        {
            log.AddPayLog("WxPayNotify", "开始调用接口");
            string returnCode = "FAIL";
            string returnMsg = "FAIL";
            string result = "";
            try
            {
                log.AddPayLog("WxPayNotify", "--------微信回调--开始--------");
                NLoggerHelper.Default.Info("WxPayNotify---微信回调--开始");
                ResponseHandler resHandler = new ResponseHandler(HttpContext);
                WxPayNotify notify = new WxPayNotify(resHandler);
                NLoggerHelper.Default.Info("WxPayNotify---微信回调--通知参数---" + notify.ToJson());
                log.AddPayLog("WxPayNotify", "--------微信回调--通知参数--------" + notify.ToJson());
                int tenantId = 0;
                if (!notify.Attach.IsNullOrEmpty() && notify.Attach.Split('-').Length > 2)
                {
                    tenantId = notify.Attach.Split('-')[1].ToInt32();
                }

                bool safe = notify.IsSafe();
                if (safe)
                {
                    if (notify.ResultCode.ToLower() == "success")
                    {
                        string orderType = string.Empty;
                        if (!notify.Attach.IsNullOrEmpty())
                        {
                            string[] arr = notify.Attach.Split('-');
                            if (arr.Length >= 2)
                            {
                                orderType = arr[0];
                            }
                        }

                        if (orderType == PayOrderType.RechargeOrder.ToString())
                        {
                            //充值
                            decimal tradeAmount = notify.TotalFee / 100m;
                            bool success = HandleRechargeOrderNotify(notify.OutTradeNo, notify.TransactionId, tradeAmount, notify.TimeEnd, PayWay.WxPay, notify.ToJson());
                            if (success)
                            {
                                returnCode = "SUCCESS";
                                returnMsg = "OK";
                            }
                        }
                        else
                        {
                            NLoggerHelper.Default.Info("WxPayNotify---微信回调--未知订单类型");
                            log.AddPayLog("WxPayNotify", "--------微信回调--未知订单类型--------");
                        }

                    }
                    else
                    {
                        NLoggerHelper.Default.Info("WxPayNotify---微信回调--交易失败");
                        log.AddPayLog("WxPayNotify", "--------微信回调--交易失败--------");
                    }
                }
                else
                {
                    NLoggerHelper.Default.Info("WxPayNotify---微信回调--参数安全认证失败");
                    log.AddPayLog("WxPayNotify", "--------微信回调--参数安全认证失败--------");
                }

                result = string.Format(result, returnCode, returnMsg);
                NLoggerHelper.Default.Info("WxPayNotify---微信回调--结束:" + result);
                log.AddPayLog("WxPayNotify", "--------微信回调--结束--------" + result);
            }
            catch (Exception ex)
            {
                NLoggerHelper.Default.Info("WxPayNotify---微信回调--异常:" + ex.ToString());
                log.AddPayLog("WxPayNotify", "--------微信回调--异常:" + ex.ToString());
            }
            return Content(result, "text/xml");
        }
public ActionResult AliPayNotify()
        {
            string result = "fail";
            try
            {
                NLoggerHelper.Default.Info("AliPayNotify---支付宝回调---开始");
                log.AddPayLog("AliPayNotify", "--------支付宝回调--开始--------");

                var httpRequest = HttpContext.Request;
                AliPayNotify notify = new AliPayNotify(httpRequest);
                NLoggerHelper.Default.Info("AliPayNotify---支付宝回调---通知参数---" + notify.ToJson());
                log.AddPayLog("AliPayNotify", "--------支付宝回调--通知参数--------" + notify.ToJson());
                if (notify.IsSafe)
                {
                    //交易支付成功
                    if (notify.TradeStatus == AliPayTradeStatus.TRADE_SUCCESS.ToString())
                    {
                        string orderType = string.Empty;
                        if (!notify.PassbackParams.IsNullOrEmpty())
                        {
                            string[] arr = notify.PassbackParams.Split('-');
                            if (arr.Length >= 2)
                            {
                                orderType = arr[0];
                            }
                        }
                        else
                        {
                            string[] arr = notify.Body.Split('-');
                            if (arr.Length >= 2)
                            {
                                orderType = arr[0];
                            }
                        }

                        if (orderType == PayOrderType.RechargeOrder.ToString())
                        {
                            //充值
                            bool success = HandleRechargeOrderNotify( notify.OutTradeNo, notify.TradeNo, notify.TotalAmount, notify.GmtPayment, PayWay.AliPay, notify.ToJson());
                            if (success)
                            {
                                result = "success";
                            }
                        }
                        else
                        {
                            NLoggerHelper.Default.Info("AliPayNotify---支付宝回调--未知订单类型");
                            log.AddPayLog("AliPayNotify", "--------支付宝回调--未知订单类型--------");
                        }
                    }
                    else
                    {
                        NLoggerHelper.Default.Info("AliPayNotify---支付宝回调--交易失败");
                        log.AddPayLog("AliPayNotify", "--------支付宝回调--交易失败--------");
                    }
                }
                else
                {
                    NLoggerHelper.Default.Info("AliPayNotify---支付宝回调--参数安全认证失败");
                    log.AddPayLog("AliPayNotify", "--------支付宝回调--参数安全认证失败--------");
                }
                log.AddPayLog("AliPayNotify", "--------支付宝回调--结束--------" + result);
            }
            catch (Exception ex)
            {
                NLoggerHelper.Default.Info("AliPayNotify---支付宝回调--异常:" + ex.ToString());
            }
            return Content(result);
        }

相关类:

public class AliPayNotify
    {
        HttpRequest request = null;

        public AliPayNotify(HttpRequest httpRequest)
        {
            request = httpRequest;
        }

        public bool IsSafe {
            get {
                bool safe = false;

                //验证商户
                if (AppId == AliPay.AppId)
                {
                    //获取当前请求中的post参数
                    var dict = new Dictionary();
                    var keys = request.Form.Keys;
                    if (keys != null)
                    {
                        foreach (string key in keys)
                        {
                            dict.Add(key, request.Form[key]);
                        }
                    }

                    if (dict.Count > 0)
                    {
                        safe = AlipaySignature.RSACheckV1(dict, AliPay.AliPublicKey, AliPay.Charset, AliPay.SignType, false);
                    }
                }

                return safe;
            }
        }

        #region 通知参数
        /// 
        /// 通知时间
        /// 
        public DateTime NotifyTime {
            get {
                return Convert.ToDateTime(request.Form["notify_time"]);
            }
        }
        /// 
        /// 通知类型
        /// 
        public string NotifyType {
            get {
                return request.Form["notify_type"];
            }
        }
        /// 
        /// 通知校验id
        /// 
        public string NotifyId {
            get {
                return request.Form["notify_id"];
            }
        }
        /// 
        /// 支付宝分配给开发者的应用Id
        /// 
        public string AppId {
            get {
                return request.Form["app_id"];
            }
        }
        /// 
        /// 编码格式,如utf-8、gbk、gb2312等
        /// 
        public string Charset {
            get {
                return request.Form["charset"];
            }
        }
        /// 
        /// 调用的接口版本
        /// 
        public string Version {
            get {
                return request.Form["version"];
            }
        }
        /// 
        /// 签名类型
        /// 
        public string SignType {
            get {
                return request.Form["sign_type"];
            }
        }
        /// 
        /// 签名
        /// 
        public string Sign {
            get {
                return request.Form["sign"];
            }
        }
        /// 
        /// 支付宝交易号
        /// 
        public string TradeNo {
            get {
                return request.Form["trade_no"];
            }
        }
        /// 
        /// 商户订单号
        /// 
        public string OutTradeNo {
            get {
                return request.Form["out_trade_no"];
            }
        }
        /// 
        /// 商户业务号,主要是退款通知中返回退款申请的流水号
        /// 
        public string OutBizNo {
            get {
                return request.Form["out_biz_no"];
            }
        }
        /// 
        /// 买家支付宝用户号
        /// 
        public string BuyerId {
            get {
                return request.Form["buyer_id"];
            }
        }
        /// 
        /// 买家支付宝账号
        /// 
        public string BuyerLogonId {
            get {
                return request.Form["buyer_logon_id"];
            }
        }
        /// 
        /// 卖家支付宝用户号
        /// 
        public string SellerId {
            get {
                return request.Form["seller_id"];
            }
        }
        /// 
        /// 卖家支付宝账号
        /// 
        public string SellerEmail {
            get {
                return request.Form["seller_email"];
            }
        }
        /// 
        /// 交易状态 WAIT_BUYER_PAY 交易创建,等待买家付款, TRADE_CLOSED 未付款交易超时关闭,或支付完成后全额退款, TRADE_SUCCESS 交易支付成功, TRADE_FINISHED 交易结束,不可退款
        /// 
        public string TradeStatus {
            get {
                return request.Form["trade_status"];
            }
        }
        /// 
        /// 订单金额(元)
        /// 
        public decimal TotalAmount {
            get {
                return string.IsNullOrEmpty(request.Form["total_amount"]) ? 0 : Convert.ToDecimal(request.Form["total_amount"]);
            }
        }
        /// 
        /// 实收金额 商家在交易中实际收到的款项,单位为元
        /// 
        public decimal ReceiptAmount {
            get {
                return string.IsNullOrEmpty(request.Form["receipt_amount"]) ? 0 : Convert.ToDecimal(request.Form["receipt_amount"]);
            }
        }
        /// 
        /// 开票金额 用户在交易中支付的可开发票的金额
        /// 
        public decimal InvoiceAmount {
            get {
                return string.IsNullOrEmpty(request.Form["invoice_amount"]) ? 0 : Convert.ToDecimal(request.Form["invoice_amount"]);
            }
        }
        /// 
        /// 付款金额 用户在交易中支付的金额
        /// 
        public decimal BuyerPayAmount {
            get {
                return string.IsNullOrEmpty(request.Form["buyer_pay_amount"]) ? 0 : Convert.ToDecimal(request.Form["buyer_pay_amount"]);
            }
        }
        /// 
        /// 集分宝金额 使用集分宝支付的金额
        /// 
        public decimal PointAmount {
            get {
                return string.IsNullOrEmpty(request.Form["point_amount"]) ? 0 : Convert.ToDecimal(request.Form["point_amount"]);
            }
        }
        /// 
        /// 总退款金额
        /// 
        public decimal RefundFee {
            get {
                return string.IsNullOrEmpty(request.Form["refund_fee"]) ? 0 : Convert.ToDecimal(request.Form["refund_fee"]);
            }
        }
        /// 
        /// 订单标题
        /// 
        public string Subject {
            get {
                return request.Form["subject"];
            }
        }
        /// 
        /// 商品描述
        /// 
        public string Body {
            get {
                return request.Form["body"];
            }
        }
        /// 
        /// 交易创建时间 该笔交易创建的时间。格式为yyyy-MM-dd HH:mm:ss
        /// 
        public DateTime GmtCreate {
            get {
                return string.IsNullOrEmpty(request.Form["gmt_create"]) ? DateTime.MinValue : Convert.ToDateTime(request.Form["gmt_create"]);
            }
        }
        /// 
        /// 交易付款时间 该笔交易的买家付款时间。格式为yyyy-MM-dd HH:mm:ss
        /// 
        public DateTime GmtPayment {
            get {
                return string.IsNullOrEmpty(request.Form["gmt_payment"]) ? DateTime.MinValue : Convert.ToDateTime(request.Form["gmt_payment"]);
            }
        }
        /// 
        /// 交易退款时间 该笔交易的退款时间。格式为yyyy-MM-dd HH:mm:ss.S
        /// 
        public DateTime GmtRefund {
            get {
                return string.IsNullOrEmpty(request.Form["gmt_refund"]) ? DateTime.MinValue : Convert.ToDateTime(request.Form["gmt_refund"]);
            }
        }
        /// 
        /// 交易结束时间 该笔交易结束时间。格式为yyyy-MM-dd HH:mm:ss
        /// 
        public DateTime GmtClose {
            get {
                return string.IsNullOrEmpty(request.Form["gmt_close"]) ? DateTime.MinValue : Convert.ToDateTime(request.Form["gmt_close"]);
            }
        }
        /// 
        /// 支付金额信息 支付成功的各个渠道金额信息  [{“amount”:“15.00”,“fundChannel”:“ALIPAYACCOUNT”}]
        /// 
        public string FundBillList {
            get {
                return request.Form["fund_bill_list"];
            }
        }
        /// 
        /// 回传参数,如果请求时传递了该参数,则返回给商户时会在异步通知时将该参数原样返回。
        /// 
        public string PassbackParams {
            get {
                return HttpUtility.UrlDecode(request.Form["passback_params"]);
            }
        }
        /// 
        /// 优惠券信息 本交易支付时所使用的所有优惠券信息 
        /// 
        public string VoucherDetailList {
            get {
                return request.Form["voucher_detail_list"];
            }
        }
        #endregion

    }

你可能感兴趣的:(.NETCore(微信、支付宝支付异步回调通知))