public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams) { sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr()); sortedTxtParams.Add(WeChatPayConsts.appid, options.AppId); sortedTxtParams.Add(WeChatPayConsts.sub_appid, options.SubAppId); sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId); sortedTxtParams.Add(WeChatPayConsts.sub_mch_id, options.SubMchId); if (signType == WeChatPaySignType.HMAC_SHA256) { sortedTxtParams.Add(WeChatPayConsts.sign_type, WeChatPayConsts.HMAC_SHA256); } sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.APIKey, signType)); }
public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams) { if (string.IsNullOrEmpty(options.RsaPublicKey)) { throw new WeChatPayException($"{nameof(WeChatPayPayBankRequest)}.{nameof(PrimaryHandler)}: {nameof(options.RsaPublicKey)} is null or empty!"); } sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr()); sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId); sortedTxtParams.Add(WeChatPayConsts.enc_bank_no, OaepSHA1WithRSA.Encrypt(BankNo, options.RsaPublicKey)); sortedTxtParams.Add(WeChatPayConsts.enc_true_name, OaepSHA1WithRSA.Encrypt(TrueName, options.RsaPublicKey)); sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType)); }
public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse { // 字典排序 var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()); if (request is WeChatPayTransfersRequest) // 企业付款到零钱 { sortedTxtParams.Add(MCHAPPID, Options.AppId); sortedTxtParams.Add(MCHID, Options.MchId); } else if (request is WeChatPayGetPublicKeyRequest || request is WeChatPayPayBankRequest || 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 // 其他接口 { sortedTxtParams.Add(APPID, Options.AppId); sortedTxtParams.Add(MCH_ID, Options.MchId); } if (request is WeChatPayPayBankRequest) // 企业付款到银行卡接口 { var no = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_BANK_NO), Options.RsaPublicKey); sortedTxtParams.SetValue(ENC_BANK_NO, no); var name = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_TRUE_NAME), Options.RsaPublicKey); sortedTxtParams.SetValue(ENC_TRUE_NAME, name); } sortedTxtParams.Add(NONCE_STR, Guid.NewGuid().ToString("N")); sortedTxtParams.Add(SIGN, Md5.GetMD5WithKey(sortedTxtParams, Options.Key, !(request is WeChatPayGetPublicKeyRequest))); // 获取公钥 不排除sign_type var body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams); var parser = new WeChatPayXmlParser <T>(); var rsp = parser.Parse(body); CheckResponseSign(rsp); return(rsp); }
public void PrimaryHandler(WeChatPayOptions options, WeChatPayDictionary sortedTxtParams) { sortedTxtParams.Add(WeChatPayConsts.timeStamp, WeChatPayUtility.GetTimeStamp()); sortedTxtParams.Add(WeChatPayConsts.nonceStr, WeChatPayUtility.GenerateNonceStr()); if (!string.IsNullOrEmpty(options.SubAppId)) { sortedTxtParams.Add(WeChatPayConsts.appId, options.SubAppId); } else { sortedTxtParams.Add(WeChatPayConsts.appId, options.AppId); } sortedTxtParams.Add(WeChatPayConsts.signType, WeChatPayConsts.MD5); sortedTxtParams.Add(WeChatPayConsts.paySign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.APIKey, WeChatPaySignType.MD5)); }
private void CheckResponseSign(WeChatPayResponse response, bool useMD5 = true, bool excludeSignType = true) { if (string.IsNullOrEmpty(response.Body)) { throw new Exception("sign check fail: Body is Empty!"); } var sign = response?.Sign; if (!response.IsError && !string.IsNullOrEmpty(sign)) { var cal_sign = WeChatPaySignature.SignWithKey(response.Parameters, Options.Key, useMD5, excludeSignType); if (cal_sign != sign) { throw new Exception("sign check fail: check Sign and Data Fail!"); } } }
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); } }
private void CheckNotifySign(WeChatPayNotifyResponse response) { if (response?.Parameters?.Count == 0) { throw new Exception("sign check fail: Body is Empty!"); } if (!response.Parameters.TryGetValue("sign", out var sign)) { throw new Exception("sign check fail: sign is Empty!"); } var cal_sign = WeChatPaySignature.SignWithKey(response.Parameters, Options.Key); if (cal_sign != sign) { throw new Exception("sign check fail: check Sign and Data Fail!"); } }
private void CheckResponseSign(WeChatPayResponse response, bool useMD5 = true, bool excludeSignType = true) { if (string.IsNullOrEmpty(response.Body)) { throw new Exception("sign check fail: Body is Empty!"); } if (response.Parameters.TryGetValue("sign", out var sign)) { if (response.Parameters["return_code"] == "SUCCESS" && !string.IsNullOrEmpty(sign)) { var cal_sign = WeChatPaySignature.SignWithKey(response.Parameters, Options.Key, useMD5, excludeSignType); if (cal_sign != sign) { throw new Exception("sign check fail: check Sign and Data Fail!"); } } } }
public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams) { sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr()); sortedTxtParams.Add(WeChatPayConsts.wxappid, options.AppId); sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId); var signList = new List <string> { "act_name", "mch_billno", "mch_id", "nonce_str", "re_openid", "total_amount", "wxappid", }; sortedTxtParams.Add(WeChatPayConsts.workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.AppSecret, signList)); sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType)); }
public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams) { if (string.IsNullOrEmpty(options.RsaPublicKey)) { throw new WeChatPayException("WeChatPayPayBankRequest: RsaPublicKey is null!"); } sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr()); sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId); var no = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_bank_no), options.RsaPublicKey); sortedTxtParams.SetValue(WeChatPayConsts.enc_bank_no, no); var name = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_true_name), options.RsaPublicKey); sortedTxtParams.SetValue(WeChatPayConsts.enc_true_name, name); sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType)); }
public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams) { sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr()); sortedTxtParams.Add(WeChatPayConsts.appid, options.AppId); sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId); var signList = new List<string> { "amount", "appid", "desc", "mch_id", "nonce_str", "openid", "partner_trade_no", "ww_msg_type", }; sortedTxtParams.Add(WeChatPayConsts.workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.Secret, signList)); sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType)); }
private void CheckNotifySign(WeChatPayNotifyResponse response) { if (response?.Parameters?.Count == 0) { throw new Exception("sign check fail: Body is Empty!"); } var sign = response?.Sign; if (string.IsNullOrEmpty(sign)) { throw new Exception("sign check fail: sign is Empty!"); } var cal_sign = WeChatPaySignature.SignWithKey(response.Parameters, Options.Key); if (cal_sign != sign) { throw new Exception("sign check fail: check Sign and Data Fail!"); } }
public Task <WeChatPayDictionary> ExecuteAsync(IWeChatPayCalcRequest request) { try { 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, "MD5"); sortedTxtParams.Add(paySign, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key)); } return(Task.FromResult(sortedTxtParams)); } catch (Exception ex) { logger?.LogError($"{DateTime.Now} ExecuteAsync", ex.Message); return(null); } }
public WeChatPayCertificateClient( IOptions <WeChatPayOptions> optionsAccessor, ILogger <WeChatPayCertificateClient> logger) { Options = optionsAccessor?.Value ?? new WeChatPayOptions(); Logger = logger; 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)); } if (string.IsNullOrEmpty(Options.Certificate)) { throw new ArgumentNullException(nameof(Options.Certificate)); } var clientHandler = new HttpClientHandler(); var certificate = Convert.FromBase64String(Options.Certificate); clientHandler.ClientCertificates.Add(new X509Certificate2(certificate, Options.MchId, X509KeyStorageFlags.MachineKeySet)); Client = new HttpClientEx(clientHandler); if (!string.IsNullOrEmpty(Options.RsaPublicKey)) { RsaPublicParameters = WeChatPaySignature.GetPublicKeyParameters(Options.RsaPublicKey); } }
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); } }
private void CheckNotifySign(WeChatPayNotify notify, WeChatPayOptions options) { if (string.IsNullOrEmpty(notify.ResponseBody)) { throw new WeChatPayException("sign check fail: Body is Empty!"); } if (notify.ResponseParameters.Count == 0) { throw new WeChatPayException("sign check fail: Parameters is Empty!"); } if (!notify.ResponseParameters.TryGetValue("sign", out var sign)) { throw new WeChatPayException("sign check fail: sign is Empty!"); } var cal_sign = WeChatPaySignature.SignWithKey(notify.ResponseParameters, options.Key, WeChatPaySignType.MD5); if (cal_sign != sign) { throw new WeChatPayException("sign check fail: check Sign and Data Fail!"); } }
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) where T : WeChatPayResponse { try { 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(notify_url, Options.RefundNotifyUrl); 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.LogInformation($"{DateTime.Now} content:{content}"); logger.LogInformation($"{DateTime.Now} GetRequestUrl:{request.GetRequestUrl()}"); var body = await CertificateClient.DoPostAsync(request.GetRequestUrl(), content); logger.LogInformation($"{DateTime.Now} body:{body}"); var parser = new WeChatPayXmlParser <T>(); var rsp = parser.Parse(body); CheckResponseSign(rsp, signType, excludeSignType); return(rsp); } catch (Exception ex) { logger.LogInformation($"{DateTime.Now} ExecuteAsync6666:{ex.Message}", ex.Message); return(null); } }
public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse { var useMD5 = true; var excludeSignType = true; // 字典排序 var sortedTxtParams = new WeChatPayDictionary(request.GetParameters()); if (request is WeChatPayTransfersRequest) { sortedTxtParams.Add(MCHAPPID, 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) { var no = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_BANK_NO), Options.RsaPublicParameters); sortedTxtParams.SetValue(ENC_BANK_NO, no); var name = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_TRUE_NAME), Options.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 // 其他接口 { 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 body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams); var parser = new WeChatPayXmlParser <T>(); var rsp = parser.Parse(body); CheckResponseSign(rsp, useMD5, excludeSignType); return(rsp); }
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, string certificateName = "Default") where T : WeChatPayResponse { var signType = true; // ture:MD5,false:HMAC-SHA256 var excludeSignType = true; // 字典排序 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 (Options.PublicKey == null) { throw new ArgumentNullException(nameof(Options.RsaPublicKey)); } 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); 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 = WeChatPayUtility.BuildContent(sortedTxtParams); Logger?.LogTrace(0, "Request:{content}", content); using (var client = ClientFactory.CreateClient(WeChatPayOptions.CertificateClientName + "." + certificateName)) { var body = await HttpClientUtility.DoPostAsync(client, 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); } }
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); } }