/// <summary> /// 校验支付结果请求 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult CheckRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult(); var paramsData = new ParameterData(); paramsData.FromUrl(data); var paramsDict = paramsData.GetValues(); //判断是否有带返回参数 if (paramsDict.Count == 0) { result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc = "支付结果无通知参数"; resultInfo.ExecuteResult = (int)ResultStatus.Failure; PayResultDAL.Update(resultInfo); return(result); } //校验请求报文 resultInfo.OrderId = paramsData.GetValue("out_trade_no"); var aliNotify = new AlipayNotify(); bool verifyResult = aliNotify.Verify(paramsDict, paramsData.GetValue("notify_id"), paramsData.GetValue("sign")); if (!verifyResult) { result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc = "verify failed"; resultInfo.ExecuteResult = (int)ResultStatus.Failure; PayResultDAL.Update(resultInfo); return(result); } result.Status = ResultStatus.Success; return(result); }
/// <summary> /// 更新支付结果记录并关联支付请求记录 /// </summary> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public virtual PayRequestInfo RelateRequestInfo(PayResultInfo resultInfo) { //判断是否存在有效的支付结果记录,如果存在,则当前支付结果记录不执行 bool exist = PayResultDAL.ExistValidPayResult(resultInfo.OrderId, resultInfo.TradeNo, (AppEnum.PayType)resultInfo.PayType); if (exist) { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "已存在有效的支付结果记录"; } PayRequestInfo requestInfo = null; if (resultInfo.ExecuteResult == (int)ResultStatus.Success) { requestInfo = PayRequestDAL.GetValidPayRequest(resultInfo.OrderId, (AppEnum.PayType)resultInfo.PayType); if (requestInfo != null && requestInfo.SysNo > 0) { resultInfo.RequestSysNo = requestInfo.SysNo; } } //更新支付结果记录信息 PayResultDAL.Update(resultInfo); return(requestInfo); }
/// <summary> /// 解析支付结果 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult <PayNotifyInfo> ResolveRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult <PayNotifyInfo>(); try { var paramsData = new ParameterData(); paramsData.FromUrl(data); resultInfo.OrderId = paramsData.GetValue("out_trade_no"); resultInfo.TradeNo = paramsData.GetValue("trade_no"); resultInfo.PaymentAmt = Convert.ToDecimal(paramsData.GetValue("total_fee")); string tradeStatus = paramsData.GetValue("trade_status"); var tradeResult = (tradeStatus == "TRADE_FINISHED" || tradeStatus == "TRADE_SUCCESS") ? ResultStatus.Success : ResultStatus.Failure; resultInfo.ResultDesc = tradeStatus; resultInfo.ExecuteResult = (int)tradeResult; result.Data = this.GetPayNotifyInfo(resultInfo); result.Status = ResultStatus.Success; } catch { result.Status = ResultStatus.Error; resultInfo.ResultDesc = result.Message = "解析支付结果参数失败"; resultInfo.ExecuteResult = (int)ResultStatus.Error; PayResultDAL.Update(resultInfo); } return(result); }
/// <summary> /// 解析支付结果 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult <PayNotifyInfo> ResolveRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult <PayNotifyInfo>(); try { var notifyData = new ParameterData(); notifyData.FromXml(data); resultInfo.OrderId = notifyData.GetValue("out_trade_no"); resultInfo.TradeNo = notifyData.GetValue("transaction_id"); resultInfo.PaymentAmt = Convert.ToDecimal(notifyData.GetValue("total_fee")) / 100;//微信支付金额的单位为“分”,所以要除以100 resultInfo.ExtTradeNo = notifyData.GetValue("out_transaction_id"); string tradeStatus = notifyData.GetValue("pay_result"); var tradeResult = ResultStatus.Success;//由于在校验支付结果请求时已经验证了是否支付成功,所以此处肯定是支付成功的 resultInfo.ResultDesc = tradeStatus; resultInfo.ExecuteResult = (int)tradeResult; result.Data = this.GetPayNotifyInfo(resultInfo); result.Status = ResultStatus.Success; } catch { resultInfo.ResultDesc = "解析支付结果参数失败"; resultInfo.ExecuteResult = (int)ResultStatus.Error; PayResultDAL.Update(resultInfo); result.Message = resultInfo.ResultDesc; result.Status = ResultStatus.Error; } return(result); }
/// <summary> /// 校验支付结果请求 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult CheckRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult(); var notifyData = new AllinpayData(AllinpayDataType.PayResult); notifyData.FromUrl(data); //校验参数个数 if (notifyData.GetValues().Count == 0) { result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc = "支付结果无通知参数"; resultInfo.ExecuteResult = (int)ResultStatus.Failure; PayResultDAL.Update(resultInfo); return(result); } //校验参数合法性 resultInfo.OrderId = notifyData.GetValue("orderNo"); bool verifyResult = AllinpayCore.VerifyResultSign(notifyData); if (!verifyResult) { result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc = "verify failed"; resultInfo.ExecuteResult = (int)ResultStatus.Failure; PayResultDAL.Update(resultInfo); return(result); } result.Status = ResultStatus.Success; return(result); }
/// <summary> /// 校验支付结果请求 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult CheckRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult(); var errorData = new WxPayData(); try { //校验请求报文 var notifyData = new ParameterData(); notifyData.FromXml(data); //检查支付结果中transaction_id是否存在 resultInfo.OrderId = notifyData.GetValue("out_trade_no") as string; resultInfo.TradeNo = notifyData.GetValue("transaction_id") as string; if (string.IsNullOrEmpty(resultInfo.TradeNo)) { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "支付结果中微信支付单号不存在"; PayResultDAL.Update(resultInfo); errorData.SetValue("return_code", "FAIL"); errorData.SetValue("return_msg", resultInfo.ResultDesc); result.Message = errorData.ToXml(); result.Status = ResultStatus.Failure; return(result); } //查询支付单,判断支付单真实性 var req = new WxPayData(); req.SetValue("transaction_id", resultInfo.TradeNo); var queryData = WxPayApi.OrderQuery(req); if (queryData.GetValue("return_code").ToString() != "SUCCESS" || queryData.GetValue("result_code").ToString() != "SUCCESS") { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "微信支付单查询失败"; PayResultDAL.Update(resultInfo); errorData.SetValue("return_code", "FAIL"); errorData.SetValue("return_msg", resultInfo.ResultDesc); result.Message = errorData.ToXml(); result.Status = ResultStatus.Failure; return(result); } } catch (WxPayException wex) { resultInfo.ExecuteResult = (int)ResultStatus.Error; resultInfo.ResultDesc = wex.Message; PayResultDAL.Update(resultInfo); errorData.SetValue("return_code", "FAIL"); errorData.SetValue("return_msg", resultInfo.ResultDesc); result.Message = errorData.ToXml(); result.Status = ResultStatus.Error; return(result); } result.Status = ResultStatus.Success; return(result); }
/// <summary> /// 执行条码支付 /// </summary> /// <param name="orderInfo">支付订单</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult <PayNotifyInfo> ExecuteBarcodePay(PayOrderInfo orderInfo, PayResultInfo resultInfo) { var result = new ExecuteResult <PayNotifyInfo>(); try { int paymentAmt = (int)(Convert.ToDecimal(orderInfo.PaymentAmt) * 100);//微信支付金额的单位为“分”,所以要乘以100 WxPayData runData = MicroPay.Run(orderInfo.OrderId, paymentAmt, orderInfo.Barcode); if (runData.GetValue("return_code").ToString() != "SUCCESS") { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "微信条码支付请求失败:" + runData.GetValue("return_msg").ToString(); PayResultDAL.Update(resultInfo); result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc; return(result); } else if (runData.GetValue("result_code").ToString() != "SUCCESS") { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "微信条码支付失败:" + runData.GetValue("err_code_des").ToString(); PayResultDAL.Update(resultInfo); result.Status = ResultStatus.Failure; result.Message = resultInfo.ResultDesc; return(result); } //支付成功,更新支付结果记录 resultInfo.OrderId = orderInfo.OrderId; resultInfo.TradeNo = runData.GetValue("transaction_id").ToString(); resultInfo.PaymentAmt = Convert.ToDecimal(runData.GetValue("total_fee")) / 100; //微信支付金额的单位为“分”,所以要除以100 resultInfo.ExecuteResult = (int)ResultStatus.Success; result.Data = this.GetPayNotifyInfo(resultInfo); result.Status = ResultStatus.Success; } catch (WxPayException wex) { resultInfo.ExecuteResult = (int)ResultStatus.Error; resultInfo.ResultDesc = wex.Message; PayResultDAL.Update(resultInfo); result.Status = ResultStatus.Error; result.Message = resultInfo.ResultDesc; } catch (Exception ex) { resultInfo.ExecuteResult = (int)ResultStatus.Error; resultInfo.ResultDesc = ex.ToString(); PayResultDAL.Update(resultInfo); result.Status = ResultStatus.Error; result.Message = ex.Message; } return(result); }
/// <summary> /// 支付结果列表 /// </summary> /// <param name="pagger"></param> /// <param name="type"></param> /// <returns></returns> public ActionResult Index(Pagger pagger, int type = 0) { var model = new SearchResult(); var list = PayResultDAL.GetPayResultList(pagger, type); model.PaggerData = pagger; model.ResultData = list; ViewBag.SearchType = type; return(View(model)); }
public JsonResult Notify(int resultSysNo) { try { var resultInfo = PayResultDAL.GetPayResult(resultSysNo); if (resultInfo == null || resultInfo.SysNo <= 0) { return(Json(new { status = 0, msg = "支付结果记录不存在" })); } if (resultInfo.ExecuteResult != (int)ResultStatus.Success) { 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.NotifyUrl.IsUrl()) { return(Json(new { status = 0, msg = "支付请求记录的通知地址无效" })); } var resultInterface = Builder.BuildAlipayResult(); var notifyResult = resultInterface.NotifyBack(resultInfo, requestInfo); if (notifyResult.Status == ResultStatus.Success) { return(Json(new { status = 1, msg = "通知成功" })); } //如果已经通知多次,则将通知状态改成已作废 int notifyCount = PayResultDAL.GetNotifyBackCount(resultInfo.SysNo); if (notifyCount >= 5 && resultInfo.NotifyStatus != (int)AppEnum.NotifyStatus.Canceled) { resultInfo.NotifyStatus = (int)AppEnum.NotifyStatus.Canceled; PayResultDAL.Update(resultInfo); } return(Json(new { status = 0, msg = "通知失败,原因:" + notifyResult.Message })); } 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 })); } }
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="data">业务数据报文</param> /// <param name="payType">支付方式</param> /// <returns></returns> public virtual PayResultInfo SaveRequest(string data, AppEnum.PayType payType) { var resultInfo = new PayResultInfo() { PayType = (int)payType, RequestData = data, ExecuteResult = (int)ResultStatus.None, NotifyStatus = (int)AppEnum.NotifyStatus.Original, CreateTime = DateTime.Now, }; resultInfo.SysNo = PayResultDAL.Insert(resultInfo); return(resultInfo); }
/// <summary> /// 保存支付结果异步回执记录 /// </summary> /// <param name="data"></param> /// <param name="payType"></param> /// <returns></returns> public static PayResultInfo SavePayResult(string data, AppEnum.PayType payType) { var payResult = new PayResultInfo() { PayType = (int)payType, RequestData = data, ExecuteResult = (int)ResultStatus.None, NotifyStatus = (int)AppEnum.GlobalStatus.Invalid, CreateTime = DateTime.Now, }; payResult.SysNo = PayResultDAL.Insert(payResult); return(payResult); }
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 })); } }
public ActionResult AllinpayRefund(string appId, string appSecret, string orderId) { if (appSecret != AppCache.GetAppSecret(appId)) { return(Json(new { msg = "非法appSecret" })); } var resultInfo = PayResultDAL.GetValidPayResult(orderId, AppEnum.PayType.Allinpay); if (resultInfo == null || resultInfo.SysNo <= 0) { return(Json(new { msg = "订单无支付记录" })); } var requestInfo = PayRequestDAL.GetPayRequest(resultInfo.RequestSysNo); var orderTime = JsonHelper.Deserialize <PayOrderInfo>(requestInfo.RequestData).OrderTime; RefundOrderInfo info = new RefundOrderInfo() { OrderId = orderId, OrderTime = orderTime ?? DateTime.Now.ToString("yyyyMMddHHmmss"), TradeNo = resultInfo.TradeNo, PaymentAmt = resultInfo.PaymentAmt.ToString(), RefundOrderId = DateTime.Now.ToString("yyyyMMddHHmmssfff"), RefundAmt = resultInfo.PaymentAmt.ToString(), NotifyUrl = string.Format("http://{0}/Test/RefundNotify?type=0", AppConfig.Global.Domain), }; var setting = JsonHelper.GetDefaultSettings(); setting.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; string data = JsonHelper.Serialize(info, setting); string sign = SignManager.CreateSign(appId, data).Data; ViewBag.AppId = appId; ViewBag.Sign = sign; ViewBag.Data = data; ViewBag.Title = "通联支付退款(测试)"; ViewBag.Url = "/allinpay/syncrefund"; return(View("Pay")); }
/// <summary> /// 校验支付参数 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="payType">支付方式</param> /// <returns></returns> private ExecuteResult <PayOrderInfo> CheckParamaters(string data, AppEnum.PayType payType) { //校验结果 var result = new ExecuteResult <PayOrderInfo>(); //参数名称 string paramName = null; //金额匹配表达式(最多保留2位小数正实数) string amtReg = @"^\d+(\.[0-9]{1,2}0*)?$"; #region 校验支付报文结构 var info = JsonHelper.Deserialize <PayOrderInfo>(data); if (info == null) { result.Status = ResultStatus.Failure; result.Message = "参数data格式不正确"; return(result); } #endregion #region 校验参数类型和取值范围 #region 校验订单编号 paramName = "orderId"; if (string.IsNullOrWhiteSpace(info.OrderId)) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}不能为空", paramName); return(result); } #endregion #region 校验支付金额 paramName = "paymentAmt"; if (string.IsNullOrWhiteSpace(info.PaymentAmt)) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}不能为空", paramName); return(result); } decimal paymentAmt = 0; if (!Regex.IsMatch(info.PaymentAmt, amtReg) || !decimal.TryParse(info.PaymentAmt, out paymentAmt)) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}类型错误", paramName); return(result); } if (paymentAmt <= 0) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}必须大于0", paramName); return(result); } #endregion #region 校验支付条形码 paramName = "barcode"; if (string.IsNullOrWhiteSpace(info.Barcode)) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}不能为空", paramName); return(result); } #endregion #region 校验支付完成的通知地址 paramName = "notifyUrl"; if (string.IsNullOrWhiteSpace(info.NotifyUrl)) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}不能为空", paramName); return(result); } if (!info.NotifyUrl.IsUrl()) { result.Status = ResultStatus.Failure; result.Message = string.Format("参数{0}格式错误", paramName); return(result); } #endregion #endregion #region 校验是否已支付 if (PayResultDAL.ExistValidPayResult(info.OrderId, payType)) { result.Status = ResultStatus.Failure; result.Message = "该订单已成功支付,不能重复支付"; return(result); } #endregion result.Status = ResultStatus.Success; result.Data = info; return(result); }
/// <summary> /// 校验支付结果请求 /// </summary> /// <param name="data">业务数据报文</param> /// <param name="resultInfo">支付结果记录</param> /// <returns></returns> public override ExecuteResult CheckRequest(string data, PayResultInfo resultInfo) { var result = new ExecuteResult(); try { //校验请求报文 var notifyData = new ParameterData(); notifyData.FromXml(data); resultInfo.OrderId = notifyData.GetValue("out_trade_no"); if (notifyData.GetValue("status") != "0") //校验返回状态 { throw new BizException("返回状态错误代码:" + notifyData.GetValue("status") + ",错误信息:" + notifyData.GetValue("message")); } if (!SwiftPassCore.CheckSign(notifyData)) //校验数据签名 { throw new BizException("签名校验未通过"); } if (notifyData.GetValue("result_code") != "0") //校验业务结果 { throw new BizException("业务结果错误代码:" + notifyData.GetValue("err_code") + ",错误信息:" + notifyData.GetValue("err_msg")); } if (notifyData.GetValue("pay_result") != "0") //校验支付结果 { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "用户支付失败:" + notifyData.GetValue("pay_info"); PayResultDAL.Update(resultInfo); result.Message = resultInfo.ResultDesc; result.Status = ResultStatus.Failure; return(result); } //检查支付结果中transaction_id是否存在 resultInfo.TradeNo = notifyData.GetValue("transaction_id"); if (string.IsNullOrEmpty(resultInfo.TradeNo)) { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "支付结果中平台交易单号不存在"; PayResultDAL.Update(resultInfo); result.Message = resultInfo.ResultDesc; result.Status = ResultStatus.Failure; return(result); } //查询支付单,判断支付单真实性 var queryData = SwiftPassPayApi.OrderQuery(resultInfo.OrderId, resultInfo.TradeNo); if (queryData.GetValue("status") != "0" || //校验返回状态 queryData.GetValue("result_code") != "0" || //校验业务结果 !SwiftPassCore.CheckSign(queryData) || //校验数据签名 queryData.GetValue("trade_state") != "SUCCESS") //校验交易状态 { resultInfo.ExecuteResult = (int)ResultStatus.Failure; resultInfo.ResultDesc = "平台交易单号查询失败"; PayResultDAL.Update(resultInfo); result.Message = resultInfo.ResultDesc; result.Status = ResultStatus.Failure; return(result); } } catch (BizException wex) { resultInfo.ExecuteResult = (int)ResultStatus.Error; resultInfo.ResultDesc = wex.Message; PayResultDAL.Update(resultInfo); result.Message = resultInfo.ResultDesc; result.Status = ResultStatus.Error; return(result); } result.Status = ResultStatus.Success; return(result); }
/// <summary> /// 通知业务系统支付结果 /// </summary> /// <param name="resultInfo">支付结果记录</param> /// <param name="requestInfo">支付请求记录</param> /// <returns></returns> public virtual ExecuteResult NotifyBack(PayResultInfo resultInfo, PayRequestInfo requestInfo) { var result = new ExecuteResult() { Status = ResultStatus.Failure }; //支付结果记录对象无效,则不执行 if (resultInfo == null || resultInfo.SysNo <= 0) { result.Message = "支付结果记录对象无效"; return(result); } //支付请求记录对象无效,则不执行 if (requestInfo == null || requestInfo.SysNo <= 0) { result.Message = "支付请求记录对象无效"; return(result); } //支付结果记录与支付请求记录不对应,则不执行 if (requestInfo.SysNo != resultInfo.RequestSysNo) { result.Message = "支付结果记录与支付请求记录不对应"; return(result); } //支付结果记录未成功执行,或者已通知,则不执行 if (resultInfo.ExecuteResult != (int)ResultStatus.Success || resultInfo.NotifyStatus == (int)AppEnum.NotifyStatus.Finished) { result.Message = "支付结果记录未成功执行或已通知成功"; return(result); } //支付请求记录中不存在有效的通知地址,则不执行 if (!requestInfo.NotifyUrl.IsUrl()) { result.Message = "支付请求记录中不存在有效的通知地址"; return(result); } var notifyInfo = this.GetPayNotifyInfo(resultInfo); 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 param = string.Format("appId={0}&sign={1}&data={2}", requestInfo.AppId, sign, data); var response = HttpHelper.HttpRequest("POST", requestInfo.NotifyUrl, param, 10000); string status = JsonHelper.GetJsonNode(response, "status"); string msg = JsonHelper.GetJsonNode(response, "msg"); result.Message = msg; var backInfo = new NotifyBackInfo() { ResultSysNo = resultInfo.SysNo, Status = (int)AppEnum.GlobalStatus.Invalid, Msg = msg, CreateTime = DateTime.Now, ResponseData = response, }; if (status == "1") { backInfo.Status = (int)AppEnum.GlobalStatus.Valid; resultInfo.NotifyStatus = (int)AppEnum.NotifyStatus.Finished; PayResultDAL.Update(resultInfo); result.Status = ResultStatus.Success; } PayResultDAL.InsertNotifyBack(backInfo); return(result); }
/// <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); }