Beispiel #1
0
        /// <summary>
        /// 发送接口请求
        /// </summary>
        /// <param name="req"></param>
        /// <param name="funcFormat"></param>
        /// <returns></returns>
        public static async Task <TResp> SendAsync <TResp>(this WechatBaseReq <TResp> req,
                                                           Func <WechatPayConfig, HttpResponseMessage, Task <TResp> > funcFormat)
            where TResp : WechatBaseResp, new()
        {
            if (funcFormat == null)
            {
                throw new ArgumentNullException(nameof(funcFormat), "接口响应格式化方法不能为空!");
            }

            PrepareBody(req);

            if (req.http_method != HttpMethod.Get)
            {
                var bodyRes = await GetPostReqBody(req);

                if (!bodyRes.IsSuccess())
                {
                    return(bodyRes.ToResp <TResp>());
                }

                req.custom_body = bodyRes.body;
            }

            var client = WechatPayHelper.HttpClientProvider?.Invoke();
            var resp   = await(client == null ? ((OssHttpRequest)req).SendAsync() : client.SendAsync(req));

            return(await funcFormat(req.pay_config, resp));
        }
Beispiel #2
0
        internal static async Task <SendBodyResp> GetPostReqBody(WechatBaseReq req)
        {
            var paraDics = req.ParaDics;

            if (paraDics == null)
            {
                throw new ArgumentException("未能获取到当前请求需要的必要参数");
            }

            var encryptParas = req.EncryptParaDics;

            if (encryptParas != null && encryptParas.Count > 0)
            {
                var encryptBodyRes = await GetReqContent_JsonEncryptSegment(req.pay_config, encryptParas);

                if (!encryptBodyRes.IsSuccess())
                {
                    return(encryptBodyRes.ToResp <SendBodyResp>());
                }

                foreach (var keyValuePair in encryptBodyRes.body)
                {
                    paraDics[keyValuePair.Key] = keyValuePair.Value;
                }
            }

            var reqBody = JsonSerializer.Serialize(paraDics, _jsonOption);

            return(new SendBodyResp()
            {
                body = reqBody
            });
        }
Beispiel #3
0
        /// <inheritdoc />
        private static void PrepareBody(WechatBaseReq req)
        {
            if (req.pay_config == null)
            {
                req.SetContextConfig(WechatPayHelper.pay_config);
            }

            if (req.pay_config == null)
            {
                throw new NotImplementedException("未发现商户支付配置信息!");
            }

            req.address_url = string.Concat(WechatPayHelper.api_domain, req.GetApiPath()); //GetApiPath();
            req.InternalPrepareBodyPara();
        }
Beispiel #4
0
 /// <summary>
 /// 发送接口请求
 /// </summary>
 /// <typeparam name="TResp"></typeparam>
 /// <param name="req"></param>
 /// <returns></returns>
 public static Task <TResp> SendAsync <TResp>(this WechatBaseReq <TResp> req)
     where TResp : WechatBaseResp, new()
 {
     return(SendAsync(req, (config, resp) => JsonFormat <TResp>(config, resp, true)));
 }