Ejemplo 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();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建vip用户及上下级关系
        /// </summary>
        /// <param name="vUserID"></param>
        /// <param name="inviteCode"></param>
        /// <returns></returns>
        public RequestResult CreateVipRelation(string vUserID, string inviteCode)
        {
            #region
            RequestResult result = new RequestResult()
            {
                retCode = ReqResultCode.failed,
                retMsg  = "计算佣金失败"
            };
            #region 数据校验
            if (string.IsNullOrWhiteSpace(vUserID))
            {
                result.retMsg = "操作用户ID不能为空";
                return(result);
            }
            #endregion
            int rows = 0;
            try
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Vip uVip = db.Vips.FirstOrDefault(p => p.UserID == vUserID);
                    if (uVip == null)
                    {
                        CardPersonal card = db.CardPersonals.FirstOrDefault(p => p.UserID == vUserID);
                        if (card != null)
                        {
                            uVip = new Vip()
                            {
                                Amount               = 0,
                                CardID               = card.ID,
                                CreateDateTime       = DateTime.Now,
                                FreeChildCount       = 0,
                                State                = Common.Enums.VipState.Enable,
                                TotalAmount          = 0,
                                TotalMonthAmountRank = 0,
                                TotalAmountRank      = 0,
                                TotalWeekAmountRank  = 0,
                                UserID               = vUserID,
                                Type             = Common.Enums.VipRank.Default,
                                VipChild2ndCount = 0,
                                VipChild3rdCount = 0
                            };
                            db.Vips.Add(uVip);
                            rows = db.SaveChanges();
                            if (rows <= 0)
                            {
                                result.retMsg = "创建vip用户失败";
                                return(result);
                            }
                        }
                        else
                        {
                            result.retMsg = "尚未创建该用户的名片";
                            return(result);
                        }
                    }
                    //有邀请码则建立关系
                    if (!string.IsNullOrEmpty(inviteCode))
                    {
                        Vip inviteVip = db.Vips.FirstOrDefault(p => p.Code == inviteCode);
                        if (inviteVip != null)
                        {
                            VipRelationship relation = db.VipRelationships.FirstOrDefault(p => p.UserID == vUserID && p.ParentUserID == inviteVip.UserID);
                            if (relation == null)
                            {
                                relation = new VipRelationship()
                                {
                                    CreateDateTime = DateTime.Now,
                                    ParentID       = inviteVip.ID,
                                    ParentUserID   = inviteVip.UserID,
                                    VipID          = uVip.ID,
                                    UserID         = vUserID
                                };
                                db.VipRelationships.Add(relation);
                                rows = db.SaveChanges();
                                if (rows <= 0)
                                {
                                    result.retMsg = "创建vip用户与上级关系时失败";
                                    return(result);
                                }
                            }
                        }
                    }
                }
                //计算佣金
                RequestResult ret = bll.CalculateVIPAmount(vUserID, 0);
                if (ret.retCode != ReqResultCode.success)
                {
                    result.retMsg = ret.retMsg;
                    return(result);
                }
            }
            catch (Exception ex)
            {
                result.retMsg = $"创建vip用户关系时发生异常:{ex.Message}";
                return(result);
            }

            result.retCode = ReqResultCode.success;
            result.retMsg  = "操作成功";
            return(result);

            #endregion
        }