Пример #1
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;
        }
Пример #2
0
        public APIChargeResult Charge()
        {
            logger.Info("Client system call is coming...");
            APIChargeResult message = new APIChargeResult();
            try
            {
                base.IniRequest();
                string siganture = string.Empty;
                string accessToken = string.Empty;
                string queryStr = string.Empty;
                base.ParseSigantures(out siganture, out accessToken, out queryStr);
                if (string.IsNullOrEmpty(siganture))
                {
                    message.Status = "FAILED";
                    message.Message = "sign不能为空";
                    return message;
                }
                if (string.IsNullOrEmpty(accessToken))
                {
                    message.Status = "FAILED";
                    message.Message = "token不能为空";
                    return message;
                }
                if (string.IsNullOrEmpty(queryStr))
                {
                    message.Status = "FAILED";
                    message.Message = "传入的参数不合法";
                    return message;
                }

                ApiAccessManagement accessMgt = new ApiAccessManagement();
                BUser user = accessMgt.GetUserByAccesstoken(accessToken);
                if (user == null)
                {
                    message.Status = "FAILED";
                    message.Message = "token不正确";
                    return message;
                }
                logger.Info(string.Format("Client system post data:{0}", queryStr));
                logger.Info(string.Format("Signature:{0}", siganture != null ? siganture : ""));
                logger.Info(string.Format("Agent - {0}", user != null ? user.User.Name : ""));
                bool verifySign = accessMgt.VerifyApiSignature(user.User.SecurityStamp, queryStr, siganture);
                if (!verifySign)
                {
                    logger.Info(string.Format("Failed to verify signature."));
                    message.Status = "FAILED";
                    message.Message = "签名不正确,请使用正确的SecurityToken进行签名";
                    return message;
                }
                logger.Info("Signature verification passed.");
                int routeId = 0;
                string callbackUrl = request["CallBackUrl"] != null ? request["CallBackUrl"] : "";
                string province = request["Province"] != null ? request["Province"] : "";
                string city = request["City"] != null ? request["City"] : "";
                string mobile = request["Mobile"] != null ? request["Mobile"] : "";
                string clientOrderId = request["Client_order_id"];
                string spName= request["MobileSP"];
                int.TryParse(request["Id"], out routeId);
                if (string.IsNullOrEmpty(mobile) || mobile.Trim().Length != 11)
                {
                    message.Status = "FAILED";
                    message.Message = "手机号码不正确";
                    return message;
                }

                if (string.IsNullOrEmpty(province))
                {
                    message.Status = "FAILED";
                    message.Message = "手机归属省份(参数Province)不能为空";
                    return message;
                }

                //if (string.IsNullOrEmpty(city))
                //{
                //    message.Status = "FAILED";
                //    message.Message = "手机归属城市(参数City)不能为空";
                //    return message;
                //}

                if (string.IsNullOrEmpty(spName))
                {
                    message.Status = "FAILED";
                    message.Message = "手机归属运营商(参数MobileSP)不能为空";
                    return message;
                }
                else
                {
                    if(spName!="中国移动" && spName!="中国联通" && spName!="中国电信")
                    {
                        message.Status = "FAILED";
                        message.Message = "手机归属运营商(参数MobileSP)值必须为 中国移动,中国联通或者中国电信";
                        return message;
                    }
                }

                if (routeId <= 0)
                {
                    message.Status = "FAILED";
                    message.Message = "非法路由产品编号(ID)";
                    return message;
                }

                ProductManagement pdtMger = new ProductManagement();
                message = pdtMger.Charge(user.User.Id, routeId, mobile,spName, province, city, callbackUrl, clientOrderId);
                logger.Info(message.Status);
                logger.Info(message.Message);
            }
            catch (KMBitException kex)
            {
                logger.Error(kex);
                message.Status = "FAILED";
                message.Message = kex.Message;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                message.Status = "FAILED";
                message.Message = "未知错误,联系平台管理员";
            }
            logger.Info("Finished processing client calling.");
            logger.Info("...................................");
            return message;
        }