public IActionResult GetPayOnlineList(string keyword, int page = 1, int limit = 20, int kid = 0) { int numPerPage, currentPage, startRowIndex; numPerPage = limit; currentPage = page; startRowIndex = (currentPage - 1) * numPerPage; Expression ex = OnlinePayOrder._.Id > 0 & OnlinePayOrder._.PaymentStatus == Utils.PaymentState[1]; string mytype = Request.Query["mytype"]; if (Utils.IsInt(mytype) && int.Parse(mytype) > 0) { ex &= OnlinePayOrder._.MyType == int.Parse(mytype); } if (!string.IsNullOrWhiteSpace(keyword)) { ex &= OnlinePayOrder._.Title.Contains(keyword); } IList <OnlinePayOrder> list = OnlinePayOrder.FindAll(ex, null, null, startRowIndex, numPerPage); long totalCount = OnlinePayOrder.FindCount(ex, null, null, startRowIndex, numPerPage); return(Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { total = totalCount, rows = list }), "text/plain")); //return Json(new { total = totalCount, rows = list }, JsonRequestBehavior.AllowGet); }
/// <summary> /// 查询订单支付状态 /// </summary> /// <returns></returns> /// <remarks>2016-12-20 杨浩 创建</remarks> public override Result QueryOrderState(string orderId) { var result = new Result() { Status = false }; string key = "0627eb791eadb57cd947dc32e59e563e"; var payOrder = new OnlinePayOrder(key); payOrder.MerchantId = "120140222"; //商户号 payOrder.RequestId = "9942"; //订单号 string hmac = Digest.HmacSign(payOrder.MerchantId + payOrder.RequestId, key); var client = new com.ekhing.Web.HttpClient("https://api.ehking.com/onlinePay/query"); string data = "{\"merchantId\":\"" + payOrder.MerchantId + "\",\"requestId\":\"" + payOrder.RequestId + "\",\"hmac\":\"" + hmac + "\"}"; string responseStr = client.Post(data); var _result = JObject.Parse(responseStr); string status = _result["status"].ToString(); if (status == "SUCCESS") { string _hmac = _result["hmac"].ToString(); string requestId = _result["requestId"].ToString(); string serialNumber = _result["serialNumber"].ToString(); string orderAmount = _result["orderAmount"].ToString(); result.Status = true; } else if (status == "FAILED") { result.Message = _result["error"].ToString(); } return(result); }
/// <summary> /// 微信支付异步通知 /// </summary> /// <returns></returns> public IActionResult notify() { XTrace.WriteLine("微信支付异步通知开始:"); try { ResponseHandler resHandler = new ResponseHandler(null); string return_code = resHandler.GetParameter("return_code"); string return_msg = resHandler.GetParameter("return_msg"); //配置 Core.Config cfg = Core.Config.GetSystemConfig(); string appId = cfg.WXAppId; // ConfigurationManager.AppSettings["WeixinAppId"]; string appSecrect = cfg.WXAppSecret; // ConfigurationManager.AppSettings["WeixinAppSecrect"]; string wxmchId = cfg.MCHId; // ConfigurationManager.AppSettings["WeixinMCHId"]; string wxmchKey = cfg.MCHKey; // ConfigurationManager.AppSettings["WeixinMCHKey"]; TenPayV3Info TenPayV3Info = new TenPayV3Info(appId, appSecrect, wxmchId, wxmchKey, Utils.GetServerUrl() + "/wxpayment/notify", Utils.GetServerUrl() + "/wxpayment/notify"); string res = null; resHandler.SetKey(TenPayV3Info.Key); //验证请求是否从微信发过来(安全) if (resHandler.IsTenpaySign() && return_code.ToUpper() == "SUCCESS") { res = "success"; //正确的订单处理 //直到这里,才能认为交易真正成功了,可以进行数据库操作,但是别忘了返回规定格式的消息! string out_trade_no = resHandler.GetParameter("out_trade_no"); //商户订单号 XTrace.WriteLine("微信异步通知订单号:" + out_trade_no + ";" + JsonConvert.SerializeObject(resHandler)); OnlinePayOrder payOrder = OnlinePayOrder.Find(OnlinePayOrder._.PayOrderNum == out_trade_no); if (payOrder == null) { XTrace.WriteLine($"支付成功,但是支付订单不存在:{out_trade_no}"); res = "wrong";//错误的订单处理 } else { if (payOrder.PaymentStatus == Utils.PaymentState[0]) { //更新支付订单 payOrder.PaymentStatus = Utils.PaymentState[1]; payOrder.ReceiveTime = DateTime.Now; payOrder.IsOK = 1; payOrder.Update(); //获取订单 Order order = Order.Find(Order._.OrderNum == payOrder.OrderNum); if (order != null) { order.PaymentStatus = Utils.PaymentState[1]; order.PayType = "微信支付"; if (order.MyType == (int)Utils.MyType.分销商认证) { order.OrderStatus = Utils.OrdersState[2]; } order.Update(); //如果是属于升级会员的,那要修改会员状态 if (order.MyType == (int)Utils.MyType.分销商认证 && order.OrderType > 0) { Member he = Member.FindById(order.UId); if (he.RoleId != order.OrderType) { he.RoleId = order.OrderType; he.IsVerifySellers = 1; he.Update(); } } //写入订单记录 OrderLog log = new OrderLog(); log.AddTime = DateTime.Now; log.OrderId = order.Id; log.OrderNum = order.OrderNum; log.UId = order.UId; log.Actions = "微信支付成功;订单号:" + order.OrderNum + ";金额:" + order.TotalPay.ToString("N2"); log.Insert(); } } } } else { res = "wrong";//错误的订单处理 } #region 记录日志 XTrace.WriteLine($"微信支付回调处理结果:{res}"); #endregion string xml = string.Format(@"<xml> <return_code><![CDATA[{0}]]></return_code> <return_msg><![CDATA[{1}]]></return_msg> </xml>", return_code, return_msg); return(Content(xml, "text/xml")); } catch (Exception ex) { new WeixinException(ex.Message, ex); throw; } }
public ReJson DoPayOrder(string ordernum, string random = "", string timeStamp = "", string signature = "") { //获取订单 Order entity = Order.Find(Order._.OrderNum == ordernum); if (entity == null) { //reJson.code = 40000; //reJson.message = "系统找不到本订单!"; //return reJson; return(new ReJson(40000, "系统找不到本订单!")); } //判断订单状态 if (entity.OrderStatus == Utils.OrdersState[3]) { //reJson.code = 40000; //reJson.message = "已完成订单不允许支付!"; //return reJson; return(new ReJson(40000, "已完成订单不允许支付!")); } if (entity.PaymentStatus != Utils.PaymentState[0]) { //reJson.code = 40000; //reJson.message = "当前订单支付状态不允许支付!"; //return reJson; return(new ReJson(40000, "当前订单支付状态不允许支付!")); } //获取用户并判断是否是已经注册用户 Member my = Member.FindById(entity.UId); if (my == null || string.IsNullOrEmpty(my.WeixinAppOpenId)) { //reJson.code = 40000; //reJson.message = "用户状态错误,无法使用本功能!"; //return reJson; return(new ReJson(40000, "用户状态错误,无法使用本功能!")); } //开始生成支付订单 OnlinePayOrder model = new OnlinePayOrder(); model.OrderId = entity.Id; model.OrderNum = entity.OrderNum; model.PayId = 1; model.PaymentNotes = "微信支付"; model.PaymentStatus = Utils.PaymentState[0]; model.PayOrderNum = Utils.GetOrderNum();//在线支付订单的订单号 model.PayType = "微信支付"; model.TotalPrice = entity.TotalPay; model.TotalQty = entity.TotalQty; model.UId = entity.UId; model.IP = Utils.GetIP(); model.IsOK = 0; model.AddTime = DateTime.Now; model.Insert(); //写入日志 OrderLog log = new OrderLog(); log.AddTime = DateTime.Now; log.OrderId = entity.Id; log.OrderNum = entity.OrderNum; log.UId = entity.UId; log.Actions = "用户使用微信支付;支付订单号:" + model.PayOrderNum; log.Insert(); Core.Config cfg = Core.Config.GetSystemConfig(); string appId = cfg.WXAppId; // ConfigurationManager.AppSettings["WeixinAppId"]; string appSecrect = cfg.WXAppSecret; // ConfigurationManager.AppSettings["WeixinAppSecrect"]; string wxmchId = cfg.MCHId; // ConfigurationManager.AppSettings["WeixinMCHId"]; string wxmchKey = cfg.MCHKey; // ConfigurationManager.AppSettings["WeixinMCHKey"]; TenPayV3Info TenPayV3Info = new TenPayV3Info(appId, appSecrect, wxmchId, wxmchKey, Utils.GetServerUrl() + "/wxpayment/notify", Utils.GetServerUrl() + "/wxpayment/notify"); TenPayV3Info.TenPayV3Notify = Utils.GetServerUrl() + "/wxpayment/notify"; XTrace.WriteLine("微信支付异步通知地址:" + TenPayV3Info.TenPayV3Notify); //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); var sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(26);//最多32位 var nonceStr = TenPayV3Util.GetNoncestr(); string rtimeStamp = Utils.GetTimeStamp(); //创建请求统一订单接口参数 var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, entity.Title, model.PayOrderNum, (int)(entity.TotalPay * 100), Utils.GetIP(), TenPayV3Info.TenPayV3Notify, Senparc.Weixin.TenPay.TenPayV3Type.JSAPI, my.WeixinAppOpenId, TenPayV3Info.Key, nonceStr); //返回给微信的请求 RequestHandler res = new RequestHandler(null); try { //调用统一订单接口 var result = TenPayV3.Unifiedorder(xmlDataInfo); XTrace.WriteLine("微信支付统一下单返回:" + JsonConvert.SerializeObject(result)); if (result.return_code == "FAIL") { //reJson.code = 40005; //reJson.message = result.return_msg; //return reJson; return(new ReJson(40005, result.return_msg)); } string nativeReqSign = res.CreateMd5Sign("key", TenPayV3Info.Key); //https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3 //paySign = MD5(appId=wxd678efh567hg6787&nonceStr=5K8264ILTKCH16CQ2502SI8ZNMTM67VS&package=prepay_id=wx2017033010242291fcfe0db70013231072&signType=MD5&timeStamp=1490840662&key=qazwsxedcrfvtgbyhnujmikolp111111) string paySign = Utils.MD5($"appId={TenPayV3Info.AppId}&nonceStr={nonceStr}&package=prepay_id={result.prepay_id}&signType=MD5&timeStamp={rtimeStamp}&key={TenPayV3Info.Key}").ToUpper(); string package = $"prepay_id={result.prepay_id}"; dynamic detail = new { timeStamp = rtimeStamp, nonceStr = nonceStr, package = package, signType = "MD5", paySign = paySign }; //reJson.code = 0; //reJson.message = "下单成功!"; //reJson.detail = detail; //return reJson; return(new ReJson(40000, "下单成功!", detail)); } catch (Exception ex) { res.SetParameter("return_code", "FAIL"); res.SetParameter("return_msg", "统一下单失败"); XTrace.WriteLine($"统一下单失败:{ex.Message}"); //reJson.code = 40005; //reJson.message = "统一下单失败,请联系管理员!"; return(new ReJson(40005, "统一下单失败,请联系管理员!")); } }