public APIResult GetRechangeResult([FromBody] GetRechangeResultArgsModel args) { if (string.IsNullOrEmpty(args.TradeNo)) { throw new ArgumentNullException("TradeNo"); } if (args.ShopFlag == null) { throw new ArgumentNullException("ShopFlag"); } var memberId = GetMemberId(); ShopPayInfo shopPayInfo = shopDb.Query <ShopPayInfo>() .Where(m => !m.IsDel) .Where(m => m.ShopFlag == args.ShopFlag) .FirstOrDefault(); if (shopPayInfo == null) { throw new Exception("当前商铺没有设置好支付信息。"); } PayProxyBase payProxy = proxyFactory.GetProxy(shopPayInfo); var rechange = db.Query <MemberTradeForRechange>() .Where(m => m.MemberId == memberId && m.TradeNo == args.TradeNo) .FirstOrDefault(); if (rechange == null) { throw new Exception("指定的纪录不存在"); } _logger.LogInformation($"获取到rechange{rechange.Id},状态 {rechange.Status}"); if (rechange.Status == MemberTradeForRechangeStatus.未完成) { var result = payProxy.GetPayResult(rechange); _logger.LogInformation($"获取到result{result.Xml}"); //rechange.SetFinish(shopDb,db,options, result,_logger); db.SaveChanges(); } return(Success(rechange)); }
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()); } } }