Exemple #1
0
        public ActionResult Index(string orderId)
        {
            var           response    = new BaseResponse();
            BaseRequest   baseRequest = new BaseRequest();
            ServiceResult result      = new ServiceResult();
            var           bizCode     = string.Empty;
            string        errMsg      = "";
            PayOrder      order       = null;
            PayChannel    payChannel  = null;

            if (orderId.IsNullOrWhiteSpace())
            {
                return(Json(result.IsFailed("参数错误,orderid")));
            }
            //订单校验
            if (!OrderVerify(orderId, out order, out payChannel, out errMsg))
            {
                return(Json(result.IsFailed(errMsg)));
            }

            baseRequest.Order      = order;
            baseRequest.PayChannel = payChannel;
            bizCode = payChannel.ChannelCode;
            if (bizCode.IsNullOrWhiteSpace())
            {
                return(Json(result.IsFailed("无效交易类型,ChannelCode为空")));
            }

            if (!ProcessorUtil.BizCodeValid(bizCode))
            {
                return(Json(result.IsFailed("支付标识和DescriptionAttribute不一致")));
            }
            var processor = this.factory.Create(bizCode);

            //构建请求参数
            var payParams = processor.CreatePayRequest(baseRequest);

            //如果是浏览器表单提交
            if (processor.IsPostForm)
            {
                ViewData["url"]    = processor.PayUrl;
                ViewData["method"] = processor.RequestMethod;
                return(View(payParams));
            }

            //异步请求
            PayResponse payResponse = processor.Process(payParams);

            if (!payResponse.Success)
            {
                return(Json(result.IsFailed(payResponse.Message)));
            }
            switch (processor.PayModel)
            {
            case PayModelEnum.无:
                break;

            case PayModelEnum.URL跳转:
                return(Redirect(payResponse.Data));

            case PayModelEnum.扫码:
                break;

            case PayModelEnum.二维码生成:
                return(QRCode(payResponse.Data));

            case PayModelEnum.HTML输出:
                return(Content(payResponse.Data, "text/html"));

            default:
                break;
            }
            return(View());
        }
Exemple #2
0
        public BaseResponse Index([FromUri] RequestData model)
        {
            var watcher = new Stopwatch();

            watcher.Start();

            var          response   = new BaseResponse();
            BaseResponse exResponse = null;

            var         requestId             = string.Empty;
            var         requestDataJson       = string.Empty;
            var         userDataJson          = string.Empty;
            var         logMsg                = new ApiLogMessage();
            var         bizCode               = string.Empty;
            var         urlEncodedUserData    = string.Empty;
            var         urlEncodedRequestData = string.Empty;
            var         parmUserData          = string.Empty;
            var         parmRequestData       = string.Empty;
            BaseRequest baseRequest           = new BaseRequest();
            PayOrder    order      = null;
            PayChannel  payChannel = null;

            try
            {
                if (model.IsNull() || model.RequestId.IsNullOrWhiteSpace())
                {
                    return(BaseResponse.Create(ApiEnum.ResponseCode.处理失败, "无效请求", null, 0));
                }


                //验证参数
                var errMsg = "";
                if (!ModelVerify(model, out errMsg))
                {
                    response = BaseResponse.Create(ApiEnum.ResponseCode.参数不正确, errMsg, null, 0);
                    return(response);
                }

                //订单校验
                if (!OrderVerify(model, out order, out payChannel, out errMsg))
                {
                    response = BaseResponse.Create(ApiEnum.ResponseCode.处理失败, errMsg, null, 0);
                    return(response);
                }

                ////验证签名
                //if (!VerifySign(baseRequest, merchant))
                //{
                //    response = BaseResponse.Create(ApiEnum.ResponseCode.无效调用凭证, "签名不正确", null, 0);
                //    return response;
                //}

                baseRequest.Order      = order;
                baseRequest.PayChannel = payChannel;
                bizCode = payChannel.ChannelCode;
                if (bizCode.IsNullOrWhiteSpace())
                {
                    return(BaseResponse.Create(ApiEnum.ResponseCode.无效交易类型, "无效交易类型,ChannelCode为空", null, 0));
                }

                if (!ProcessorUtil.BizCodeValid(bizCode))
                {
                    return(BaseResponse.Create(ApiEnum.ResponseCode.无效交易类型, "支付标识和DescriptionAttribute不一致", null, 0));
                }
                var processor = this.factory.Create(bizCode);
                response = processor.Process(baseRequest);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                response       = BaseResponse.Create(ApiEnum.ResponseCode.系统内部错误, "不好意思,程序开小差,正在重启" + ex.ToString(), 0);
                exResponse     = BaseResponse.Create(ApiEnum.ResponseCode.系统内部错误, ex.ToString(), 0);
                logMsg.IsError = true;
            }
            finally
            {
                //WriteRequestInfo(userData, requestData, requestId, bizCode);

                watcher.Stop();
                var duration = watcher.Elapsed.TotalMilliseconds;

                var logStr = string.Empty;
                logStr += string.Format("【请求报文】RequestId:{0}", requestId) + Environment.NewLine;
                logStr += string.Format("UserData:{0}", urlEncodedUserData) + Environment.NewLine;
                logStr += string.Format("RequestData:{0}", urlEncodedRequestData) + Environment.NewLine;
                logStr += string.Format("【响应报文】{0}", response.ToJson());
                logStr += string.Format("【耗时】{0}毫秒", duration);
                log.Info(logStr.ToString());


                logMsg.UserDataStr    = urlEncodedUserData;
                logMsg.RequestDataStr = urlEncodedRequestData;
                logMsg.RequestId      = requestId;
                logMsg.LogTime        = DateTime.Now;


                logMsg.RequestJson = requestDataJson;
                logMsg.Response    = exResponse.IsNull() ? response.ToJson() : exResponse.ToJson();
                logMsg.Duration    = duration;

                if (AppConfig.LogType == LogType.MQ)
                {
                    try
                    {
                        this.bus.Publish(logMsg);
                    }
                    catch (Exception ex)
                    {
                        log.Error("写入MQ失败,RequestId:{0}\r\n{1}".Fmt("", ex.ToString()));
                    }
                }
            }

            return(response);
        }