Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <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);
        }
Esempio n. 5
0
        /// <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);
        }
Esempio n. 6
0
        /// <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);
        }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
        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 }));
            }
        }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        /// <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);
        }
Esempio n. 11
0
        /// <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);
        }