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); } }
public Task <WeChatPayDictionary> ExecuteAsync(IWeChatPayCallRequest request, string optionsName) { var options = _optionsSnapshotAccessor.Get(optionsName); var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()); if (request is WeChatPayAppCallPaymentRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } if (string.IsNullOrEmpty(sortedTxtParams.GetValue(partnerid))) { sortedTxtParams.Add(partnerid, options.MchId); } sortedTxtParams.Add(noncestr, Guid.NewGuid().ToString("N")); sortedTxtParams.Add(timestamp, WeChatPayUtility.GetTimeStamp()); sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key)); } else if (request is WeChatPayLiteAppCallPaymentRequest || request is WeChatPayH5CallPaymentRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appId))) { sortedTxtParams.Add(appId, options.AppId); } sortedTxtParams.Add(timeStamp, WeChatPayUtility.GetTimeStamp()); sortedTxtParams.Add(nonceStr, Guid.NewGuid().ToString("N")); sortedTxtParams.Add(signType, SIGN_TYPE_MD5); sortedTxtParams.Add(paySign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key)); } return(Task.FromResult(sortedTxtParams)); }
public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request, string optionsName, string certificateName) where T : WeChatPayResponse { var signType = true; // ture:MD5,false:HMAC-SHA256 var options = _optionsSnapshotAccessor.Get(optionsName); var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()) { { nonce_str, Guid.NewGuid().ToString("N") } }; if (request is WeChatPayTransfersRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(mch_appid))) { sortedTxtParams.Add(mch_appid, options.AppId); } sortedTxtParams.Add(mchid, options.MchId); } else if (request is WeChatPayGetPublicKeyRequest) { sortedTxtParams.Add(mch_id, options.MchId); sortedTxtParams.Add(sign_type, SIGN_TYPE_MD5); } else if (request is WeChatPayPayBankRequest) { if (options.PublicKey == null) { throw new WeChatPayException("WeChatPayPayBankRequest: PublicKey is null!"); } var no = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_bank_no), options.PublicKey); sortedTxtParams.SetValue(enc_bank_no, no); var name = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_true_name), options.PublicKey); sortedTxtParams.SetValue(enc_true_name, name); sortedTxtParams.Add(mch_id, options.MchId); } else if (request is WeChatPayQueryBankRequest) { sortedTxtParams.Add(mch_id, options.MchId); } else if (request is WeChatPayGetTransferInfoRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); } else if (request is WeChatPayDownloadFundFlowRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256); signType = false; // HMAC-SHA256 } else if (request is WeChatPayRefundRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); } else if (request is WeChatPaySendRedPackRequest || request is WeChatPaySendGroupRedPackRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid))) { sortedTxtParams.Add(wxappid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); } else if (request is WeChatPaySendWorkWxRedPackRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid))) { sortedTxtParams.Add(wxappid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); var sign_list = new List <string> { "act_name", "mch_billno", "mch_id", "nonce_str", "re_openid", "total_amount", "wxappid", }; sortedTxtParams.Add(workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.Secret, sign_list)); } else if (request is WeChatPayPayWwSpTrans2PockeRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); var sign_list = new List <string> { "amount", "appid", "desc", "mch_id", "nonce_str", "openid", "partner_trade_no", "ww_msg_type", }; sortedTxtParams.Add(workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.Secret, sign_list)); } else if (request is WeChatPayDepositReverseRequest || request is WeChatPayDepositConsumeRequest || request is WeChatPayDepositRefundRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256); signType = false; // HMAC-SHA256 } else // 其他接口 { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, options.AppId); } sortedTxtParams.Add(mch_id, options.MchId); } 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(certificateName)) { 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>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse { var useMD5 = true; var excludeSignType = true; if (CertificateClient == null) { throw new ArgumentNullException(nameof(Options.Certificate)); } // 字典排序 var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()); if (request is WeChatPayTransfersRequest) { sortedTxtParams.Add(MCH_APPID, Options.AppId); sortedTxtParams.Add(MCHID, Options.MchId); } else if (request is WeChatPayGetPublicKeyRequest) { sortedTxtParams.Add(MCH_ID, Options.MchId); sortedTxtParams.Add(SIGN_TYPE, "MD5"); excludeSignType = false; } else if (request is WeChatPayPayBankRequest) { if (RsaPublicParameters == null) { throw new ArgumentNullException(nameof(Options.RsaPublicKey)); } var no = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_BANK_NO), RsaPublicParameters); sortedTxtParams.SetValue(ENC_BANK_NO, no); var name = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_TRUE_NAME), RsaPublicParameters); sortedTxtParams.SetValue(ENC_TRUE_NAME, name); sortedTxtParams.Add(MCH_ID, Options.MchId); sortedTxtParams.Add(SIGN_TYPE, "MD5"); } else if (request is WeChatPayQueryBankRequest) { sortedTxtParams.Add(MCH_ID, Options.MchId); sortedTxtParams.Add(SIGN_TYPE, "MD5"); } else if (request is WeChatPayGetTransferInfoRequest) { sortedTxtParams.Add(APPID, Options.AppId); sortedTxtParams.Add(MCH_ID, Options.MchId); sortedTxtParams.Add(SIGN_TYPE, "MD5"); } else if (request is WeChatPayDownloadFundFlowRequest) { sortedTxtParams.Add(APPID, Options.AppId); sortedTxtParams.Add(MCH_ID, Options.MchId); sortedTxtParams.Add(SIGN_TYPE, "HMAC-SHA256"); useMD5 = false; excludeSignType = false; } else if (request is WeChatPayRefundRequest) { sortedTxtParams.Add(APPID, Options.AppId); sortedTxtParams.Add(MCH_ID, Options.MchId); } else // 其他接口 { sortedTxtParams.Add(APPID, Options.AppId); sortedTxtParams.Add(MCH_ID, Options.MchId); } sortedTxtParams.Add(NONCE_STR, Guid.NewGuid().ToString("N")); sortedTxtParams.Add(SIGN, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key, useMD5, excludeSignType)); var content = HttpClientEx.BuildContent(sortedTxtParams); Logger.LogInformation(0, "Request Content:{content}", content); var rspContent = await CertificateClient.DoPostAsync(request.GetRequestUrl(), content); Logger.LogInformation(1, "Response Content:{content}", rspContent); var parser = new WeChatPayXmlParser <T>(); var rsp = parser.Parse(rspContent); CheckResponseSign(rsp, useMD5, excludeSignType); return(rsp); }
public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse { var signType = true; // ture:MD5,false:HMAC-SHA256 var excludeSignType = true; if (CertificateClient == null) { throw new ArgumentNullException(nameof(Options.Certificate)); } // 字典排序 var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()); if (request is WeChatPayTransfersRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(mch_appid))) { sortedTxtParams.Add(mch_appid, Options.AppId); } sortedTxtParams.Add(mchid, Options.MchId); } else if (request is WeChatPayGetPublicKeyRequest) { sortedTxtParams.Add(mch_id, Options.MchId); sortedTxtParams.Add(sign_type, "MD5"); excludeSignType = false; } else if (request is WeChatPayPayBankRequest) { if (PublicKey == null) { throw new ArgumentNullException(nameof(Options.RsaPublicKey)); } var no = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_bank_no), PublicKey); sortedTxtParams.SetValue(enc_bank_no, no); var name = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_true_name), PublicKey); sortedTxtParams.SetValue(enc_true_name, name); sortedTxtParams.Add(mch_id, Options.MchId); sortedTxtParams.Add(sign_type, "MD5"); } else if (request is WeChatPayQueryBankRequest) { sortedTxtParams.Add(mch_id, Options.MchId); sortedTxtParams.Add(sign_type, "MD5"); } else if (request is WeChatPayGetTransferInfoRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, Options.AppId); } sortedTxtParams.Add(mch_id, Options.MchId); sortedTxtParams.Add(sign_type, "MD5"); } else if (request is WeChatPayDownloadFundFlowRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, Options.AppId); } sortedTxtParams.Add(mch_id, Options.MchId); signType = false; // HMAC-SHA256 } else if (request is WeChatPayRefundRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, Options.AppId); } sortedTxtParams.Add(mch_id, Options.MchId); } else if (request is WeChatPaySendRedPackRequest || request is WeChatPaySendGroupRedPackRequest) { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid))) { sortedTxtParams.Add(wxappid, Options.AppId); } sortedTxtParams.Add(mch_id, Options.MchId); } else // 其他接口 { if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid))) { sortedTxtParams.Add(appid, Options.AppId); } sortedTxtParams.Add(mch_id, Options.MchId); } sortedTxtParams.Add(nonce_str, Guid.NewGuid().ToString("N")); sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key, signType, excludeSignType)); var content = HttpClientEx.BuildContent(sortedTxtParams); Logger?.LogTrace(0, "Request:{content}", content); var body = await CertificateClient.DoPostAsync(request.GetRequestUrl(), content); Logger?.LogTrace(1, "Response:{body}", body); var parser = new WeChatPayXmlParser <T>(); var rsp = parser.Parse(body); CheckResponseSign(rsp, signType, excludeSignType); return(rsp); }