예제 #1
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            try
            {
                // 字典排序
                var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
                {
                    { mch_id, Options.MchId },
                    { nonce_str, Guid.NewGuid().ToString("N") },
                    { notify_url, Options.NotifyUrl }
                };

                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, Options.AppId);
                }

                sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));
                var content = HttpClientEx.BuildContent(sortedTxtParams);

                var body = await Client.DoPostAsync(request.GetRequestUrl(), content);

                var parser = new WeChatPayXmlParser <T>();
                var rsp    = parser.Parse(body);
                CheckResponseSign(rsp);
                return(rsp);
            }
            catch (Exception ex)
            {
                logger?.LogError($"{DateTime.Now} ExecuteAsync(1)", ex.Message);
                return(null);
            }
        }
예제 #2
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { APPID, Options.AppId },
                { MCH_ID, Options.MchId },
                { NONCE_STR, Guid.NewGuid().ToString("N") }
            };

            sortedTxtParams.Add(SIGN, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));

            var rspContent = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            if (string.IsNullOrEmpty(rspContent))
            {
                throw new Exception("rspContent is Empty!");
            }

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(rspContent);

            CheckResponseSign(rsp);
            return(rsp);
        }
예제 #3
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { APPID, Options.AppId },
                { MCH_ID, Options.MchId },
                { NONCE_STR, Guid.NewGuid().ToString("N") }
            };

            sortedTxtParams.Add(SIGN, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));
            var content = HttpClientEx.BuildContent(sortedTxtParams);

            Logger.LogInformation(0, "Request Content:{content}", content);

            var rspContent = await Client.DoPostAsync(request.GetRequestUrl(), content);

            Logger.LogInformation(1, "Response Content:{content}", rspContent);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(rspContent);

            CheckResponseSign(rsp);
            return(rsp);
        }
예제 #4
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, Options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, Options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));
            var content = HttpClientEx.BuildContent(sortedTxtParams);

            Logger?.LogTrace(0, "Request:{content}", content);

            var body = await Client.DoPostAsync(request.GetRequestUrl(), content);

            Logger?.LogTrace(1, "Response:{body}", body);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(body);

            CheckResponseSign(rsp);
            return(rsp);
        }
예제 #5
0
        public static async Task <string> PostAsync <T>(this HttpClient client, IWeChatPayRequest <T> request, IDictionary <string, string> textParams) where T : WeChatPayResponse
        {
            var url     = request.GetRequestUrl();
            var content = WeChatPayUtility.BuildContent(textParams);

            using (var reqContent = new StringContent(content, Encoding.UTF8, "application/xml"))
                using (var resp = await client.PostAsync(url, reqContent))
                    using (var respContent = resp.Content)
                    {
                        return(await respContent.ReadAsStringAsync());
                    }
        }
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request, string optionsName) where T : WeChatPayResponse
        {
            var signType = true; // ture:MD5,false:HMAC-SHA256
            var options  = _optionsSnapshotAccessor.Get(optionsName);

            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (request is WeChatPayDepositMicroPayRequest || request is WeChatPayDepositOrderQueryRequest || request is WeChatPayDepositRefundQueryRequest)
            {
                sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256);
                signType = false; // HMAC-SHA256
            }

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));
            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            _logger.Log(options.LogLevel, "Request:{content}", content);

            using (var client = _clientFactory.CreateClient())
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                _logger.Log(options.LogLevel, "Response:{body}", body);

                var parser   = new WeChatPayXmlParser <T>();
                var response = parser.Parse(body);

                if (request.IsCheckResponseSign())
                {
                    CheckResponseSign(response, options, signType);
                }

                return(response);
            }
        }
예제 #7
0
        public Task <T> PageExecuteAsync <T>(IWeChatPayRequest <T> request, WeChatPayOptions options) where T : WeChatPayResponse
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(options.AppId))
            {
                throw new ArgumentNullException(nameof(options.AppId));
            }

            if (string.IsNullOrEmpty(options.MchId))
            {
                throw new ArgumentNullException(nameof(options.MchId));
            }

            if (string.IsNullOrEmpty(options.Key))
            {
                throw new ArgumentNullException(nameof(options.Key));
            }

            var signType        = request.GetSignType();
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters());

            request.PrimaryHandler(options, signType, sortedTxtParams);

            var url = request.GetRequestUrl();

            if (url.Contains("?"))
            {
                url += "&" + WeChatPayUtility.BuildQuery(sortedTxtParams);
            }
            else
            {
                url += "?" + WeChatPayUtility.BuildQuery(sortedTxtParams);
            }

            var rsp = Activator.CreateInstance <T>();

            rsp.ResponseBody = url;
            return(Task.FromResult(rsp));
        }
예제 #8
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { APPID, Options.AppId },
                { MCH_ID, Options.MchId },
                { NONCE_STR, Guid.NewGuid().ToString("N") }
            };

            sortedTxtParams.Add(SIGN, Md5.GetMD5WithKey(sortedTxtParams, Options.Key));

            var body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(body);

            CheckResponseSign(rsp);
            return(rsp);
        }
예제 #9
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request, WeChatPayOptions options) where T : WeChatPayResponse
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(options.AppId))
            {
                throw new WeChatPayException($"options.{nameof(WeChatPayOptions.AppId)} is Empty!");
            }

            if (string.IsNullOrEmpty(options.MchId))
            {
                throw new WeChatPayException($"options.{nameof(WeChatPayOptions.MchId)} is Empty!");
            }

            if (string.IsNullOrEmpty(options.APIKey))
            {
                throw new WeChatPayException($"options.{nameof(WeChatPayOptions.APIKey)} is Empty!");
            }

            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters());

            request.PrimaryHandler(sortedTxtParams, options);

            var client = _httpClientFactory.CreateClient(Name);
            var body   = await client.PostAsync(request, sortedTxtParams);

            var parser   = new WeChatPayResponseXmlParser <T>();
            var response = parser.Parse(body);

            if (request.GetNeedCheckSign())
            {
                var signType = request.GetSignType();
                CheckResponseSign(response, options, signType);
            }

            return(response);
        }
예제 #10
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request, string optionsName) where T : WeChatPayResponse
        {
            var options = string.IsNullOrEmpty(optionsName) ? _optionsSnapshotAccessor.Value : _optionsSnapshotAccessor.Get(optionsName);

            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key));
            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            _logger.Log(options.LogLevel, "Request:{content}", content);

            using (var client = _clientFactory.CreateClient())
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                _logger.Log(options.LogLevel, "Response:{body}", body);

                var parser   = new WeChatPayXmlParser <T>();
                var response = parser.Parse(body);

                if (request.IsCheckResponseSign())
                {
                    CheckResponseSign(response, options);
                }

                return(response);
            }
        }
예제 #11
0
 public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
 {
     return(await ExecuteAsync(request, null));
 }