private static void SavePayNotifyInfo(PayModuleEnum payModule, PaymentNotify paymentNotify) { var payNotifyInfo = new PayNotifyInfo(); payNotifyInfo.ModuleName = payModule.ToString(); payNotifyInfo.NotifyID = Guid.NewGuid(); payNotifyInfo.appid = paymentNotify.appid; payNotifyInfo.attach = paymentNotify.attach; payNotifyInfo.bank_type = paymentNotify.bank_type; payNotifyInfo.cash_fee = paymentNotify.cash_fee; payNotifyInfo.cash_fee_type = paymentNotify.cash_fee_type; payNotifyInfo.coupon_count = paymentNotify.coupon_count; payNotifyInfo.coupon_fee = paymentNotify.coupon_fee; payNotifyInfo.CreateTime = DateTime.Now; payNotifyInfo.device_info = paymentNotify.device_info; payNotifyInfo.err_code = paymentNotify.err_code; payNotifyInfo.err_code_des = paymentNotify.err_code_des; payNotifyInfo.fee_type = paymentNotify.fee_type; payNotifyInfo.is_subscribe = paymentNotify.is_subscribe; payNotifyInfo.mch_id = paymentNotify.mch_id; payNotifyInfo.nonce_str = paymentNotify.nonce_str; payNotifyInfo.openid = paymentNotify.openid; payNotifyInfo.out_trade_no = paymentNotify.out_trade_no; payNotifyInfo.result_code = paymentNotify.result_code; payNotifyInfo.return_code = paymentNotify.return_code; payNotifyInfo.return_msg = paymentNotify.return_msg; payNotifyInfo.sign = paymentNotify.sign; payNotifyInfo.time_end = paymentNotify.time_end; payNotifyInfo.total_fee = paymentNotify.total_fee; payNotifyInfo.trade_type = paymentNotify.trade_type; payNotifyInfo.transaction_id = paymentNotify.transaction_id; payNotifyInfo.Add(); }
public JsonResult CheckPay(string orderId, string resultType) { try { if (!Request.IsAjaxRequest()) { return(Json(new { status = -2, msg = "非法请求!" })); } var requestInfo = PayRequestDAL.GetValidPayRequest(orderId, this._payType); if (requestInfo == null || requestInfo.SysNo <= 0) { return(Json(new { status = -2, msg = "系统不存在该订单的支付请求,请自行返回下单页面!" })); } var notifyInfo = new PayNotifyInfo() { OrderId = requestInfo.OrderId, PaymentAmt = requestInfo.PaymentAmt.ToString(), TradeNo = "", ExtTradeNo = "", Result = ((int)ResultStatus.Failure).ToString(), }; int status = 0; var resultInfo = PayResultDAL.GetValidPayResult(orderId, this._payType); if (resultInfo != null && resultInfo.SysNo > 0) { status = 1;//有支付成功记录 notifyInfo.TradeNo = resultInfo.TradeNo; notifyInfo.ExtTradeNo = resultInfo.ExtTradeNo; notifyInfo.Result = resultInfo.ExecuteResult.ToString(); } //当支付页面收到的支付结果为成功时,不管有没有收到支付成功的异步回执,返回到支付结果着陆页时都提示支付成功 if (string.Equals(resultType, "ok", StringComparison.CurrentCultureIgnoreCase)) { notifyInfo.Result = ((int)ResultStatus.Success).ToString(); } var resultInterface = Builder.BuildWeChatPayResult(); var url = resultInterface.GetReturnUrl(requestInfo, notifyInfo); return(Json(new { status = status, url = url })); } catch (Exception ex) { string log = string.Format(@"校验支付结果发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:orderId={3}", Environment.NewLine, ex.Message, ex.StackTrace, orderId); LogWriter.WriteLog(log, AppConfig.Global.SwiftPassWeChatPayLogFolder, ExceptionHelper.ExceptionLevel.Exception); return(Json(new { status = -1, msg = "系统执行时发生异常!", error = ex.Message })); } }
/// <summary> /// 获取支付结果通知对象 /// </summary> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> protected PayNotifyInfo GetPayNotifyInfo(PayResultInfo resultInfo) { var notifyInfo = new PayNotifyInfo() { OrderId = resultInfo.OrderId, TradeNo = resultInfo.TradeNo, PaymentAmt = resultInfo.PaymentAmt.ToString(), ExtTradeNo = resultInfo.ExtTradeNo, Result = resultInfo.ExecuteResult.ToString(), }; return(notifyInfo); }
/// <summary> /// 获取返回链接 /// </summary> /// <param name="requestInfo">支付请求记录</param> /// <param name="notifyInfo">支付结果通知</param> /// <returns></returns> public virtual string GetReturnUrl(PayRequestInfo requestInfo, PayNotifyInfo notifyInfo) { if (requestInfo != null && !string.IsNullOrWhiteSpace(requestInfo.ReturnUrl)) { var setting = JsonHelper.GetDefaultSettings(); setting.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; string data = JsonHelper.Serialize(notifyInfo, setting); string sign = SignManager.CreateSign(requestInfo.AppId, data).Data; string returnUrl = requestInfo.ReturnUrl + (requestInfo.ReturnUrl.IndexOf("?") > 0 ? "&" : "?"); returnUrl += string.Format("appId={0}&sign={1}&data={2}", requestInfo.AppId, sign, data); return(returnUrl); } return(null); }
/// <summary> /// 支付完成返回页面 /// </summary> /// <returns></returns> public ActionResult OnlinePayReturn() { //请求参数报文 string requestParams = string.Empty; //支付完成通知对象 var notifyInfo = new PayNotifyInfo(); try { var resultInterface = Builder.BuildAllinpayResult(); requestParams = HttpUtility.UrlDecode(Request.Form.ToString()); var resultInfo = new PayResultInfo(); //校验请求报文 var checkResult = resultInterface.CheckRequest(requestParams, resultInfo); if (checkResult.Status != ResultStatus.Success) { ViewBag.ErrorMsg = checkResult.Message; return(View()); } //解析支付结果 var resolveResult = resultInterface.ResolveRequest(requestParams, resultInfo); if (resolveResult.Status != ResultStatus.Success) { ViewBag.ErrorMsg = resolveResult.Message; return(View()); } //获取支付完成返回地址,如果有值,则跳转到该地址去 notifyInfo = resolveResult.Data; var requestInfo = PayRequestDAL.GetValidPayRequest(notifyInfo.OrderId, this._payType); var returnUrl = resultInterface.GetReturnUrl(requestInfo, notifyInfo); if (!string.IsNullOrEmpty(returnUrl)) { return(Redirect(returnUrl)); } } catch (Exception ex) { string log = string.Format(@"支付完成返回发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:{3}", Environment.NewLine, ex.Message, ex.StackTrace, requestParams); LogWriter.WriteLog(log, AppConfig.Global.AllinpayLogFolder, ExceptionHelper.ExceptionLevel.Exception); ViewBag.ErrorMsg = "系统执行发生异常:" + ex.Message; } ViewBag.NotifyInfo = notifyInfo; return(View()); }
public JsonResult GetReturnUrl(int resultSysNo) { try { var resultInfo = PayResultDAL.GetPayResult(resultSysNo); if (resultInfo == null || resultInfo.SysNo <= 0) { return(Json(new { status = 0, msg = "支付结果记录不存在" })); } if (resultInfo.RequestSysNo <= 0) { return(Json(new { status = 0, msg = "支付结果记录无对应的请求记录" })); } var requestInfo = PayRequestDAL.GetPayRequest(resultInfo.RequestSysNo); if (requestInfo == null || requestInfo.SysNo <= 0) { return(Json(new { status = 0, msg = "支付结果记录对应的请求记录不存在" })); } if (!requestInfo.ReturnUrl.IsUrl()) { return(Json(new { status = 0, msg = "支付请求记录的返回地址不是有效URL" })); } var notifyInfo = new PayNotifyInfo() { OrderId = resultInfo.OrderId, PaymentAmt = resultInfo.PaymentAmt.ToString(), TradeNo = resultInfo.TradeNo, ExtTradeNo = resultInfo.ExtTradeNo, Result = resultInfo.ExecuteResult.ToString(), }; var resultInterface = Builder.BuildAlipayResult(); string returnUrl = resultInterface.GetReturnUrl(requestInfo, notifyInfo); return(Json(new { status = 1, href = returnUrl })); } catch (Exception ex) { LogWriter.WriteLog(ex.Message + "\r\n" + ex.StackTrace, _logDirName, ExceptionHelper.ExceptionLevel.Exception); return(Json(new { status = -1, msg = ex.Message, error = ex.StackTrace })); } }
/// <summary> /// 通知业务系统支付成功 /// </summary> /// <param name="payResult"></param> /// <param name="payRequest"></param> /// <returns></returns> public static bool NotifyBack(PayResultInfo payResult, PayRequestInfo payRequest) { //支付结果记录对象无效,则不执行 if (payResult == null || payResult.SysNo <= 0) { return(false); } //支付请求记录对象无效,则不执行 if (payRequest == null || payRequest.SysNo <= 0) { return(false); } //支付结果记录与支付请求记录不对应,则不执行 if (payRequest.SysNo != payResult.RequestSysNo) { return(false); } //支付结果记录未成功执行,或者已通知,则不执行 if (payResult.ExecuteResult != (int)ResultStatus.Success || payResult.NotifyStatus != (int)AppEnum.GlobalStatus.Invalid) { return(false); } //支付请求记录中不存在有效的通知地址,则不执行 if (!payRequest.NotifyUrl.IsUrl()) { return(false); } var payNotify = new PayNotifyInfo() { OrderId = payResult.OrderId, TradeNo = payResult.TradeNo, PaymentAmt = payResult.PaymentAmt.ToString(), Result = ((int)ResultStatus.Success).ToString(), }; string data = JsonHelper.Serialize(payNotify); string sign = SignManager.CreateSign(data).Data; string param = "sign=" + sign; param += "&data=" + data; var response = HttpHelper.HttpRequest("POST", payRequest.NotifyUrl, param, 10000); string status = JsonHelper.GetJsonNode(response, "status"); string msg = JsonHelper.GetJsonNode(response, "msg"); var notifyBack = new NotifyBackInfo() { ResultSysNo = payResult.SysNo, Status = (int)AppEnum.GlobalStatus.Valid, CreateTime = DateTime.Now, ResponseData = response, }; if (status != "1") { notifyBack.Status = (int)AppEnum.GlobalStatus.Invalid; notifyBack.Msg = msg; } else { payResult.NotifyStatus = (int)AppEnum.GlobalStatus.Valid; PayResultDAL.Update(payResult); } PayResultDAL.InsertNotifyBack(notifyBack); bool result = notifyBack.Status == (int)AppEnum.GlobalStatus.Valid; return(result); }
/// <summary> /// WAP在线支付JSPAI页面 /// </summary> /// <param name="requestSysNo"></param> /// <param name="code"></param> /// <returns></returns> public ActionResult OnlineWapPayJSAPI(int requestSysNo, string code) { try { var onlinePay = Builder.BuildOnlinePay(); //获取支付请求记录 var requestInfo = PayRequestDAL.GetPayRequest(requestSysNo); if (requestInfo == null || requestInfo.SysNo <= 0) { ViewBag.ErrorMsg = "您尚未发起支付请求,请返回后重新提交"; return(View()); } //校验支付环境 var checkResult = onlinePay.CheckBrowserType(AppEnum.BrowserType.WeChat, requestInfo); if (checkResult.Status != ResultStatus.Success) { ViewBag.ErrorMsg = checkResult.Message; return(View()); } #region 组装浏览器调起JS API支付所需的参数 try { var jsApiPay = new JsApiPay(System.Web.HttpContext.Current); //获取授权用户信息 jsApiPay.GetOpenidAndAccessTokenFromCode(code); //获取订单支付金额 int paymentAmt = (int)(requestInfo.PaymentAmt * 100);//微信支付金额的单位为“分”,所以要乘以100 //异步通知url string notifyUrl = string.Format("http://{0}/SwiftPassWeChatPay/OnlinePayNotify", AppConfig.Global.Domain); var orderInfo = JsonHelper.Deserialize <PayOrderInfo>(requestInfo.RequestData); DateTime orderEndTime = DateTime.ParseExact(orderInfo.OrderTime, "yyyyMMddHHmmss", new CultureInfo("zh-CN", true)).AddHours(24); DateTime minExpireTime = DateTime.Now.AddMinutes(6);//为保险,多加1分钟 //交易过期时间(最短过期时间间隔必须大于5分钟) string expireTime = (orderEndTime > minExpireTime ? orderEndTime : minExpireTime).ToString("yyyyMMddHHmmss"); //获取调起JS API的参数 ViewBag.JsApiParams = SwiftPassPayApi.WeChatJsApiPay(requestInfo.OrderId, paymentAmt, notifyUrl, expireTime, jsApiPay.openid); //订单编号 ViewBag.OrderId = requestInfo.OrderId; //异常时返回的业务系统着陆页面 var resultInterface = Builder.BuildSwiftPassWeChatPayResult(); var notifyInfo = new PayNotifyInfo() { OrderId = requestInfo.OrderId, TradeNo = "", PaymentAmt = requestInfo.PaymentAmt.ToString(), Result = ((int)ResultStatus.Error).ToString(), }; ViewBag.ReturnUrl = resultInterface.GetReturnUrl(requestInfo, notifyInfo); } catch (BizException bex) { requestInfo.ResultDesc = bex.Message; requestInfo.ExecuteResult = (int)ResultStatus.Error; PayRequestDAL.Update(requestInfo); ViewBag.ErrorMsg = bex.Message; return(View()); } catch (Exception ex) { requestInfo.ExecuteResult = (int)ResultStatus.Error; requestInfo.ResultDesc = ex.ToString(); PayRequestDAL.Update(requestInfo); ViewBag.ErrorMsg = "系统执行时发生异常:" + ex.Message; return(View()); } #endregion //支付请求执行成功 onlinePay.ExecuteSuccess(requestInfo); } catch (Exception ex) { ViewBag.ErrorMsg = "系统执行时发生异常:" + ex.Message; string log = string.Format(@"WAP支付JSAPI发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:requestSysNo={3} code={4}", Environment.NewLine, ex.Message, ex.StackTrace, requestSysNo, code); LogWriter.WriteLog(log, AppConfig.Global.SwiftPassWeChatPayLogFolder, ExceptionHelper.ExceptionLevel.Exception); } return(View()); }