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); } }
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); } }