protected void RemoveQRCode(Charge_Order order) { if (order.MarketOrderId > 0) { chargebitEntities db = null; try { db = new chargebitEntities(); Marketing_Orders mOrder = (from mo in db.Marketing_Orders where mo.Id == order.MarketOrderId select mo).FirstOrDefault <Marketing_Orders>(); if (mOrder != null && !string.IsNullOrEmpty(mOrder.CodePath)) { string tmpPhysicalPath = System.IO.Path.Combine(settings.RootDirectory, mOrder.CodePath.Replace('/', '\\')); if (System.IO.File.Exists(tmpPhysicalPath)) { System.IO.File.Delete(tmpPhysicalPath); } } }catch (Exception ex) { Logger.Fatal(ex); }finally { if (db != null) { db.Dispose(); } } } }
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); }
public bool CreateActivityTaocan(Marketing_Activity_Taocan taocan) { bool result = false; if (taocan == null) { throw new KMBitException("输入不正确"); } if (taocan.ActivityId <= 0) { throw new KMBitException("活动信息丢失"); } if (taocan.RouteId <= 0) { throw new KMBitException("套餐必须选择"); } if (taocan.Quantity <= 0) { throw new KMBitException("数量不能小于0"); } if (taocan.Price <= 0) { throw new KMBitException("价格不能小于0"); } chargebitEntities db = new chargebitEntities(); try { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == taocan.ActivityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { throw new KMBitException(string.Format("编号为{0}的活动不存在", taocan.ActivityId)); } Customer customer = (from c in db.Customer where c.Id == activity.CustomerId select c).FirstOrDefault <Customer>(); if (customer == null) { throw new KMBitException(string.Format("编号为{0}的活动不属于任何客户", taocan.ActivityId)); } if (customer.AgentId != CurrentLoginUser.User.Id) { throw new KMBitException(string.Format("编号为{0}的活动不属您的客户", taocan.ActivityId)); } List <BAgentRoute> routes = new List <BAgentRoute>(); var query = from r in db.Agent_route join u in db.Users on r.CreatedBy equals u.Id join uu in db.Users on r.UpdatedBy equals uu.Id into luu from lluu in luu.DefaultIfEmpty() join re in db.Resource on r.Resource_Id equals re.Id join tc in db.Resource_taocan on r.Resource_taocan_id equals tc.Id join city in db.Area on tc.Area_id equals city.Id into lcity from llcity in lcity.DefaultIfEmpty() join sp in db.Sp on tc.Sp_id equals sp.Id into lsp from llsp in lsp.DefaultIfEmpty() join tcc in db.Taocan on tc.Taocan_id equals tcc.Id into ltc from lltc in ltc.DefaultIfEmpty() where r.Id == taocan.RouteId select new BAgentRoute { Route = r, CreatedBy = u, UpdatedBy = lluu, Taocan = new BResourceTaocan { Taocan = tc, Resource = new BResource { Resource = re }, Province = llcity, SP = llsp, Taocan2 = lltc } }; routes = query.ToList <BAgentRoute>(); if (routes.Count == 0) { throw new KMBitException(string.Format("编号为{0}的套餐不存在", taocan.RouteId)); } BAgentRoute route = routes[0]; if (!route.Route.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐已经被管理员停用", taocan.RouteId)); } if (!route.Taocan.Taocan.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐的落地套餐被管理员停用", taocan.RouteId)); } if (!route.Taocan.Resource.Resource.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐的落地被管理员停用", taocan.RouteId)); } taocan.CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); taocan.ResourceId = route.Taocan.Resource.Resource.Id; taocan.ResourceTaocanId = route.Taocan.Taocan.Id; taocan.Generated = true; Users agency = (from a in db.Users where activity.AgentId == a.Id select a).FirstOrDefault <Users>(); if (agency.Remaining_amount < taocan.Quantity * (route.Taocan.Taocan.Sale_price * route.Route.Discount)) { throw new KMBitException("代理商账户没有足够的余额"); } if (customer.RemainingAmount < taocan.Quantity * taocan.Price) { throw new KMBitException("客户账户没有足够的余额"); } Marketing_Activity_Taocan dbmTaocan = (from mt in db.Marketing_Activity_Taocan where mt.ResourceTaocanId == taocan.ResourceTaocanId && mt.ActivityId == taocan.ActivityId select mt).FirstOrDefault <Marketing_Activity_Taocan>(); if (dbmTaocan == null) { db.Marketing_Activity_Taocan.Add(taocan); } else { dbmTaocan.Quantity += taocan.Quantity; taocan.Id = dbmTaocan.Id; } db.SaveChanges(); if (taocan.Id > 0) { agency.Remaining_amount -= taocan.Quantity * (route.Taocan.Taocan.Sale_price * route.Route.Discount); customer.RemainingAmount -= taocan.Quantity * taocan.Price; db.SaveChanges(); for (int i = 0; i < taocan.Quantity; i++) { Marketing_Orders order = new Marketing_Orders() { ActivityId = taocan.ActivityId, ActivityTaocanId = taocan.Id, AgentPurchasePrice = 0, AgentSalePrice = 0, CreatedTime = taocan.CreatedTime, ExpiredTime = activity.ExpiredTime, PlatformPurchasePrice = 0, PlatformSalePrice = route.Taocan.Taocan.Sale_price, Sent = false, Used = false, StartTime = activity.StartedTime, UsedTime = 0 }; if (route.Taocan.Taocan.EnableDiscount) { order.PlatformPurchasePrice = route.Taocan.Taocan.Purchase_price * route.Taocan.Taocan.Resource_Discount; } order.AgentPurchasePrice = order.PlatformSalePrice * route.Route.Discount; order.AgentSalePrice = taocan.Price; db.Marketing_Orders.Add(order); } db.SaveChanges(); if (activity.ScanType == 2) { GenerateQRCodeForMarketingOrders(taocan.Id); } result = true; } }catch (Exception ex) { throw ex; } finally { if (db != null) { db.Dispose(); } } return(result); }
public string GetOneRandomMarketOrderQrCodeUrl(string spName, string openId, int agentId, int customerId, int activityId) { logger.Info("OpenId:" + openId); AppSettings settings = AppSettings.GetAppSettings(); if (string.IsNullOrEmpty(openId)) { throw new KMBitException("用户的微信号不能为空,用户必须通过关注微信公众号来获取二维码"); } if (string.IsNullOrEmpty(spName)) { throw new KMBitException("获取客户活动随机二维码时候,必须输入运营商名称"); } if (!spName.Contains("+联通") && !spName.Contains("+移动") && !spName.Contains("+电信")) { throw new KMBitException("运营商名称只能是,+联通,+移动,+电信,+号为英文键盘下的+号"); } if (agentId == 0) { throw new KMBitException("代理商编号不能为空"); } if (customerId == 0) { throw new KMBitException("客户编号不能为空"); } using (chargebitEntities db = new chargebitEntities()) { int sp = 0; if (spName.Contains("联通")) { sp = 3; } else if (spName.Contains("移动")) { sp = 1; } else if (spName.Contains("电信")) { sp = 2; } Marketing_Activities activity = null; if (activityId == 0) { //get the latest activity activity = (from a in db.Marketing_Activities where a.CustomerId == customerId && a.AgentId == agentId && a.Enabled == true && a.ScanType == 2 orderby a.Id descending select a).FirstOrDefault <Marketing_Activities>(); } else { activity = (from a in db.Marketing_Activities where a.Id == activityId select a).FirstOrDefault <Marketing_Activities>(); } if (activity == null) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.AgentId != agentId) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.CustomerId != customerId) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.ScanType != 2) { throw new KMBitException("此活动为直接扫码活动,不能分个推送二维码"); } List <Marketing_Activity_Taocan> rTaocans = (from mt in db.Marketing_Activity_Taocan join t in db.Resource_taocan on mt.ResourceTaocanId equals t.Id where mt.ActivityId == activity.Id && t.Sp_id == sp select mt).ToList <Marketing_Activity_Taocan>(); if (rTaocans.Count == 0) { throw new KMBitException(string.Format("此次活动{0}的手机号码不能扫码充流量,如有疑问请联系活动举办方", spName)); } Marketing_Activity_Taocan mTaocan = rTaocans[0]; Marketing_Orders returnOrder = null; List <Marketing_Orders> existedOrders = (from mo in db.Marketing_Orders where mo.ActivityId == activity.Id && mo.OpenId == openId select mo).ToList <Marketing_Orders>(); if (existedOrders.Count > 0) { returnOrder = existedOrders[0]; if (!returnOrder.Used && returnOrder.Sent) { logger.Info("Already sent but not used, sent again"); return(settings.WebURL + "/" + settings.QRFolder + "/" + returnOrder.CodePath); } else { logger.Info("Already sent and used"); throw new KMBitException(string.Format("此微信号已经获取过二维码,并且已经扫码使用过二维码(一次活动一个微信号只能获取一个二维码)")); } } logger.Info("not sent and not used"); returnOrder = (from mo in db.Marketing_Orders where mo.ActivityTaocanId == mTaocan.Id && mo.ActivityId == activity.Id && mo.Used == false && mo.Sent == false orderby mo.Id descending select mo).FirstOrDefault <Marketing_Orders>(); if (returnOrder == null) { throw new KMBitException(string.Format("本次活动的二维码全部送完,尽情期待下次活动,感谢您的关注")); } returnOrder.OpenId = openId; returnOrder.Sent = true; db.SaveChanges(); return(settings.WebURL + "/" + settings.QRFolder + "/" + returnOrder.CodePath); } }
public ChargeResult MarketingCharge(BMarketOrderCharge orderCharge) { logger.Info("MarketingCharge"); ChargeResult result = new ChargeResult(); if (orderCharge == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (orderCharge.AgentId == 0 || orderCharge.CustomerId == 0 || orderCharge.ActivityId == 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (string.IsNullOrEmpty(orderCharge.SPName)) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (string.IsNullOrEmpty(orderCharge.Phone)) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } int sp = 0; if (orderCharge.SPName.Contains("联通")) { sp = 3; } else if (orderCharge.SPName.Contains("移动")) { sp = 1; } else if (orderCharge.SPName.Contains("电信")) { sp = 2; } chargebitEntities db = new chargebitEntities(); Marketing_Activity_Taocan mtaocan = null; Marketing_Orders mOrder = null; try { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == orderCharge.ActivityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (activity.CustomerId != orderCharge.CustomerId) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (activity.AgentId != orderCharge.AgentId) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } //非直接扫码活动,必须传入特定的marketing order id if (activity.ScanType != 1 && orderCharge.ActivityOrderId == 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } ChargeOrder order = new ChargeOrder() { AgencyId = orderCharge.AgentId, ChargeType = 1, City = orderCharge.City, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), IsMarket = true, MacAddress = orderCharge.OpenId, MobileNumber = orderCharge.Phone, MobileSP = orderCharge.SPName, Payed = true, Province = orderCharge.Province, }; //direct scan if (activity.ScanType == 1 && orderCharge.ActivityOrderId <= 0) { //判断是否还有可用marketing order List <Marketing_Activity_Taocan> rTaocans = (from mt in db.Marketing_Activity_Taocan join t in db.Resource_taocan on mt.ResourceTaocanId equals t.Id where mt.ActivityId == orderCharge.ActivityId && t.Sp_id == sp select mt).ToList <Marketing_Activity_Taocan>(); if (rTaocans.Count == 0) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动" + orderCharge.SPName + "不能扫码充值"; return(result); } mtaocan = rTaocans[0]; mOrder = (from o in db.Marketing_Orders where o.ActivityId == orderCharge.ActivityId && o.Sent == false && o.Used == false && o.ActivityTaocanId == mtaocan.Id select o).FirstOrDefault <Marketing_Orders>(); if (mOrder == null) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } if (mOrder.Used) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } mOrder.Used = true; mOrder.Sent = true; //db.SaveChanges(); order.MarketOrderId = mOrder.Id; order.ResourceTaocanId = mtaocan.ResourceTaocanId; }//weichat push else if (activity.ScanType == 2 && orderCharge.ActivityOrderId > 0) { mOrder = (from o in db.Marketing_Orders where o.Id == orderCharge.ActivityOrderId select o).FirstOrDefault <Marketing_Orders>(); if (mOrder == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } if (mOrder.Used) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } mtaocan = (from mt in db.Marketing_Activity_Taocan where mt.Id == mOrder.ActivityTaocanId select mt).FirstOrDefault <Marketing_Activity_Taocan>(); if (mtaocan == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } Resource_taocan rT = (from r in db.Resource_taocan where r.Id == mtaocan.ResourceTaocanId select r).FirstOrDefault <Resource_taocan>(); if (rT == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } if (rT.Sp_id != sp) { string tmpSPName = ""; if (rT.Sp_id == 1) { tmpSPName = "中国移动"; } else if (rT.Sp_id == 2) { tmpSPName = "中国电信"; } else if (rT.Sp_id == 3) { tmpSPName = "中国联通"; } result.Status = ChargeStatus.FAILED; result.Message = string.Format("此二维码链接不能充值{0}的手机号码的流量,只能充值{1}的号码的流量", orderCharge.SPName, tmpSPName); return(result); } order.ResourceTaocanId = mtaocan.ResourceTaocanId; order.MarketOrderId = orderCharge.ActivityOrderId; mOrder.Used = true; mOrder.Sent = true; } else if (activity.ScanType == 1 && orderCharge.ActivityOrderId > 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } else if (activity.ScanType == 2 && orderCharge.ActivityOrderId <= 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } OrderManagement orderMgr = new OrderManagement(CurrentLoginUser); order = orderMgr.GenerateOrder(order); ChargeBridge chargeBridge = new ChargeBridge(); if (order.Id > 0) { db.SaveChanges(); result = chargeBridge.Charge(order); if (result.Status == ChargeStatus.FAILED) { //Rollback, the order cannot be used next time mOrder.UsedTime = 0; mOrder.Used = false; if (activity.ScanType == 1) { mOrder.Sent = false; } db.SaveChanges(); } else { mOrder.UsedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } db.SaveChanges(); } } catch (KMBitException kex) { logger.Warn(kex); result.Status = ChargeStatus.FAILED; result.Message = kex.Message; if (mOrder != null) { mOrder.Used = false; db.SaveChanges(); } } catch (Exception ex) { logger.Fatal(ex); result.Status = ChargeStatus.FAILED; result.Message = "系统错误,稍后再试"; if (mOrder != null) { mOrder.Used = false; db.SaveChanges(); } } finally { if (db != null) { db.Dispose(); } } logger.Info("Finished MarketingCharge"); return(result); }
public void GetChargeStatus(int resourceId) { chargebitEntities db = new chargebitEntities(); try { List <Charge_Order> orders = (from o in db.Charge_Order where o.Status == 1 select o).ToList <Charge_Order>(); if (orders.Count <= 0) { Logger.Info("No orders need to sync status of resourceId:" + resourceId); return; } Logger.Info(string.Format("{0} orders need to sync status", orders.Count)); KMBit.DAL.Resrouce_interface rInterface = (from ri in db.Resrouce_interface where ri.Resource_id == resourceId select ri).FirstOrDefault <Resrouce_interface>(); ServerUri = new Uri(rInterface.APIURL); List <WebRequestParameters> parmeters = new List <WebRequestParameters>(); parmeters.Add(new WebRequestParameters("V", version, false)); parmeters.Add(new WebRequestParameters("Action", "getReports", false)); SortedDictionary <string, string> paras = new SortedDictionary <string, string>(); paras["Account"] = rInterface.Username; paras["Count"] = statusCount.ToString(); string signStr = ""; foreach (KeyValuePair <string, string> p in paras) { if (signStr == string.Empty) { signStr += p.Key.ToLower() + "=" + p.Value; } else { signStr += "&" + p.Key.ToLower() + "=" + p.Value; } } signStr += "&key=" + KMAes.DecryptStringAES(rInterface.Userpassword); paras["Sign"] = UrlSignUtil.GetMD5(signStr); foreach (KeyValuePair <string, string> p in paras) { parmeters.Add(new WebRequestParameters(p.Key, p.Value, false)); } bool succeed = false; SendRequest(parmeters, false, out succeed); if (succeed && !string.IsNullOrEmpty(Response)) { JObject jsonRes = JObject.Parse(Response); string code = jsonRes["Code"] != null ? jsonRes["Code"].ToString() : ""; string message = jsonRes["Message"] != null ? jsonRes["Message"].ToString() : ""; Logger.Info(string.Format("Code:{0}, Message:{1}", code, message)); if (!string.IsNullOrEmpty(code) && code == "0" && !string.IsNullOrEmpty(message) && message == "OK") { JArray charges = (JArray)jsonRes["Reports"]; if (charges != null) { Logger.Info(string.Format("Get {0} reports from resource", charges.Count)); for (int i = 0; i < charges.Count; i++) { JObject report = (JObject)charges[i]; string taskId = report["TaskID"] != null? report["TaskID"].ToString():""; string phone = report["Mobile"] != null? report["Mobile"].ToString():""; string status = report["Status"] != null? report["Status"].ToString():""; string time = report["ReportTime"] != null?report["ReportTime"].ToString():""; string rptCode = report["ReportCode"] != null? report["ReportCode"].ToString():""; Logger.Info(string.Format("TaskId:{0}, MobilePhone:{1}, Status:{2}, Time:{3}", taskId, phone, status, time)); Charge_Order order = (from o in orders where o.Out_Order_Id == taskId && o.Phone_number == phone select o).FirstOrDefault <Charge_Order>(); if (order != null && !string.IsNullOrEmpty(taskId) && !string.IsNullOrEmpty(phone) && !string.IsNullOrEmpty(status)) { DateTime cTime = DateTime.MinValue; DateTime.TryParse(time, out cTime); if (cTime != DateTime.MinValue) { order.Completed_Time = DateTimeUtil.ConvertDateTimeToInt(cTime); } if (status == "4") { order.Status = 2; order.Message = "充值成功:" + rptCode; RemoveQRCode(order); } else if (status == "5") { order.Status = 3; order.Message = "充值失败:" + rptCode; if (order.Agent_Id > 0) { if (order.MarketOrderId <= 0) { Users agency = (from u in db.Users where u.Id == order.Agent_Id select u).FirstOrDefault <Users>(); if (agency != null) { agency.Remaining_amount += order.Purchase_price; order.Refound = true; } } else { order.Message = string.Format("充值失败:{0},二维码可以重复扫码使用直到充值成功", rptCode); order.Refound = false; Marketing_Orders mOrder = (from mo in db.Marketing_Orders where mo.Id == order.MarketOrderId select mo).FirstOrDefault <Marketing_Orders>(); if (mOrder != null) { mOrder.Used = false; } } db.SaveChanges(); } } } //send back status to agent api calling if (!string.IsNullOrEmpty(order.CallBackUrl)) { this.SendStatusBackToAgentCallback(order); } } if (charges != null && charges.Count > 0) { db.SaveChanges(); } } } } } catch (KMBitException kex) { Logger.Warn(kex); }catch (Exception ex) { Logger.Fatal(ex); }finally { if (db != null) { db.Dispose(); } } }
public virtual void ChangeOrderStatus(ChargeOrder order, ChargeResult result, bool needCallBack = false) { lock (o) { int beforeStaus = order.Status; using (chargebitEntities db = new chargebitEntities()) { Charge_Order cOrder = (from o in db.Charge_Order where o.Id == order.Id select o).FirstOrDefault <Charge_Order>(); if (cOrder != null) { switch (result.Status) { case ChargeStatus.ONPROGRESS: cOrder.Out_Order_Id = order.OutOrderId; cOrder.Status = 1; cOrder.Message = result.Message; order.Status = 1; break; case ChargeStatus.SUCCEED: if (string.IsNullOrEmpty(cOrder.Out_Order_Id)) { cOrder.Out_Order_Id = order.OutOrderId; } cOrder.Status = 2; order.Status = 2; cOrder.Message = result.Message + ",落地充值成功"; cOrder.Out_Order_Id = order.OutOrderId; cOrder.Completed_Time = KMBit.Util.DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); //remove qrcode picture RemoveQRCode(cOrder); break; case ChargeStatus.FAILED: if (string.IsNullOrEmpty(cOrder.Out_Order_Id)) { cOrder.Out_Order_Id = order.OutOrderId; } order.Status = 3; cOrder.Status = 3; cOrder.Completed_Time = KMBit.Util.DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); //Refound the money if (cOrder.Agent_Id > 0) { Users agency = (from u in db.Users where u.Id == cOrder.Agent_Id select u).FirstOrDefault <Users>(); if (agency != null) { //marketing scan, no need to refound, just re-enable the scan if (cOrder.MarketOrderId == 0) { cOrder.Message = result.Message + ",充值订单金额:" + cOrder.Purchase_price + "已经退回代理商账户"; agency.Remaining_amount += cOrder.Purchase_price; cOrder.Refound = true; db.SaveChanges(); } else { //no need to refound for scanning cOrder.Message = result.Message + ",二维码可以重复扫码使用直到充值成功"; cOrder.Refound = false; Marketing_Orders mOrder = (from mo in db.Marketing_Orders where mo.Id == cOrder.MarketOrderId select mo).FirstOrDefault <Marketing_Orders>(); if (mOrder != null) { mOrder.Used = false; db.SaveChanges(); } } } } else { if (cOrder.Operate_User > 0) { cOrder.Message = result.Message + ",管理员后台充值,无需退款"; } else { cOrder.Message = result.Message + ",用户前台直充失败,需要手动退款给用户"; } } break; } db.SaveChanges(); if (needCallBack && beforeStaus == 1 && cOrder.Received == false && cOrder.PushedTimes < 4) { this.SendStatusBackToAgentCallback(cOrder); } } } } }
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!"); } }