Esempio n. 1
0
        /// <summary>
        /// 接收返回的结果,根据结果处理订单状态
        /// </summary>
        public override void ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();

            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                Log.Debug("notifyData", $"notifyData:完成");
                //若transaction_id不存在,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();
            string out_trade_no   = notifyData.GetValue("out_trade_no").ToString();

            //查询订单,判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            //查询订单成功
            else
            {
                int row = 0;
                #region 更改订单状态,保存支付结果
                if (!string.IsNullOrEmpty(out_trade_no))
                {
                    using (ApplicationDbContext db = new ApplicationDbContext())
                    {
                        Order order = db.Orders.FirstOrDefault(p => p.Code == out_trade_no && p.State == Common.Enums.OrderState.UnHandle);
                        if (order != null)
                        {
                            order.Amount      = Convert.ToDecimal(notifyData.GetValue("total_fee").ToString()) / 100m;
                            order.PayCode     = transaction_id;
                            order.PayResult   = notifyData.ToJson();
                            order.State       = notifyData.GetValue("result_code").ToString() == "SUCCESS" ? Common.Enums.OrderState.Success : Common.Enums.OrderState.Failed;
                            order.PayDateTime = DateTime.Now;
                            row = db.SaveChanges();
                            if (row > 0 && order.State == Common.Enums.OrderState.Success)
                            {
                                var vip = db.Vips.FirstOrDefault(s => s.UserID == order.UserID);
                                vip.Type  = Common.Enums.VipRank.Vip99;
                                vip.State = Common.Enums.VipState.Enable;
                                vip.Code  = Bll.VipBLL.RandomCode();
                                db.SaveChanges();
                                //查看是否需要计算上级收益
                                VIPAccountBLL bll = new VIPAccountBLL();
                                bll.CalculateVIPAmount(order.UserID, 1);
                            }
                        }
                    }
                }
                #endregion
                if (row > 0)
                {
                    WxPayData res = new WxPayData();
                    res.SetValue("return_code", "SUCCESS");
                    res.SetValue("return_msg", "OK");
                    Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
                    page.Response.Write(res.ToXml());
                    page.Response.End();
                }
                else
                {//如果没更新成功,就继续接收通知
                    WxPayData res = new WxPayData();
                    res.SetValue("return_code", "FAIL");
                    res.SetValue("return_msg", "未接收成功");
                    Log.Error(this.GetType().ToString(), "The recieve result is error : " + res.ToXml());
                    page.Response.Write(res.ToXml());
                    page.Response.End();
                }
            }
        }