JeffreySu/WeiXinMPSDK · error · TenpayApiRequestException
未获取到用于加密收款用户姓名的微信支付公钥或平台证书。
Error message
未获取到用于加密收款用户姓名的微信支付公钥或平台证书。
What it means
This TenpayApiRequestException is thrown by CreateAuthorizationRequestAsync (FundAppApis.Authorization.cs:150) when the library cannot find a WeChat Pay public key or platform certificate to encrypt the payee user's real name before sending the authorization request to the fund/transfer API. WeChat Pay requires sensitive fields like names to be encrypted with a downloaded public key or platform certificate; if neither was loaded from the configured certificates/public keys collection, encryption is impossible and the library aborts instead of sending plaintext.
Solutions
- Configure the TenPayV3 setting with a valid WeChat Pay public key (or platform certificates) so publicKeys contains at least one non-empty Key/Value entry
- If using public-key mode, enable TenPayV3_TenPayPubKeyEnable and store the downloaded WeChat Pay public key (pub_key.pem) in the setting
- Verify the certificate/public key file path and that the file content parses to a non-empty Key and Value at startup
- Re-download or refresh platform certificates if they have expired; log the publicKeys collection before the call to confirm it is populated
Example fix
// before: no public key configured, call throws
await fundAppApis.CreateAuthorizationRequestAsync(request);
// after: ensure encryption material exists first
if (string.IsNullOrWhiteSpace(_tenpayV3Setting.TenPayV3_CertificatePublicKeys))
{
_tenpayV3Setting.TenPayV3_CertificatePublicKeys = File.ReadAllText("pub_key.pem");
}
await fundAppApis.CreateAuthorizationRequestAsync(request); Defensive patterns
Strategy: validation
Validate before calling
var pubKeys = _tenpayV3Setting?.GetPublicKeys?.Invoke();
if (pubKeys == null || pubKeys.Count == 0 || pubKeys.Values.Any(k => string.IsNullOrWhiteSpace(k.Key) || string.IsNullOrWhiteSpace(k.Value)))
throw new InvalidOperationException("WeChat Pay public key / platform certificate not configured before calling FundApp APIs."); Type guard
bool HasEncryptionMaterial(TenPayV3Setting s) =>
s != null && s.EncryptionType.HasValue &&
s.GetPublicKeys != null && s.GetPublicKeys().Any(k => !string.IsNullOrWhiteSpace(k.Key) && !string.IsNullOrWhiteSpace(k.Value)); Try / catch
try { await fundAppApis.CreateAuthorizationRequestAsync(request); }
catch (TenpayApiRequestException ex) when (ex.Message.Contains("公钥或平台证书"))
{ logger.LogError(ex, "Missing WeChat Pay encryption key material; check TenPayV3 settings."); throw; } Prevention
- Load and verify public key / certificate material at application startup, not at request time
- Enable TenPayV3_TenPayPubKeyEnable only after downloading pub_key.pem from WeChat Pay merchant console
- Log the number of loaded certificates/keys in health checks
- Refresh certificates before expiry
When it happens
Trigger: Calling CreateAuthorizationRequestAsync while _tenpayV3Setting has no usable entry in the publicKeys collection: the loaded platform certificates/public key JSON is empty, SelectPaymentPublicKey returned an entry whose Key or Value is null/whitespace, or certificate download/refresh failed silently before this call.
Common situations: TenPayV3 settings missing TenPayV3_CertificatePublicKeys or public key path configured to a nonexistent file; WeChat Pay V3 public key mode (TenPayV3_TenPayPubKeyEnable) enabled but the WeChat Pay public key not downloaded; certificate expired or replaced by WeChat so the cached blob no longer parses; running with an empty platform certificate list in environments without network access to fetch certificates.
Related errors
- 未获取到用于加密分账接收方姓名的微信支付公钥或平台证书。
- 没有设置证书加密类型(EncryptionType)
- 没有设置证书加密类型(EncryptionType)
- 没有设置证书加密类型(EncryptionType)
- 没有设置证书加密类型(EncryptionType)
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/bae022e7ac4c1424.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/FundApp/FundAppApis.Authorization.cs:150
object target, bool containsSensitiveData)
{
if (!containsSensitiveData)
{
return new TenPayApiRequest(_tenpayV3Setting);
}
var publicKey = GetConfiguredPaymentPublicKey();
if (string.IsNullOrWhiteSpace(publicKey.Key))
{
var publicKeys = await new BasePayApis(_tenpayV3Setting)
.GetPublicKeysAsync().ConfigureAwait(false);
publicKey = SelectPaymentPublicKey(publicKeys);
}
if (string.IsNullOrWhiteSpace(publicKey.Key) ||
string.IsNullOrWhiteSpace(publicKey.Value))
{
throw new TenpayApiRequestException(
"未获取到用于加密收款用户姓名的微信支付公钥或平台证书。");
}
SecurityHelper.FieldEncrypt(target, publicKey.Value,
_tenpayV3Setting.EncryptionType.Value,
_tenpayV3Setting.TenPayV3_TenPayPubKeyEnable);
return new TenPayApiRequest(_tenpayV3Setting, httpClient =>
httpClient.DefaultRequestHeaders.Add("Wechatpay-Serial", publicKey.Key));
}
private KeyValuePair<string, string> GetConfiguredPaymentPublicKey()
{
if (!_tenpayV3Setting.TenPayV3_TenPayPubKeyEnable)
{
return default;
}
return new KeyValuePair<string, string>(View on GitHub (pinned to be573f6f94)