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