Exemplo n.º 1
0
        private static async Task <HttpResponseMessage> BuildHttpResponseMessage(HttpReqModel reqModel)
        {
            var handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };
            var reqUrl = reqModel.Url;

            using (var httpClient = new HttpClient(handler)) {
                var reqParams = new List <string>();
                if (reqModel.ReqData != null)
                {
                    foreach (var prop in reqModel.ReqData.GetType().GetProperties())
                    {
                        var propValue = Convert.ToString(prop.GetValue(reqModel.ReqData));
                        if (!String.IsNullOrEmpty(propValue))
                        {
                            reqParams.Add($"{prop.Name}={propValue}");
                        }
                    }
                }
                if (reqParams.Count > 0)
                {
                    reqUrl += ("?" + String.Join("&", reqParams));
                }
                HttpResponseMessage rsp = null;
                foreach (var pair in (reqModel.Headers ?? new Dictionary <string, string>()))
                {
                    httpClient.DefaultRequestHeaders.Add(pair.Key, pair.Value);
                }
                if (String.Equals(reqModel.Method, "post", StringComparison.OrdinalIgnoreCase))
                {
                    HttpContent postContent = null;
                    if (reqModel.ReqBody.GetType() == typeof(Dictionary <string, string>))
                    {
                        //form-url-encoded
                        postContent = new FormUrlEncodedContent(reqModel.ReqBody as Dictionary <string, string>);
                    }
                    else
                    {
                        postContent = new StringContent(JsonConvert.SerializeObject(reqModel.ReqBody ?? new { }), Encoding.UTF8, "application/json");
                    }
                    rsp = await httpClient.PostAsync(reqUrl, postContent);
                }
                else if (String.Equals(reqModel.Method, "get", StringComparison.OrdinalIgnoreCase))
                {
                    rsp = await httpClient.GetAsync(reqUrl);
                }
                else
                {
                    throw new BizException($"请求地址{reqModel.Url}参数有误,只支持Post 和 Get");
                }
                if (rsp.StatusCode != HttpStatusCode.OK)
                {
                    throw new BizException("模拟请求失败", $"请求失败,参数{JsonConvert.SerializeObject(reqModel)},返回{rsp.StatusCode},Body为{await rsp.Content.ReadAsStringAsync()}");
                }
                return(rsp);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 快捷支付签约
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>

        public async Task <ExpressContractedClientRsp> ExpressContractAsync(TypeVItem req)
        {
            req.NotNull("请求参数");
            debugInfo.Info($"快捷签约:{req.ToJsonString()}");
            req.Card.NotNull("卡信息");
            req.Trade.NotNull("交易信息");
            var card = req.Card;

            card.Bank.NotNull("持卡人支付卡号发卡行");
            card.Type.EnumValueValid("持卡人支付卡号卡类型");
            card.No.NotNull("持卡人支付卡号");
            card.Name.NotNull("持卡人姓名");
            card.IdType.NotNull("持卡人证件类型");
            card.IdNo.NotNull("持卡人证件号");
            card.Phone.NotNull("持卡人手机号");

            var trade = req.Trade;

            trade.Type.ShouldBe("V", "交易类型");
            trade.Id.NotNull("交易号");
            trade.Id.NotTooLong(30, "交易号");
            trade.Amount.DecimalValid("交易金额");
            trade.Currency.ShouldBe("CNY", "交易币种");

            var xmlData      = JdHelper.GetTypeVXml(req);
            var desEncrypted = JdHelper.DesEncrypt(xmlData, jdConfig.DesKey);
            var sign         = JdHelper.Md5(jdConfig.Version, jdConfig.Merchant, jdConfig.Terminal, desEncrypted,
                                            jdConfig.Md5Key);
            var xml = JdHelper.CreateChinaBank(jdConfig.Version, jdConfig.Merchant, jdConfig.Terminal, desEncrypted,
                                               sign);
            var base64EncodedStr = JdHelper.Base64Encode(xml);
            var reqModel         = new HttpReqModel()
            {
                Method  = "post",
                ReqBody = new Dictionary <string, string>()
                {
                    { "req", base64EncodedStr },
                    { "charset", "utf-8" }
                },
                Url = jdConfig.FastPayUrl
            };
            var rspStr = await HttpHelper.GetResultAsync <string>(reqModel);

            var content = rspStr.Substring(rspStr.IndexOf('=') + 1);

            debugInfo.Info($"快捷签约返回原文:{rspStr}");
            var rsp = await ParseExpresssContractAsync(content);

            debugInfo.Info($"快捷签约解析:{rsp.ToJsonString()}");
            return(rsp);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取返回的结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public static async Task <T> GetResultAsync <T>(HttpReqModel reqModel) where T : class
        {
            var rsp = await BuildHttpResponseMessage(reqModel);

            var rspString = await rsp.Content.ReadAsStringAsync();

            try {
                if (typeof(T) == typeof(string))
                {
                    return(rspString as T);
                }
                return(JsonConvert.DeserializeObject <T>(rspString));
            } catch (Exception ex) {
                throw new BizException("读取对象失败", $"序列化{typeof(T)}失败,返回结果为{rspString},异常:{ex.Message}");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 快捷支付
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task <FastPayRsp> FastPayAsync(TypeSItem req)
        {
            req.NotNull("请求参数");
            debugInfo.Info($"快捷支付:{req.ToJsonString()}");
            var trade = req.Trade;

            trade.NotNull("交易参数");
            trade.Notice = jdConfig.ExpressPayNotifyUrl;
            trade.Type.ShouldBe("S", "交易类型应为S");
            trade.Id.NotNull("交易号");
            trade.Amount.LongValid("金额");
            trade.Code.NotNull("交易验证码");
            trade.Currency.NotNull("交易币种");
            var card = req.Card;

            card.NotNull("卡片信息");
            card.Bank.NotNull("持卡人支付卡号发卡行");
            card.Type.ShouldBe("D", "持卡人卡类型");
            card.No.NotNull("持卡人支付卡号");
            card.Name.NotNull("持卡人姓名");
            card.IdType.NotNull("持卡人证件类型");
            card.IdNo.NotNull("持卡人证件号");
            card.Phone.NotNull("持卡人手机号");
            var typeSXml     = JdHelper.GetTypeSXml(req);
            var desEncrypted = JdHelper.DesEncrypt(typeSXml, jdConfig.DesKey);
            var sign         = JdHelper.Md5(jdConfig.Version, jdConfig.Merchant, jdConfig.Terminal, desEncrypted,
                                            jdConfig.Md5Key);
            var chinabank = JdHelper.CreateChinaBank(jdConfig.Version, jdConfig.Merchant, jdConfig.Terminal,
                                                     desEncrypted, sign);
            var base64Encoded = JdHelper.Base64Encode(chinabank);
            var reqModel      = new HttpReqModel()
            {
                Method  = "post",
                ReqBody = new Dictionary <string, string>()
                {
                    { "req", base64Encoded },
                    { "charset", "utf-8" }
                },
                Url = jdConfig.FastPayUrl
            };
            var rspStr = await HttpHelper.GetResultAsync <string>(reqModel);

            var content = rspStr.Substring(rspStr.IndexOf('=') + 1);
            var rsp     = await ParseFastPayAsync(content);

            return(rsp);
        }
Exemplo n.º 5
0
        public static async Task <T> GetWithdrawRspAsync <T>(string url, JdWithdrawRequest req) where T : class
        {
            url.NotNull("请求地址");
            var reqBody = new Dictionary <string, string>();

            reqBody.Add("encrypt_type", req.EncryptType);
            reqBody.Add("customer_no", req.CustomerNo);
            reqBody.Add("sign_data", req.SignData);
            reqBody.Add("encrypt_data", req.EncryptData);
            reqBody.Add("sign_type", req.SignTye);
            var httpReqModel = new HttpReqModel()
            {
                Url     = url,
                Method  = "post",
                ReqBody = reqBody
            };

            return(await HttpHelper.GetResultAsync <T>(httpReqModel));
        }
Exemplo n.º 6
0
 public static async Task SendRequstAsync(HttpReqModel reqModel)
 {
     await BuildHttpResponseMessage(reqModel);
 }