Example #1
0
        public bool UpdateAgentRuote(int route_id, float discount, bool enabled)
        {
            if (!CurrentLoginUser.Permission.UPDATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限更新代理商路由");
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Agent_route route = (from r in db.Agent_route where r.Id == route_id select r).FirstOrDefault <Agent_route>();
                if (route == null)
                {
                    throw new KMBitException("此路由套餐不存在");
                }
                if (discount <= 0 || discount > 1)
                {
                    throw new KMBitException("折扣不能等于0或者大于1,必须介于0-1之间,可以等于1");
                }
                route.Discount    = discount;
                route.Enabled     = enabled;
                route.UpdatedBy   = CurrentLoginUser.User.Id;
                route.Update_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #2
0
        public bool CreateRoute(Agent_route route)
        {
            if (route == null)
            {
                throw new KMBitException("路由信息不能为空");
            }
            if (route.User_id <= 0)
            {
                throw new KMBitException("代理商信息不能为空");
            }
            if (route.Resource_Id <= 0)
            {
                throw new KMBitException("落地资源信息不能为空");
            }
            if (route.Resource_taocan_id <= 0)
            {
                throw new KMBitException("落地资源套餐不能为空");
            }
            if (route.Discount <= 0 || route.Discount > 1)
            {
                throw new KMBitException("代理商路由(资源套餐)折扣必须在0-1之间");
            }
            if (!CurrentLoginUser.Permission.CREATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限创建代理商路由");
            }
            if (route.Create_time <= 0)
            {
                route.Create_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            }
            if (route.CreatedBy <= 0)
            {
                route.CreatedBy = CurrentLoginUser.User.Id;
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Resource_taocan taocan = (from t in db.Resource_taocan where t.Resource_id == route.Resource_Id && t.Id == route.Resource_taocan_id select t).FirstOrDefault <Resource_taocan>();
                if (taocan == null || !taocan.Enabled)
                {
                    return(ret);
                }
                if (route.Sale_price == 0)
                {
                    route.Sale_price = taocan.Sale_price;
                }
                db.Agent_route.Add(route);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #3
0
        public bool CreateRoute(Agent_route route)
        {
            if (route == null)
            {
                throw new KMBitException("路由信息不能为空");
            }
            if (route.User_id <= 0)
            {
                throw new KMBitException("代理商信息不能为空");
            }
            if (route.Resource_Id <= 0)
            {
                throw new KMBitException("落地资源信息不能为空");
            }
            if (route.Resource_taocan_id <= 0)
            {
                throw new KMBitException("落地资源套餐不能为空");
            }
            if (route.Discount <= 0 || route.Discount > 1)
            {
                throw new KMBitException("代理商路由(资源套餐)折扣必须在0-1之间");
            }
            if (!CurrentLoginUser.Permission.CREATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限创建代理商路由");
            }
            if (route.Create_time <= 0)
            {
                route.Create_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            }
            if (route.CreatedBy <= 0)
            {
                route.CreatedBy = CurrentLoginUser.User.Id;
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                db.Agent_route.Add(route);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #4
0
        public bool UpdateTaocanPrice(int routeId, float price, bool enable)
        {
            bool result = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Agent_route route = (from r in db.Agent_route where r.Id == routeId select r).FirstOrDefault <Agent_route>();
                if (route == null)
                {
                    throw new KMBitException(string.Format("编号为{0}的套餐不存在", routeId.ToString()));
                }

                if (route.User_id != CurrentLoginUser.User.Id)
                {
                    throw new KMBitException(string.Format("编号为{0}的套餐不属于你,不能修改套餐信息", routeId.ToString()));
                }

                route.Sale_price = price;
                route.Enabled    = enable;
                db.SaveChanges();
                result = true;
            }
            return(result);
        }
Example #5
0
        public ChargeOrder GenerateOrder(ChargeOrder order)
        {
            if (order == null)
            {
                throw new KMBitException("订单对象不能为NULL");
            }
            if (string.IsNullOrEmpty(order.MobileNumber))
            {
                throw new KMBitException("充值的手机号码不能为空");
            }
            chargebitEntities db = null;

            try
            {
                db = new chargebitEntities();
                Marketing_Orders          mOrder   = null;
                Marketing_Activity_Taocan mTaocan  = null;
                Marketing_Activities      activity = null;
                if (order.IsMarket && order.MarketOrderId > 0)
                {
                    //if (string.IsNullOrEmpty(order.MacAddress))
                    //{
                    //    throw new KMBitException("活动充值时必须获取终端的MAC地址");
                    //}
                    mOrder = (from o in db.Marketing_Orders where o.Id == order.MarketOrderId select o).FirstOrDefault <Marketing_Orders>();
                    if (mOrder == null)
                    {
                        throw new KMBitException(string.Format("编号为{0}的活动充值记录不存在", order.MarketOrderId));
                    }
                    if (mOrder.Used)
                    {
                        throw new KMBitException("此二维码已经只用过,不能重复使用");
                    }
                    mTaocan = (from m in db.Marketing_Activity_Taocan where m.Id == mOrder.ActivityTaocanId select m).FirstOrDefault <Marketing_Activity_Taocan>();
                    if (mTaocan == null)
                    {
                        throw new KMBitException(string.Format("编号为{0}的活动充值记录的套餐信息不存在", order.MarketOrderId));
                    }

                    activity = (from a in db.Marketing_Activities where a.Id == mTaocan.ActivityId select a).FirstOrDefault <Marketing_Activities>();
                    if (activity == null)
                    {
                        throw new KMBitException(string.Format("编号为{0}的活动不存在", mTaocan.ActivityId));
                    }
                    //check if the device or the number is already charged
                    if (activity.ScanType == 1)
                    {
                        int[] acos = (from mo in db.Marketing_Orders where mo.ActivityId == activity.Id select mo.Id).ToArray <int>();
                        if (acos == null || acos.Length <= 0)
                        {
                            throw new KMBitException(string.Format("编号为{0}的活动还没有任何可用的充值套餐", mTaocan.ActivityId));
                        }
                        //验证电话号码是否在某个直扫活动里已经成功充值过
                        int count = (from o in db.Charge_Order where o.Phone_number == order.MobileNumber && o.Status != 3 && acos.Contains(o.MarketOrderId) select o.Id).Count();
                        if (count > 0)
                        {
                            throw new KMBitException("同一次营销活动每个手机每个号码只能扫描一次");
                        }
                    }

                    order.ResourceId       = mTaocan.ResourceId;
                    order.ResourceTaocanId = mTaocan.ResourceTaocanId;
                    order.RouteId          = mTaocan.RouteId;
                    if (order.AgencyId == 0)
                    {
                        order.AgencyId = (from a in db.Marketing_Activities
                                          where a.Id == mTaocan.ActivityId
                                          select a.AgentId
                                          ).FirstOrDefault <int>();
                    }
                }
                Resource_taocan taocan = null;
                if (order.ResourceTaocanId <= 0 && order.RouteId > 0)
                {
                    var query = from ta in db.Agent_route
                                join tc in db.Resource_taocan on ta.Resource_taocan_id equals tc.Id
                                where ta.Id == order.RouteId
                                select tc;
                    taocan = query.FirstOrDefault <Resource_taocan>();
                }
                else if (order.ResourceTaocanId > 0)
                {
                    taocan = (from tc in db.Resource_taocan where tc.Id == order.ResourceTaocanId select tc).FirstOrDefault <Resource_taocan>();
                }
                if (!taocan.Enabled)
                {
                    throw new KMBitException(ChargeConstant.RESOURCE_TAOCAN_DISABLED);
                }
                if (taocan == null)
                {
                    throw new KMBitException("套餐信息不存在,不能充值");
                }
                order.ResourceTaocanId = taocan.Id;
                KMBit.DAL.Resource resource = (from ri in db.Resource
                                               join tr in db.Resource_taocan on ri.Id equals tr.Resource_id
                                               where tr.Id == order.ResourceTaocanId
                                               select ri).FirstOrDefault <Resource>();

                if (resource == null)
                {
                    throw new KMBitException("落地资源不存在");
                }
                if (!resource.Enabled)
                {
                    throw new KMBitException(ChargeConstant.RESOURCE_DISABLED);
                }
                Agent_route route = null;
                if (order.AgencyId > 0 && order.RouteId > 0)
                {
                    route = (from r in db.Agent_route where r.Id == order.RouteId select r).FirstOrDefault <Agent_route>();
                    if (route == null)
                    {
                        throw new KMBitException("代理商路由不存在");
                    }

                    if (route.User_id != order.AgencyId)
                    {
                        throw new KMBitException("当前代理商没有此路由");
                    }

                    if (!route.Enabled)
                    {
                        throw new KMBitException(string.Format("编号为{0}代理商路由已被管理员停用", route.Id));
                    }
                }

                Charge_Order history = new Charge_Order();
                history.Agent_Id            = order.AgencyId;
                history.Completed_Time      = 0;
                history.Created_time        = order.CreatedTime > 0 ? order.CreatedTime : DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                history.Operate_User        = order.OperateUserId;
                history.Out_Order_Id        = "";
                history.Payed               = order.Payed;
                history.Phone_number        = order.MobileNumber;
                history.MobileSP            = order.MobileSP;
                history.Province            = order.Province;
                history.City                = order.City;
                history.Resource_id         = resource.Id;
                history.Resource_taocan_id  = order.ResourceTaocanId;
                history.RuoteId             = order.RouteId;
                history.Status              = 11;
                history.CallBackUrl         = order.CallbackUrl != null ? order.CallbackUrl : null;
                history.Platform_Sale_Price = taocan.Sale_price;
                if (taocan.EnableDiscount)
                {
                    history.Platform_Cost_Price = taocan.Purchase_price * taocan.Resource_Discount;
                }
                else
                {
                    history.Platform_Cost_Price = taocan.Purchase_price;
                }

                if (order.IsMarket && order.MarketOrderId > 0)
                {
                    //营销充值
                    history.Charge_type    = 1;
                    history.MarketOrderId  = order.MarketOrderId;
                    history.Sale_price     = mTaocan.Price;
                    history.Purchase_price = history.Platform_Sale_Price * route.Discount;
                    history.Payed          = true;
                }
                else
                {
                    if (order.AgencyId > 0)
                    {
                        //代理商充值
                        history.Charge_type    = 1;
                        history.Purchase_price = taocan.Sale_price * route.Discount;
                        history.Sale_price     = route.Sale_price;
                        history.Revenue        = history.Purchase_price - history.Platform_Cost_Price;
                    }
                    else if (order.OperateUserId > 0)
                    {
                        //管理员后台充值
                        history.Charge_type    = 2;
                        history.Purchase_price = taocan.Sale_price;
                        history.Sale_price     = taocan.Sale_price;
                        history.Revenue        = history.Platform_Sale_Price - history.Platform_Cost_Price;
                    }
                    else
                    {
                        //前台用户直冲
                        history.Charge_type    = 0;
                        history.Purchase_price = taocan.Sale_price;
                        history.Sale_price     = taocan.Sale_price;
                        history.Revenue        = history.Platform_Sale_Price - history.Platform_Cost_Price;
                    }
                }

                db.Charge_Order.Add(history);
                db.SaveChanges();
                order.Id = history.Id;

                //Create Payment record for direct charge
                if (history.Charge_type == 0)
                {
                    Payment_history payment = new Payment_history()
                    {
                        PaymentAccount = "", Amount = taocan.Sale_price, ChargeOrderId = history.Id, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), PayType = 0, Tranfser_Type = 1
                    };
                    db.Payment_history.Add(payment);
                    db.SaveChanges();
                    order.PaymentId = payment.Id;
                }
            }catch (Exception ex)
            {
                throw new KMBitException(ex.Message);
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }

            return(order);
        }
Example #6
0
        public virtual void ProceedOrder(ChargeOrder order, out ChargeResult result, bool isSOAPAPI = false)
        {
            lock (o)
            {
                Logger.Info("Processing order...");
                Logger.Info("OrderId:" + order.Id);
                result = new ChargeResult();
                using (chargebitEntities db = new chargebitEntities())
                {
                    if (!isSOAPAPI)
                    {
                        KMBit.DAL.Resrouce_interface rInterface = (from ri in db.Resrouce_interface where ri.Resource_id == order.ResourceId select ri).FirstOrDefault <Resrouce_interface>();
                        if (rInterface == null)
                        {
                            result.Status  = ChargeStatus.FAILED;
                            result.Message = ChargeConstant.RESOURCE_INTERFACE_NOT_CONFIGURED;
                            return;
                        }

                        if (string.IsNullOrEmpty(rInterface.APIURL))
                        {
                            result.Status  = ChargeStatus.FAILED;
                            result.Message = ChargeConstant.RESOURCE_INTERFACE_APIURL_EMPTY;
                            return;
                        }
                    }
                    Charge_Order cOrder = (from o in db.Charge_Order where o.Id == order.Id select o).FirstOrDefault <Charge_Order>();
                    if (cOrder == null)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = ChargeConstant.ORDER_NOT_EXIST;
                        return;
                    }
                    cOrder.Process_time = KMBit.Util.DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    Resource_taocan taocan = (from t in db.Resource_taocan where t.Id == order.ResourceTaocanId select t).FirstOrDefault <Resource_taocan>();
                    if (order.AgencyId == 0)
                    {
                        cOrder.Payed = order.Payed;
                        //cOrder.Sale_price = taocan.Sale_price;
                        //cOrder.Purchase_price = taocan.Sale_price;
                        //cOrder.Platform_Cost_Price = taocan.Purchase_price;
                        //cOrder.Platform_Sale_Price = taocan.Sale_price;
                        //cOrder.Revenue = taocan.Sale_price- taocan.Purchase_price;
                        cOrder.Status = 10;//等待充值
                        result.Status = ChargeStatus.SUCCEED;
                        db.SaveChanges();
                        return;
                    }

                    Users       agency = (from u in db.Users where u.Id == order.AgencyId select u).FirstOrDefault <Users>();
                    Agent_route ruote  = (from au in db.Agent_route where au.Id == order.RouteId select au).FirstOrDefault <Agent_route>();
                    if (ruote != null)
                    {
                        //no need to verify agent remaining amount or credit amount if the charge is marketing order
                        if (cOrder.MarketOrderId <= 0)
                        {
                            float price = taocan.Sale_price * ruote.Discount;
                            if (agency.Pay_type == 1)
                            {
                                if (agency.Remaining_amount < price)
                                {
                                    result.Message        = ChargeConstant.AGENT_NOT_ENOUGH_MONEY;
                                    result.Status         = ChargeStatus.FAILED;
                                    cOrder.Status         = 3;//failed
                                    cOrder.Message        = result.Message;
                                    cOrder.Completed_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                                    //db.Charge_Order.Remove(cOrder);
                                    db.SaveChanges();
                                    return;
                                }
                                else
                                {
                                    agency.Remaining_amount = agency.Remaining_amount - price;
                                    result.Status           = ChargeStatus.SUCCEED;
                                    cOrder.Payed            = true;
                                    Logger.Info("Payment:" + price);
                                    Logger.Info("Payment has been executed on agent remaining amount.");
                                    db.SaveChanges();
                                }
                            }
                            else if (agency.Pay_type == 2)
                            {
                                if (agency.Remaining_amount < price)
                                {
                                    if (agency.Remaining_amount + agency.Credit_amount < price)
                                    {
                                        result.Message        = ChargeConstant.AGENT_NOT_ENOUGH_CREDIT;
                                        result.Status         = ChargeStatus.FAILED;
                                        cOrder.Status         = 3;//failed
                                        cOrder.Message        = result.Message;
                                        cOrder.Completed_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                                        //db.Charge_Order.Remove(cOrder);
                                        db.SaveChanges();
                                        return;
                                    }
                                    else
                                    {
                                        cOrder.Payed            = true;
                                        agency.Remaining_amount = agency.Remaining_amount - price;
                                        agency.Credit_amount    = agency.Credit_amount - (price - agency.Remaining_amount);
                                        Logger.Info("Payment:" + price);
                                        Logger.Info("Payment has been executed on agent Credit_amount amount.");
                                        db.SaveChanges();
                                        result.Status = ChargeStatus.SUCCEED;
                                    }
                                }
                                else
                                {
                                    agency.Remaining_amount = agency.Remaining_amount - price;
                                    result.Status           = ChargeStatus.SUCCEED;
                                    Logger.Info("Payment:" + price);
                                    Logger.Info("Payment has been executed on agent remaining amount.");
                                    db.SaveChanges();
                                    cOrder.Payed = true;
                                }
                            }
                        }

                        cOrder.Status = 10;
                        //cOrder.Sale_price = ruote.Sale_price;
                        //cOrder.Purchase_price = price;
                        //cOrder.Platform_Cost_Price = taocan.Purchase_price;
                        //cOrder.Platform_Sale_Price = taocan.Sale_price;
                        //cOrder.Revenue = price - taocan.Purchase_price;
                        db.SaveChanges();
                    }
                    if (result.Status == ChargeStatus.FAILED)
                    {
                        //db.Charge_Order.Remove(cOrder);
                    }
                    else
                    {
                        if (cOrder.MarketOrderId > 0)
                        {
                            Marketing_Orders mOrder = (from mo in db.Marketing_Orders where mo.Id == cOrder.MarketOrderId select mo).FirstOrDefault <Marketing_Orders>();
                            if (mOrder != null)
                            {
                                mOrder.Used        = true;
                                mOrder.Sent        = true;
                                mOrder.UsedTime    = KMBit.Util.DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                                mOrder.PhoneNumber = cOrder.Phone_number;
                                mOrder.OpenId      = cOrder.DeviceMacAddress;
                            }
                        }
                    }
                    db.SaveChanges();
                }
                Logger.Info("ProceedOrder Done!");
            }
        }
Example #7
0
        public APIChargeResult Charge(int agentId, int routeId, string mobile, string spname, string province, string city, string callBackUrl, string client_order_id)
        {
            if (string.IsNullOrEmpty(mobile))
            {
                throw new KMBitException("手机号码不能为空");
            }
            if (string.IsNullOrEmpty(spname))
            {
                throw new KMBitException("手机号归属运行商不能为空");
            }
            if (string.IsNullOrEmpty(province))
            {
                throw new KMBitException("手机号归属省份不能为空");
            }
            //if (string.IsNullOrEmpty(city))
            //{
            //    throw new KMBitException("手机号归属城市不能为空");
            //}
            ChargeResult result = null;
            ChargeBridge cb     = new ChargeBridge();
            ChargeOrder  order  = new ChargeOrder()
            {
                ClientOrderId    = client_order_id,
                Payed            = false,
                OperateUserId    = 0,
                AgencyId         = agentId,
                Id               = 0,
                Province         = province,
                City             = city != null? city:"",
                MobileNumber     = mobile,
                MobileSP         = spname,
                OutOrderId       = "",
                ResourceId       = 0,
                ResourceTaocanId = 0,
                RouteId          = routeId,
                CreatedTime      = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),
                CallbackUrl      = callBackUrl
            };
            chargebitEntities db       = null;
            OrderManagement   orderMgt = new OrderManagement();

            try
            {
                db = new chargebitEntities();
                Users agent = (from u in db.Users where u.Id == agentId select u).FirstOrDefault <Users>();
                if (agent == null)
                {
                    throw new KMBitException(string.Format("编号为{0}的代理商不存在", agentId));
                }
                if (!agent.Enabled)
                {
                    throw new KMBitException(string.Format("代理商{0}已经被关闭禁用", agent.Name));
                }
                //verify mobile sp
                Agent_route route = (from r in db.Agent_route where r.Id == routeId && r.User_id == agentId select r).FirstOrDefault <Agent_route>();
                if (route == null)
                {
                    throw new KMBitException(string.Format("代理商 {1} 编号为{0}的路由不存在", routeId, agent.Name));
                }
                if (!route.Enabled)
                {
                    throw new KMBitException(string.Format("代理商 {1} 编号为{0}的路由已经被关闭", routeId, agent.Name));
                }
                Resource_taocan taocan = (from t in db.Resource_taocan where t.Id == route.Resource_taocan_id select t).FirstOrDefault <Resource_taocan>();
                int             spId   = (from sp in db.Sp where sp.Name.Contains(spname.Trim()) select sp.Id).FirstOrDefault <int>();
                if (spId == 0)
                {
                    throw new KMBitException("手机运营商的值必须是-中国移动,中国联通或者中国电信");
                }
                int provinceId = (from area in db.Area where area.Name.Contains(province) select area.Id).FirstOrDefault <int>();
                if (provinceId == 0)
                {
                    throw new KMBitException("手机号码归属省份值不正确,例如 河南,海南,江苏,请以此种格式传入");
                }
                if (taocan.NumberProvinceId > 0 && provinceId > 0)
                {
                    if (provinceId != taocan.NumberProvinceId)
                    {
                        throw new KMBitException(string.Format("当前路由不能充{0}-{1}的手机号码", spname, province));
                    }
                }
                order = orderMgt.GenerateOrder(order);
                //result = cb.Charge(order);
                if (order.Id > 0)
                {
                    result         = new ChargeResult();
                    result.Status  = ChargeStatus.SUCCEED;
                    result.Message = "充值信息已提交到充值系统";
                }
            }
            catch (KMBitException kex)
            {
                throw kex;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                result         = new ChargeResult();
                result.Status  = ChargeStatus.FAILED;
                result.Message = "未知错误,请联系平台管理员";
            }

            APIChargeResult apiResult = new APIChargeResult();

            //apiResult.Message = result.Message;
            apiResult.OrderId = order.Id;
            switch (result.Status)
            {
            case ChargeStatus.SUCCEED:
                apiResult.Status  = ChargeStatus.SUCCEED.ToString();
                apiResult.Message = result.Message;
                break;

            case ChargeStatus.FAILED:
                apiResult.Status = ChargeStatus.FAILED.ToString();
                break;

            case ChargeStatus.ONPROGRESS:
                apiResult.Status = ChargeStatus.SUCCEED.ToString();
                break;

            case ChargeStatus.PENDIND:
                apiResult.Status = ChargeStatus.SUCCEED.ToString();
                break;
            }

            return(apiResult);
        }