{"record":{"id":"a4386f99460a9282","repo":"JeffreySu/WeiXinMPSDK","slug":"securityechoapis","errorCode":null,"errorMessage":"未获取到用于安全探测的微信支付公钥或平台证书。","messagePattern":"未获取到用于安全探测的微信支付公钥或平台证书。","errorType":"exception","errorClass":"TenpayApiRequestException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Security/SecurityEchoApis.cs","lineNumber":72,"sourceCode":"                $\"{Senparc.Weixin.Config.TenPayV3Host}/{{0}}v3/security/echo\");\n            return await request.RequestAsync<SecurityEchoReturnJson>(url, data, timeOut)\n                .ConfigureAwait(false);\n        }\n\n        private async Task<TenPayApiRequest> CreateSecurityRequestAsync(object target)\n        {\n            var publicKey = GetConfiguredPaymentPublicKey();\n            if (string.IsNullOrWhiteSpace(publicKey.Key))\n            {\n                var publicKeys = await new BasePayApis(_setting)\n                    .GetPublicKeysAsync().ConfigureAwait(false);\n                publicKey = SelectPaymentPublicKey(publicKeys);\n            }\n\n            if (string.IsNullOrWhiteSpace(publicKey.Key) ||\n                string.IsNullOrWhiteSpace(publicKey.Value))\n            {\n                throw new TenpayApiRequestException(\n                    \"未获取到用于安全探测的微信支付公钥或平台证书。\");\n            }\n\n            SecurityHelper.FieldEncrypt(target, publicKey.Value,\n                _setting.EncryptionType.Value,\n                _setting.TenPayV3_TenPayPubKeyEnable);\n            return new TenPayApiRequest(_setting, httpClient =>\n                httpClient.DefaultRequestHeaders.Add(\"Wechatpay-Serial\", publicKey.Key));\n        }\n\n        private KeyValuePair<string, string> GetConfiguredPaymentPublicKey()\n        {\n            if (!_setting.TenPayV3_TenPayPubKeyEnable)\n            {\n                return default;\n            }\n\n            return new KeyValuePair<string, string>(","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Security/SecurityEchoApis.cs#L54-L90","documentation":"CreateSecurityRequestAsync in SecurityEchoApis encrypts sensitive fields before the WeChat Pay security/echo API call. It needs a WeChat Pay public key or platform certificate for field encryption; when none is resolvable (SelectPaymentPublicKey returns empty / no keys), it throws TenpayApiRequestException.","triggerScenarios":"Calling security echo/sensitive-info encryption APIs (request) when _tenpayV3Setting has no usable public key: TenPayV3_TenPayPubKey not configured and no platform certificate is available via GetPublicKeysAsync.","commonSituations":"New merchants using public-key mode who haven't downloaded the WeChat Pay public key, or merchants whose platform certificate expired/was never fetched, so the key dictionary is empty.","solutions":["Configure TenPayV3_TenPayPubKey (the WeChat Pay public key from the merchant platform) in TenPayV3Setting.","Ensure platform certificates are downloaded/refreshed so GetPublicKeysAsync returns at least one key.","Check Weixin's certificate/auto-update mode and network access to WeChat Pay certificate endpoint."],"exampleFix":"// before\nvar setting = new TenPayV3Setting(appId, mchId, subMchId, tenPayV3_Key, certPath, certPassword);\n// after\nvar setting = new TenPayV3Setting(appId, mchId, subMchId, tenPayV3_Key, certPath, certPassword,\n    tenPayV3_TenPayPubKey: \"PUB_KEY_ID_xxx|<public key content>\");","handlingStrategy":"try-catch","validationCode":"var hasKey = !string.IsNullOrWhiteSpace(setting.TenPayV3_TenPayPubKey);\n// or confirm GetPublicKeysAsync returns at least one key before calling sensitive APIs","typeGuard":null,"tryCatchPattern":"try { await securityApis.CreateSecurityRequestAsync(...); }\ncatch (TenpayApiRequestException ex) when (ex.Message.Contains(\"公钥\"))\n{ logger.Error(ex, \"未配置微信支付公钥/平台证书\"); }","preventionTips":["Configure TenPayV3_TenPayPubKey at startup for public-key mode","Keep platform certificate auto-refresh enabled","Fail fast at app startup with a config check for the public key"],"tags":["wechat-pay","encryption","configuration","missing-credentials"],"backgroundTag":"missing-credentials","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}