public override void OnException(HttpActionExecutedContext filterContext) { base.OnException(filterContext); ResultModel result = new ResultModel() { Code = EnumHelper.GetValue(EnumResultCode.未知异常) }; try { if (filterContext != null && filterContext.Exception != null) { //异步记录日志 Log4netHelper.Get(KeyModel.Log.ExceptionName).AsyncWriter(filterContext.Exception.ToString()); result = DoubleXHelper.GetResult(filterContext.Exception); } } catch (Exception ex) { //异步记录日志 Log4netHelper.Get(KeyModel.Log.ExceptionName).AsyncWriter(ex.ToString()); result = DoubleXHelper.GetResult(ex); } finally { filterContext.Response = WebApiHelper.ToHttpResponseMessage(result); } }
/// <summary> /// 页面-支付宝支付通知 /// </summary> public ActionResult AlipayNotify(RequestModel request) { //支付宝通知日志 Log4netHelper.Get(KeyModel.Log.PayNotification).AsyncWriter(request.JsonString); string tradStatus = JsonHelper.GetValue(request.Obj, "trade_status"); if (!(tradStatus.ToLower() == "trade_success")) { return(Content("fail")); } string tradeNo = JsonHelper.GetValue(request.Obj, "out_trade_no"); decimal moneyValue = DecimalHelper.Get(JsonHelper.GetValue(request.Obj, "total_fee")); string rechrageRecordId = JsonHelper.GetValue(request.Obj, "trade_no"); if (VerifyHelper.IsEmpty(tradeNo) || VerifyHelper.IsEmpty(moneyValue)) { throw new MessageException(EnumMessageCode.信息错误); } try { rechargeRecordService.RechargeSuccess(tradeNo, moneyValue, rechrageRecordId); } catch (Exception ex) { throw ex; } return(Content("success")); }
//1. 不支持exception记录(事件中) //2. 无法捕捉到500之外的http exception //3. controller之外抛出的异常无法处理 //4. ajax调用出现exception时,会将错误页面内容返回(己处理了) public override void OnException(ExceptionContext filterContext) { //如果异常未处理 if (!filterContext.ExceptionHandled) { //异步记录日志 Log4netHelper.Get(KeyModel.Log.ExceptionName).AsyncWriter(filterContext.Exception.ToString()); //Log4netHelper.Get(KeyModel.Log.ServiceName).Writer("这里是自定义错误信息"); //返回操作 if (VerifyHelper.IsAjax(filterContext.HttpContext)) { //当结果为json时,设置异常已处理(如有单独的Attribute记录日志,这里不需要设置,如设置的true的话就不会再跑记录日志的attribute) filterContext.ExceptionHandled = true; filterContext.Result = MvcHelper.ToJsonResult(DoubleXHelper.GetResult(filterContext.Exception)); } else { filterContext.ExceptionHandled = true; filterContext.Result = new RedirectResult(WebHelper.GetErrorUrl(DoubleXHelper.GetResult(filterContext.Exception))); } } else { base.OnException(filterContext); } }
/// <summary> /// 页面-支付宝支付跳转 /// </summary> public ActionResult AlipayReturn(RequestModel request) { //异步记录日志 Log4netHelper.Get(KeyModel.Log.PayNotification).AsyncWriter(request.JsonString); var result = WebHelper.GetResult(request); if (result.Code == EnumHelper.GetValue(EnumResultCode.操作成功)) { //支付失败 string returnSuccess = JsonHelper.GetValue(request.Obj, "is_success"); string tradStatus = JsonHelper.GetValue(request.Obj, "trade_status"); if (!(returnSuccess.ToLower() == "t" && tradStatus.ToLower() == "trade_success")) { throw new MessageException(EnumMessageCode.支付失败); } string tradeNo = JsonHelper.GetValue(request.Obj, "out_trade_no"); decimal moneyValue = DecimalHelper.Get(JsonHelper.GetValue(request.Obj, "total_fee")); string rechrageRecordId = JsonHelper.GetValue(request.Obj, "trade_no"); if (VerifyHelper.IsEmpty(tradeNo) || VerifyHelper.IsEmpty(moneyValue)) { throw new MessageException(EnumMessageCode.信息错误); } try { rechargeRecordService.RechargeSuccess(tradeNo, moneyValue, rechrageRecordId); } catch (Exception ex) { throw ex; } } return(new RedirectResult(WebHelper.GetMemberUrl("/trade/rechargerecord"))); }
//通知信息 public override void ProcessNotify() { WxPayData notifyData = GetNotifyData(); Log4netHelper.Get(KeyModel.Log.PayNotification).AsyncWriter(notifyData.ToJson()); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); context.Response.Write(res.ToXml()); context.Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); string tradeNo = StringHelper.Get(notifyData.GetValue("out_trade_no")); decimal moneyValue = DecimalHelper.Get(notifyData.GetValue("total_fee")) / 100; string rechrageRecordId = ""; bool isFunction = false; if (QueryOrder(transaction_id)) { //执执判断是否执行成功 isFunction = fun(tradeNo, moneyValue, rechrageRecordId); } if (!isFunction) { WxPayData res = new WxPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); context.Response.Write(res.ToXml()); context.Response.End(); } else { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); context.Response.Write(res.ToXml()); context.Response.End(); } }
/// <summary> /// 页面-微信支付通知 /// </summary> /// <param name="request"></param> /// <returns></returns> public ActionResult WxNotify(RequestModel request) { //微信支付通知日志 Log4netHelper.Get(KeyModel.Log.PayNotification).AsyncWriter(request.JsonString); //支付成功操作委托 Func <string, decimal, string, bool> function = (string tradeNo, decimal money, string rechargeRecordId) => { return(rechargeRecordService.RechargeSuccess(tradeNo, money, rechargeRecordId)); }; //微信支付通知处理 WxPayNativeNotify not = new WxPayNativeNotify(System.Web.HttpContext.Current, function); not.ProcessNotify(); return(Content("success")); }
/// <summary> /// 页面-支付跳转 /// </summary> /// <returns></returns> public ActionResult ToPlatform(RequestModel request) { var result = WebHelper.GetResult(request); if (result.Code == EnumHelper.GetValue(EnumResultCode.操作成功)) { Guid id = GuidHelper.Get(JsonHelper.GetValue(request.Obj, "Id")); var rechargeRecordModel = rechargeRecordService.Get(x => x.Id == id); if (VerifyHelper.IsEmpty(id) || VerifyHelper.IsEmpty(rechargeRecordModel)) { throw new MessageException(EnumMessageCode.信息错误); } result.Obj = rechargeRecordModel; ViewBag.PayHtml = rechargeRecordService.GetPaymentPlatformHtml(rechargeRecordModel); //支付申请 string logMsg = string.Format("{0}----\r\n---{1}", JsonHelper.Serialize(rechargeRecordModel), ViewBag.PayHtml); Log4netHelper.Get(KeyModel.Log.PaySubmit).AsyncWriter(logMsg); } return(View(result)); }
/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public WxPayData GetNotifyData() { //接收从微信后台POST过来的数据 System.IO.Stream requestInputStream = context.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = requestInputStream.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } requestInputStream.Flush(); requestInputStream.Close(); requestInputStream.Dispose(); //支付日志 Log4netHelper.Get(KeyModel.Log.PayNotification).AsyncWriter(builder.ToString()); //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { data.FromXml(builder.ToString()); } catch (WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); } return(data); }