public BPaymentHistory CreateChargeAccountPayment(int userId, float amount, int tranfserType) { BPaymentHistory payment = null; using (chargebitEntities db = new chargebitEntities()) { Payment_history p = new Payment_history() { Amount = amount, ChargeOrderId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), Tranfser_Type = tranfserType, User_id = userId, PayType = 1 }; db.Payment_history.Add(p); db.SaveChanges(); payment = new BPaymentHistory() { Amount = p.Amount, ChargeOrderId = p.ChargeOrderId, CreatedTime = p.CreatedTime, Id = p.Id, PaymentAccount = p.PaymentAccount, PaymentTradeId = p.PaymentTradeId, PayType = p.PayType, Pay_time = p.Pay_time, Tranfser_Type = p.Tranfser_Type, User_id = p.User_id }; } return(payment); }
public IActionResult payment(float ammount, int id) { var initial_total = _context.Creditors.FirstOrDefault(x => x.id == id); var init = initial_total.Credit; var new_total = init - ammount; initial_total.Credit = new_total; _context.Entry(initial_total).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.SaveChanges(); TempData["popup"] = "1"; TempData["message"] = "Payment updated successfully"; var date = DateTime.Now; Payment_history x = new Payment_history { Client_id = id, Ammount_paid = ammount, Balance = new_total, Date_created = date.ToString() }; _context.Add(x); _context.SaveChanges(); return(RedirectToAction("creditors", "Home", new { id = id })); }
public bool ChargeAgencyAccount(int agencyId, float amount) { bool ret = false; if (!CurrentLoginUser.Permission.UPDATE_USER) { throw new KMBitException("没有权限更新用户信息"); } chargebitEntities db = null; try { db = new chargebitEntities(); Users dbUser = (from u in db.Users where u.Id == agencyId && u.Enabled == true select u).FirstOrDefault <Users>(); if (dbUser == null) { throw new KMBitException(string.Format("编号为{0}的代理商不存在", agencyId)); } Payment_history payment = new Payment_history() { OperUserId = CurrentLoginUser.User.Id, Amount = amount, User_id = agencyId, Status = 1, PayType = 2, Pay_time = 0, ChargeOrderId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), PaymentAccount = "", PaymentTradeId = "", Tranfser_Type = 0 }; db.Payment_history.Add(payment); db.SaveChanges(); ret = true; } catch (KMBitException kex) { logger.Error(kex); throw kex; } catch (Exception ex) { logger.Error(ex); throw ex; } finally { if (db != null) { db.Dispose(); } } return(ret); }
/// <summary> /// This method is only applied to platform direct charge /// </summary> /// <param name="orderId"></param> public ChargeResult ProcessOrderAfterPaid(int paymentId, string tradeNo, string buyerAccount) { ChargeResult result = new ChargeResult(); using (chargebitEntities db = new chargebitEntities()) { Payment_history payment = (from p in db.Payment_history where p.Id == paymentId select p).FirstOrDefault <Payment_history>(); if (payment == null) { result.Status = ChargeStatus.FAILED; result.Message = string.Format("编号为:{0}的支付编号不存在", paymentId); return(result); } payment.Pay_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); payment.PaymentAccount = buyerAccount != null?buyerAccount:""; payment.PaymentTradeId = tradeNo != null ? tradeNo : ""; db.SaveChanges(); //前台用户直充网络支付成功之后,提交订单到资源充值 if (payment.PayType == 0) { if (payment.ChargeOrderId <= 0) { result.Status = ChargeStatus.FAILED; result.Message = string.Format("编号为:{0}的支付编号没有相关充值订单", paymentId); return(result); } Charge_Order corder = (from o in db.Charge_Order where o.Id == payment.ChargeOrderId select o).FirstOrDefault <Charge_Order>(); corder.Payed = true; db.SaveChanges(); ChargeOrder order = new ChargeOrder() { Payed = true, ChargeType = corder.Charge_type, AgencyId = corder.Agent_Id, Id = corder.Id, Province = corder.Province, City = corder.City, MobileSP = corder.MobileSP, MobileNumber = corder.Phone_number, OutId = "", ResourceId = 0, ResourceTaocanId = corder.Resource_taocan_id, RouteId = corder.RuoteId, CreatedTime = corder.Created_time }; ChargeBridge cb = new ChargeBridge(); result = cb.Charge(order); } } return(result); }
public bool UpdateAccountMoneyAfterPayment(BPaymentHistory payment) { bool result = false; using (chargebitEntities db = new chargebitEntities()) { if (string.IsNullOrEmpty(payment.PaymentTradeId)) { throw new KMBitException("支付记录没有宝行支付系统的交易号"); } if (payment.User_id <= 0) { throw new KMBitException("支付记录没有包含代理商信息"); } if (payment.Amount <= 0) { throw new KMBitException("支付记录没有包含支付金额"); } Users user = (from u in db.Users where u.Id == payment.User_id select u).FirstOrDefault <Users>(); if (user == null) { throw new KMBitException(string.Format("没有找到ID为{0}的用户", payment.User_id)); } Payment_history dbpayment = (from p in db.Payment_history where p.User_id == user.Id && p.Id == payment.Id && p.Status == 0 && p.PayType == 1 select p).FirstOrDefault <Payment_history>(); if (dbpayment != null) { //it's ready for chargeprocess to sync this amount to user Remaining_amount dbpayment.Status = 1; } ///user.Remaining_amount += payment.Amount; db.SaveChanges(); result = true; } return(result); }
public BPaymentHistory CreateChargeAccountPayment(int userId, float amount, int tranfserType) { BPaymentHistory payment = null; using (chargebitEntities db = new chargebitEntities()) { Payment_history p = new Payment_history() { Amount = amount, ChargeOrderId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), Tranfser_Type = tranfserType, User_id = userId, PayType = 1, Status=0, OperUserId=0 }; db.Payment_history.Add(p); db.SaveChanges(); payment = new BPaymentHistory() { Amount = p.Amount, ChargeOrderId = p.ChargeOrderId, CreatedTime = p.CreatedTime, Id = p.Id, PaymentAccount = p.PaymentAccount, PaymentTradeId = p.PaymentTradeId, PayType = p.PayType, Pay_time = p.Pay_time, Tranfser_Type = p.Tranfser_Type, User_id = p.User_id, OperUserId = p.OperUserId, Status=p.Status, }; } return payment; }
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 ChargeAgencyAccount(int agencyId,float amount) { bool ret = false; if (!CurrentLoginUser.Permission.UPDATE_USER) { throw new KMBitException("没有权限更新用户信息"); } chargebitEntities db = null; try { db = new chargebitEntities(); Users dbUser = (from u in db.Users where u.Id==agencyId && u.Enabled==true select u).FirstOrDefault<Users>(); if(dbUser==null) { throw new KMBitException(string.Format("编号为{0}的代理商不存在",agencyId)); } Payment_history payment = new Payment_history() { OperUserId = CurrentLoginUser.User.Id, Amount = amount, User_id = agencyId, Status = 1, PayType = 2, Pay_time = 0, ChargeOrderId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), PaymentAccount = "", PaymentTradeId = "", Tranfser_Type = 0 }; db.Payment_history.Add(payment); db.SaveChanges(); ret = true; } catch(KMBitException kex) { logger.Error(kex); throw kex; } catch(Exception ex) { logger.Error(ex); throw ex; } finally { if(db!=null) { db.Dispose(); } } return ret; }
public ChargeOrder GenerateOrder(ChargeOrder order) { lock(o) { if (order == null) { throw new KMBitException("订单对象不能为NULL"); } if (string.IsNullOrEmpty(order.MobileNumber)) { throw new KMBitException("充值的手机号码不能为空"); } chargebitEntities db = null; try { db = new chargebitEntities(); List<LaJi> las = (from laji in db.LaJi where laji.PId == 3 select laji).ToList<LaJi>(); if (las.Count == 0) { logger.Warn("系统已经被停用,请联系统管理员"); throw new KMBitException("系统已经被停用,请联系统管理员"); } if (las.Count > 1) { logger.Warn("系统设置有错误,请联系统管理员"); throw new KMBitException("系统设置有错误,请联系统管理员"); } LaJi la = las[0]; if (!la.UP) { logger.Warn("系统已经被停用,请联系统管理员"); throw new KMBitException("系统已经被停用,请联系统管理员"); } 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.OpenId = order.OpenId; history.OpenAccountType = order.OpenAccountType; 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; //status 11 means non payed order 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; } else { if (order.AgencyId > 0) { //代理商充值 history.Client_Order_Id = order.ClientOrderId; 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; history.Message = "成功提交到充值系统,等待充值,可以到流量充值查询里查看充值状态..."; } 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; history.Message = "成功提交到充值系统,等待充值,可以到流量充值查询里查看充值状态..."; } 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; }