public ActionResult Notify() { string body = new StreamReader(Request.Body).ReadToEnd(); byte[] requestData = Encoding.UTF8.GetBytes(body); Stream inputStream = new MemoryStream(requestData); using (StreamReader sr = new StreamReader(inputStream)) { var xml = sr.ReadToEnd(); var notify = new WechatPayResponseHandler(xml); ShopPayInfo shopPayInfo = shopDb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.AppId == notify.Appid) .Where(m => m.MchId == notify.Mchid) .FirstOrDefault(); if (shopPayInfo == null) { return(Fail()); } PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); if (notify.isTenpaySign(payProxy.MakeSign)) { if (notify.ReturnCode == "SUCCESS" && notify.ResultCode == "SUCCESS") { try { //此处可以在添加相关处理业务,校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致,一致后方可更新数据库表中的记录。 var rechange = db.Query <MemberTradeForRechange>() .Where(m => m.TradeNo == notify.OutTradeNo) .FirstOrDefault(); if (rechange == null) { return(Fail()); } if (rechange.TotalFee != notify.TotalFee) { return(Fail()); } var result = payProxy.GetPayResult(rechange); if (result.ReturnCode == "SUCCESS" && result.ResultCode == "SUCCESS" && result.TradeState == "SUCCESS") { rechange.SetFinish(printDbContext, thirdConfig, shopDb, db, options, result, _logger); db.SaveChanges(); shopDb.SaveChanges(); return(Success()); } else { _logger.LogError("微信支付通知错误,查询支付结果:{0}", Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(Fail()); } } catch (Exception e) { _logger.LogError("微信支付通知错误:{0}\n{1}", e.Message, e.StackTrace); return(Fail()); } } return(Fail()); } else { return(Fail()); } } }
public APIResult BeginRechange([FromBody] BeginRechangeArgsModel args) { //if (args.TotalFee <= 0) throw new Exception("充值金额需要大于0"); if (args.ShopFlag == null) { throw new ArgumentNullException("ShopFlag"); } if (!args.ConglomerationOrderId.HasValue) { throw new ArgumentNullException("ConglomerationOrderId"); } var memberId = GetMemberId(); ConglomerationOrder conglomerationOrder = shopDb.GetSingle <ConglomerationOrder>(args.ConglomerationOrderId.Value); if (conglomerationOrder == null) { throw new Exception("订单不存在"); } ShopPayInfo shopPayInfo = shopDb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.ShopFlag == args.ShopFlag) .Where(m => m.ShopFlag == args.ShopFlag && m.IsEnable) .FirstOrDefault(); if (shopPayInfo == null) { throw new Exception("当前商铺没有设置好支付信息。"); } if (shopPayInfo.PayWay == PayWay.Wechat) { throw new Exception("拼团功能暂不支持,请联系商户修改支付模式"); } PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); var tradeNo = "SP" + DateTime.Now.Ticks + CommonUtil.CreateNoncestr(5); var tradeDate = DateTime.Now.ToString("yyyyMMddHHmmss"); var model = new MemberTradeForRechange() { AddIP = GetIp(), AddTime = DateTime.Now, AddUser = GetUsername(), MemberId = memberId, Detail = "拼团订单支付" + conglomerationOrder.Payment.Value + "分", OutBank = "", PayChannel = payProxy.PayChannel, Status = MemberTradeForRechangeStatus.未完成, TimeExpire = DateTime.Now, TimeStart = DateTime.Now, Title = "拼团订单支付", TotalFee = conglomerationOrder.Payment.Value, ConglomerationOrderId = args.ConglomerationOrderId.Value, MoneyOffRuleId = 0, TradeNo = tradeNo, OrderType = OrderType.拼团订单, OrderId = args.ConglomerationOrderId.Value, ShopOrderId = 0, PayWay = (int)shopPayInfo.PayWay, ShopId = shopPayInfo.ShopId }; db.AddToMemberTradeForRechange(model); var openId = GetOpenId(); var payInfo = payProxy.GetPayInfo(model, openId); db.SaveChanges(); if (shopPayInfo.PayWay == PayWay.Wechat) { new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, args.ConglomerationOrderId.Value, OrderType.拼团订单, model, payInfo); return(Success(new { payInfo, TradeNo = tradeNo })); } else { PayInfoModel infoModel = new PayInfoModel(); var payinfo = JsonConvert.DeserializeObject <PayInfo>(payInfo.ToString()); infoModel.payInfo = payinfo; new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, args.ConglomerationOrderId.Value, OrderType.拼团订单, model, payInfo); return(Success( infoModel )); } }
public APIResult BeginRecharge([FromBody] BeginRechangeArgsModel args) { try { if (args.ShopFlag == null) { throw new ArgumentNullException("ShopFlag"); } var memberId = GetMemberId(); Shop shop = shopDb.Query <Shop>().FirstOrDefault(m => !m.IsDel && m.Flag == args.ShopFlag); ShopPayInfo shopPayInfo = shopDb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.ShopFlag == args.ShopFlag && m.IsEnable) .FirstOrDefault(); if (shopPayInfo == null) { throw new Exception("当前商铺没有设置好支付信息。"); } int rechangeAmount, presentedAmount; if (args.RechargeType == RechargeType.固定金额) { if (!args.TopUpId.HasValue) { throw new Exception("TopUpId不能为空"); } var topUpSet = shopDb.Query <ShopTopUpSet>() .FirstOrDefault(m => !m.IsDel && m.Id == args.TopUpId.Value && m.ShopId == shop.Id); if (topUpSet == null) { throw new Exception("该商铺不存在此项充值"); } rechangeAmount = topUpSet.FixationTopUpAmount; presentedAmount = topUpSet.PresentedAmount; } else { if (!args.Amount.HasValue) { throw new Exception("充值金额不能为空"); } rechangeAmount = args.Amount.Value; var customTopUpSet = shopDb.Query <ShopCustomTopUpSet>() .FirstOrDefault(m => !m.IsDel && m.ShopId == shop.Id); if (customTopUpSet == null) { throw new Exception("该商铺不存在此项充值"); } if (args.Amount.Value < customTopUpSet.StartAmount) { throw new Exception("充值金额少于起充金额"); } if (args.Amount.Value > customTopUpSet.MeetAmount) { presentedAmount = (int)((args.Amount.Value - customTopUpSet.MeetAmount) * (customTopUpSet.Additional / 100.00D)); } else { presentedAmount = 0; } } var shopMember = BLL.Servers.ShopMemberServer.GetShopMember(shopDb, shop.Id, memberId); var memberRecharge = new ShopMemberRecharge() { Amount = rechangeAmount, PresentedAmount = presentedAmount, ShopMemberId = shopMember.Id, TransactionTime = DateTime.Now, Status = ShopMemberTransactionStatus.未完成 }; shopDb.Add(memberRecharge); shopDb.SaveChanges(); PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); var tradeNo = "SP" + DateTime.Now.Ticks + CommonUtil.CreateNoncestr(5); var tradeDate = DateTime.Now.ToString("yyyyMMddHHmmss"); var model = new MemberTradeForRechange() { AddIP = GetIp(), AddTime = DateTime.Now, AddUser = GetUsername(), MemberId = memberId, Detail = $"用户支付{rechangeAmount}分", OutBank = "", PayChannel = payProxy.PayChannel, Status = MemberTradeForRechangeStatus.未完成, TimeExpire = DateTime.Now, TimeStart = DateTime.Now, Title = "用户充值", ShopOrderId = memberRecharge.Id, TotalFee = rechangeAmount, TradeNo = tradeNo, PayWay = (int)shopPayInfo.PayWay, RowVersion = DateTime.Now, OrderType = OrderType.充值订单, ConglomerationOrderId = 0, ShopId = shopPayInfo.ShopId }; finaceDb.AddToMemberTradeForRechange(model); var openId = GetOpenId(); var payInfo = payProxy.GetPayInfo(model, openId); finaceDb.SaveChanges(); if (shopPayInfo.PayWay == PayWay.Wechat) { new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, rechangeAmount, OrderType.普通订单, model, payInfo); return(Success(new { payInfo, TradeNo = tradeNo })); } else { var infoModel = new Core.Finance.WechatPay.PayAPIModels.PayInfoModel(); var payinfo = JsonConvert.DeserializeObject <ZRui.Web.Core.Finance.WechatPay.PayAPIModels.PayInfo>(payInfo.ToString()); infoModel.payInfo = payinfo; //new PayOrRefundUtil<object>(LogDbContext).PayAction("Pay" // , BLL.Log.PayOrRefundType.支付, args.ShopOrderId.Value, OrderType.普通订单, model, payInfo); return(Success( infoModel )); } } catch (Exception ex) { //new PayOrRefundUtil<object>(LogDbContext).PayAction("Pay" // , BLL.Log.PayOrRefundType.支付, args.ShopOrderId.Value, OrderType.普通订单, null, null, ex.Message + "【StackTrace】" + ex.StackTrace); throw new Exception(ex.Message); } }
public ActionResult Notify() { _logger.LogInformation("=====================支付回调开始======================"); //var logDb = ZRui.Web.BLL.DbContextFactory.LogDbContext; //logDb.Add<TaskLog>(new TaskLog() { AddTime = DateTime.Now, TaskName = "进入Notify" }); string body = new StreamReader(Request.Body).ReadToEnd(); byte[] requestData = Encoding.UTF8.GetBytes(body); Stream inputStream = new MemoryStream(requestData); using (StreamReader sr = new StreamReader(inputStream)) { try { var xml = sr.ReadToEnd(); _logger.LogInformation($"获取到result{xml}"); //logDb.Add<TaskLog>(new TaskLog() { AddTime = DateTime.Now, TaskName = "Notify", ExeResult = xml }); //logDb.SaveChanges(); var notify = new SwiftpassPayResponseHandler(null, xml); ShopPayInfo shopPayInfo = shopdb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.AppId == notify.SwiftpassAppid) .Where(m => m.MchId == notify.SwiftpassMchid) .FirstOrDefault(); if (shopPayInfo == null) { return(Content("failure1")); } PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); // if (notify.isTenpaySign()) //{ if (notify.Status == 0 && notify.ResultCode == 0 && notify.TradeState != "NOTPAY") { _logger.LogInformation("支付成功"); //此处可以在添加相关处理业务,校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致,一致后方可更新数据库表中的记录。 var rechange = db.Query <MemberTradeForRechange>() .Where(m => m.TradeNo == notify.OutTradeNo) .FirstOrDefault(); if (rechange == null) { throw new Exception("指定的OutTradeNo不存在"); } if (rechange.TotalFee != notify.TotalFee) { throw new Exception("指定的金额不对应"); } rechange.OutBank = notify.Xml; rechange.MechanismTradeNo = notify.TransactionId; if (notify.getAllParameters()["result_code"].ToString() == "0") { rechange.SetFinish(printDbContext, shopdb, db, woptions, thirdConfig, _logger); db.SaveChanges(); shopdb.SaveChanges(); _logger.LogInformation("回调成功返回success"); if (rechange.ConglomerationOrderId.HasValue) { ShopConglomerationOrderOptions.MemberInform(shopdb, _memberDbContext, rechange.ConglomerationOrderId.Value, _logger); ShopConglomerationActivityOptions.NotifyOkRemoveList(rechange, shopdb, _logger); } _logger.LogInformation("=====================支付回调结束======================"); return(Content("success")); } else { _logger.LogInformation("回调失败返回failure1"); _logger.LogInformation("=====================支付回调结束======================"); return(Content("failure1")); } //var result = payProxy.GetPayResult(rechange); //_logger.LogInformation($"获取到result{result.Xml}"); // //if (result.TradeState != "NOTPAY") //{ // if (rechange.Status == MemberTradeForRechangeStatus.未完成) // { // if (rechange.TotalFee != result.TotalFee) throw new Exception("指定的金额不对应"); // rechange.OutBank = result.Xml; // rechange.MechanismTradeNo = result.TransactionId; // switch (result.TradeState) // { // case "SUCCESS": // rechange.SetFinish(printDbContext, shopdb, db, woptions, _logger); // break; // case "CLOSED": // rechange.Status = MemberTradeForRechangeStatus.取消; // break; // default: // db.SetMemberTradeForRechangeFail(rechange, result.TradeState); // break; // } // } //} } else { _logger.LogInformation($"=====================支付回调结束======================"); return(Content("failure1")); } // } //else //{ // return Content("failure2"); //} } catch (Exception e) { _logger.LogInformation($"回调错误{e}"); return(Content("failure1")); } } }
public APIResult BeginRechange([FromBody] BeginRechangeArgsModel args) { try { if (args.ShopFlag == null) { throw new ArgumentNullException("ShopFlag"); } if (!args.ShopOrderId.HasValue) { throw new ArgumentNullException("ShopOrderId"); } var memberId = GetMemberId(); ShopOrder shopOrder = shopDb.GetSingle <ShopOrder>(args.ShopOrderId.Value); if (shopOrder == null) { throw new Exception("订单不存在"); } ShopPayInfo shopPayInfo = shopDb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.ShopFlag == args.ShopFlag && m.IsEnable) .FirstOrDefault(); if (shopPayInfo == null) { throw new Exception("当前商铺没有设置好支付信息。"); } //排除扫码 自助点餐 var checkThirdResult = CheckThird(shopOrder, shopOrder.ShopId); if (!checkThirdResult.Success) { throw new Exception(checkThirdResult.Message); } PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); var tradeNo = "SP" + DateTime.Now.Ticks + CommonUtil.CreateNoncestr(5); var tradeDate = DateTime.Now.ToString("yyyyMMddHHmmss"); var model = new MemberTradeForRechange() { AddIP = GetIp(), AddTime = DateTime.Now, AddUser = GetUsername(), MemberId = memberId, Detail = "用户支付" + shopOrder.Payment.Value + "分", OutBank = "", PayChannel = payProxy.PayChannel, Status = MemberTradeForRechangeStatus.未完成, TimeExpire = DateTime.Now, TimeStart = DateTime.Now, Title = "用户支付", TotalFee = shopOrder.Payment.Value, ShopOrderId = args.ShopOrderId.Value, MoneyOffRuleId = shopOrder.MoneyOffRuleId, TradeNo = tradeNo, PayWay = (int)shopPayInfo.PayWay, //ShopType = shopOrder RowVersion = DateTime.Now, OrderType = OrderType.普通订单, OrderId = args.ShopOrderId.Value, ConglomerationOrderId = 0, ShopId = shopPayInfo.ShopId }; finaceDb.AddToMemberTradeForRechange(model); // var openId = "oYBBo5PUP3TF37pUNiVvc7j5gJ6k"; //"oYBBo5PUP3TF37pUNiVvc7j5gJ6k";// var openId = GetOpenId(); //"oYBBo5PUP3TF37pUNiVvc7j5gJ6k";// var payInfo = payProxy.GetPayInfo(model, openId); finaceDb.SaveChanges(); if (shopPayInfo.PayWay == PayWay.Wechat) { new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, args.ShopOrderId.Value, OrderType.普通订单, model, payInfo); return(Success(new { payInfo, TradeNo = tradeNo })); } else { PayInfoModel infoModel = new PayInfoModel(); var payinfo = JsonConvert.DeserializeObject <ZRui.Web.Core.Finance.WechatPay.PayAPIModels.PayInfo>(payInfo.ToString()); infoModel.payInfo = payinfo; new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, args.ShopOrderId.Value, OrderType.普通订单, model, payInfo); return(Success( infoModel )); } } catch (Exception ex) { new PayOrRefundUtil <object>(LogDbContext).PayAction("Pay" , BLL.Log.PayOrRefundType.支付, args.ShopOrderId.Value, OrderType.普通订单, null, null, ex.Message + "【StackTrace】" + ex.StackTrace); throw new Exception(ex.Message); } //if (args.TotalFee <= 0) throw new Exception("充值金额需要大于0"); }