/// <summary> /// 回调接口 /// </summary> /// <param name="pid"></param> /// <param name="mid"></param> /// <param name="Request"></param> /// <param name="Response"></param> /// <returns></returns> public async Task <NotifyReturn> CallBack(int pid, int mid, Microsoft.AspNetCore.Http.HttpRequest Request, Microsoft.AspNetCore.Http.HttpResponse Response) { Settle_plat p = OnlineSettle.GetPlat(pid); Settle_mch m = OnlineSettle.GetMch(mid); OnlineSettle onlinepay = (OnlineSettle)Activator.CreateInstance(Type.GetType(p.Plat_class), p, m); NotifyReturn r = await onlinepay.CallBack(Request); if (r.IsCheck) { if (r.IsPay) { Settle_order order = DB.Context.From <Settle_order>().Where(o => o.Order_id == r.OrderNumber).ToFirstDefault(); if (order != null && string.IsNullOrEmpty(order.Order_id)) { if (order.Status == 0) { order.Status = 1; order.Finish_time = DateTime.Now.ToTimeStamp(); order.Pay_amount = r.Totalfee; order.Notify_status = 0; order.Notify_times = 0; order.Notify_lasttime = DateTime.Now.ToTimeStamp(); order.Returnmsg = r.ReturnMsg; DB.Context.Save(order); } } } } if (string.IsNullOrEmpty(r.OrderNumber)) { Settle_order order = DB.Context.From <Settle_order>().Where(o => o.Order_id == r.OrderNumber).ToFirstDefault(); if (order != null && !string.IsNullOrEmpty(order.Order_id) && !string.IsNullOrEmpty(order.Callback_url)) { SortedDictionary <string, string> para = new SortedDictionary <string, string>(); para.Add("mchid", DB.MchId); para.Add("orderid", order.Order_id); para.Add("serialid", order.Plat_order_id); para.Add("amount", order.Pay_amount.ToString()); para.Add("attach", order.Attach); para.Add("status", order.Status.ToString()); string sign = string.Format("{0}&key={1}", OnlineSettle.GetParamSrc(para), DB.MchKey); sign = Dos.Common.EncryptHelper.MD5EncryptWeChat(sign, "utf-8"); para.Add("sign", sign); Response.Redirect(string.Format("{0}?{1}", order.Callback_url, OnlineSettle.GetParamSrc(para))); } } return(r); }
/// <summary> /// 通知接口 /// </summary> /// <param name="pid"></param> /// <param name="mid"></param> /// <param name="Request"></param> /// <returns></returns> public async Task <NotifyReturn> Notify(int pid, int mid, Microsoft.AspNetCore.Http.HttpRequest Request) { Settle_plat p = OnlineSettle.GetPlat(pid); Settle_mch m = OnlineSettle.GetMch(mid); OnlineSettle onlinepay = (OnlineSettle)Activator.CreateInstance(Type.GetType(p.Plat_class), p, m); NotifyReturn r = await onlinepay.Notify(Request); if (r.IsCheck) { if (r.IsPay) { Settle_order order = DB.Context.From <Settle_order>().Where(o => o.Order_id == r.OrderNumber).ToFirstDefault(); if (order != null && !string.IsNullOrEmpty(order.Order_id)) { if (order.Status == 0) { order.Status = 1; order.Finish_time = DateTime.Now.ToTimeStamp(); order.Pay_amount = r.Totalfee; order.Notify_status = 0; order.Notify_times = 0; order.Notify_lasttime = DateTime.Now.ToTimeStamp(); order.Returnmsg = r.ReturnMsg; DB.Context.Save(order); } } } else { Settle_order order = DB.Context.From <Settle_order>().Where(o => o.Order_id == r.OrderNumber).ToFirstDefault(); if (order != null && !string.IsNullOrEmpty(order.Order_id)) { if (order.Status == 0) { order.Returnmsg = r.ReturnMsg; DB.Context.Save(order); } } } return(r); } else { return(r); } }
/// <summary> /// 统一下单 /// </summary> /// <param name="mchid"></param> /// <param name="orderid"></param> /// <param name="body"></param> /// <param name="paytype"></param> /// <param name="amount"></param> /// <param name="attach"></param> /// <param name="ip"></param> /// <param name="callbackurl"></param> /// <param name="notifyurl"></param> /// <param name="sign"></param> /// <returns></returns> public async Task <UnifiedorderReturn> Unifiedorder(string mchid, string orderid, string Bank_name, string Bank_branch, string Bank_card_number, string Bank_account, string amount, string attach, string ip, string callbackurl, string notifyurl) { UnifiedorderReturn r = new UnifiedorderReturn(); if (mchid != DB.MchId) { r.Type = PayReturnType.Err; r.Content = "商户号不存在"; return(r); } decimal fee = 0; decimal.TryParse(amount, out fee); if (fee <= 0) { r.Type = PayReturnType.Err; r.Content = "打款金额必须大于0"; return(r); } Settle_mch m = OnlineSettle.MchList.FindAll(pp => pp.Nullity == false && ("" + pp.BankList + ",").Contains("," + Bank_name.Trim() + ",") && (pp.Max_money == 0 || pp.Max_money >= fee) && (pp.Min_money == 0 || pp.Min_money <= fee)).OrderBy(i => Guid.NewGuid()).First(); if (m == null) { r.Type = PayReturnType.Err; r.Content = "平台没有可用的打款渠道"; return(r); } Settle_plat p = OnlineSettle.GetPlat(m.Plat_id); if (p == null) { r.Type = PayReturnType.Err; r.Content = "平台支付渠道有误"; return(r); } if (DB.Context.Exists <Settle_order>(o => o.Order_id == orderid)) { r.Type = PayReturnType.Err; r.Content = "订单号已经存在"; return(r); } Settle_order order = new Settle_order(); order.Order_id = orderid; order.Mch_id = m.Id; order.Order_amount = fee; order.Pay_amount = 0; order.Bank_name = Bank_name.Trim(); order.Bank_branch = Bank_branch.Trim(); order.Bank_card_number = Bank_card_number.Trim(); order.Bank_account = Bank_account.Trim(); order.Status = 0; order.Callback_url = callbackurl; order.Notify_url = notifyurl; order.Create_time = DateTime.Now.ToTimeStamp(); order.Update_time = order.Create_time; order.Finish_time = order.Update_time; order.Notify_status = 0; order.Notify_lasttime = order.Update_time; order.Notify_times = 0; order.Attach = attach; OnlineSettle onlinepay = (OnlineSettle)Activator.CreateInstance(Type.GetType(p.Plat_class), p, m); r = await onlinepay.Unifiedorder(orderid, Bank_name, Bank_branch, Bank_card_number, Bank_account, fee, ip, attach); if (r.Type != 0) { order.Plat_order_id = r.SerialNumber; order.Pay_amount = decimal.Parse(r.RealPrice); order.Returnmsg = r.Content; } DB.Context.Save(order); return(r); }
public async Task <string> SendNotify(string orderid) { Settle_order o = DB.Context.From <Settle_order>().Where(p => p.Order_id == orderid).First(); if (o == null || o.Order_id != orderid) { return("订单不存在"); } if (o.Notify_url.Length < 7) { return("订单通知地址不误"); } SortedDictionary <string, string> para = new SortedDictionary <string, string>(); para.Add("mchid", DB.MchId); para.Add("orderid", o.Order_id); para.Add("serialid", o.Plat_order_id); para.Add("amount", o.Pay_amount.ToString()); para.Add("attach", o.Attach); para.Add("msg", o.Returnmsg); para.Add("status", o.Status.ToString()); string sign = string.Format("{0}&key={1}", OnlineSettle.GetParamSrc(para), DB.MchKey); sign = Dos.Common.EncryptHelper.MD5EncryptWeChat(sign, "utf-8"); para.Add("sign", sign); string msg = Task.Run(() => { string r; try { r = Dos.Common.HttpHelper.Post( new Dos.Common.HttpParam { Url = o.Notify_url, Encoding = System.Text.Encoding.UTF8, ParamType = Dos.Common.EnumHelper.HttpParamType.Form, GetParam = para } ); } catch (Exception ee) { r = ee.ToString(); } return(r); }).Result; if (o.Notify_status < 2) { if (msg == "ok") { o.Notify_status = 2; } else { o.Notify_status = 1; } if (o.Notify_times == null) { o.Notify_times = 0; } o.Notify_times = o.Notify_times + 1; o.Notify_lasttime = DateTime.Now.ToTimeStamp(); if (o.Notify_status != 2) { switch (o.Notify_times) { case 1: Publish(DB.MchId, o.Order_id, 60); break; //1分钟后再次通知 case 2: Publish(DB.MchId, o.Order_id, 300); break; //5分钟后再次通知 case 3: Publish(DB.MchId, o.Order_id, 900); break; //15分钟后再次通知 case 4: Publish(DB.MchId, o.Order_id, 7200); break; //2小时后再次通知 case 5: Publish(DB.MchId, o.Order_id, 21600); break; //6小时后再次通知 default: break; } } DB.Context.Save(o); } return(msg); }
/// <summary> /// 查询接口 /// </summary> /// <param name="mchid"></param> /// <param name="orderid"></param> /// <param name="sign"></param> /// <returns></returns> public async Task <QueryReturn> Query(string mchid, string orderid) { QueryReturn r = new QueryReturn(); if (mchid != DB.MchId) { r.ReturnMsg = "商户号不存在"; return(r); } Settle_order order = DB.Context.From <Settle_order>().Where(p => p.Order_id == orderid).ToFirstDefault(); if (order == null || string.IsNullOrEmpty(order.Order_id)) { r.ReturnMsg = "订单不存在"; return(r); } if (order.Status == 1) { r.ReturnMsg = "ok"; r.IsPay = true; r.OrderNumber = order.Order_id; r.SerialNumber = order.Plat_order_id; r.Totalfee = order.Pay_amount; r.Attach = order.Attach; return(r); } else { Settle_mch mch = OnlineSettle.GetMch(order.Mch_id); if (mch == null) { r.ReturnMsg = "平台商户信息错误"; return(r); } Settle_plat plat = OnlineSettle.GetPlat(mch.Plat_id); if (plat == null) { r.ReturnMsg = "平台渠道信息错误"; return(r); } OnlineSettle onlinepay = (OnlineSettle)Activator.CreateInstance(Type.GetType(plat.Plat_class), plat, mch); r = await onlinepay.OrderQuery(order.Order_id); if (r.ReturnMsg == "ok" && r.IsPay) { order.Status = 1; if (r.Totalfee > 0) { order.Pay_amount = r.Totalfee; } order.Finish_time = DateTime.Now.ToTimeStamp(); order.Returnmsg = r.ReturnMsg; DB.Context.Save(order); } else { order.Returnmsg = r.ReturnMsg; DB.Context.Save(order); } return(r); } }