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