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);
            }
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
        }
        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);
            }
        }
Beispiel #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));
        }
Beispiel #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);
        }
Beispiel #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 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 client = _httpClientFactory.CreateClient(nameof(WeChatPayClient));
            var body   = await client.PostAsync(request.GetRequestUrl(), sortedTxtParams);

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

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

            return(response);
        }
Beispiel #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);
            }
        }