{"record":{"id":"f0dcfdb2deac5a33","repo":"JeffreySu/WeiXinMPSDK","slug":"encryptiontype-medicalinsuranceapis","errorCode":null,"errorMessage":"没有设置证书加密类型（EncryptionType）","messagePattern":"没有设置证书加密类型（EncryptionType）","errorType":"exception","errorClass":"Senparc.Weixin.Exceptions.WeixinException","httpStatus":null,"severity":"critical","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/MedicalInsurance/MedicalInsuranceApis.cs","lineNumber":180,"sourceCode":"                effectiveAppId = !string.IsNullOrWhiteSpace(_tenpayV3Setting.TenPayV3_SubAppId)\n                    ? _tenpayV3Setting.TenPayV3_SubAppId\n                    : _tenpayV3Setting.TenPayV3_AppId;\n            }\n\n            var result = new MedicalInsurancePayPackage\n            {\n                mixTradeNo = mixTradeNo,\n                appid = includeAppId ? effectiveAppId : null\n            };\n\n            if (string.IsNullOrWhiteSpace(prepayId))\n            {\n                return result;\n            }\n\n            if (!_tenpayV3Setting.EncryptionType.HasValue)\n            {\n                throw new Senparc.Weixin.Exceptions.WeixinException(\n                    \"没有设置证书加密类型（EncryptionType）\");\n            }\n\n            result.timeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();\n            result.nonceStr = Guid.NewGuid().ToString(\"N\");\n            result.package = $\"prepay_id={prepayId}\";\n            result.signType = \"RSA\";\n            result.paySign = TenPaySignHelper.CreatePaySign(result.timeStamp, result.nonceStr,\n                result.package, effectiveAppId, _tenpayV3Setting.TenPayV3_PrivateKey,\n                _tenpayV3Setting.EncryptionType.Value);\n            return result;\n        }\n\n        private static string BuildSubMchIdQuery(string subMchId)\n        {\n            return string.IsNullOrWhiteSpace(subMchId)\n                ? string.Empty\n                : $\"?sub_mchid={Escape(subMchId)}\";","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/MedicalInsurance/MedicalInsuranceApis.cs#L162-L198","documentation":"CreatePayPackage (MedicalInsuranceApis.cs:180) builds the JS/mini-program pay package and must sign it, which requires knowing the certificate encryption type (RSA or SM). If _tenpayV3Setting.EncryptionType is null — i.e. the TenPayV3 settings never specified EncryptionType — a WeixinException is thrown before the package can be produced.","triggerScenarios":"Calling CreatePayPackage (directly or via CreateMiniProgramPayPackage / CreateJsApiPayPackage) with a TenPayV3Setting whose EncryptionType property has no value.","commonSituations":"Newly registered WeChat Pay merchants on H5/mini-program platforms must use RSA (older app/product profiles allowed SM), and the setting was left unset; migrating config files where the EncryptionType key was dropped; constructing TenPayV3Setting manually in code without setting the property.","solutions":["Set EncryptionType on the TenPayV3Setting (e.g. Senparc.Weixin.TenPay.TenPayV3Type.RSA) at configuration/startup time","Check your config source (appsettings/env) is actually bound to the EncryptionType field","Rebuild the TenPayV3Setting used by MedicalInsuranceApis so it includes EncryptionType before creating pay packages"],"exampleFix":"// before\nvar setting = new TenPayV3Setting { TenPayV3_AppId = appId, ... }; // EncryptionType unset\n\n// after\nvar setting = new TenPayV3Setting {\n    ...,\n    EncryptionType = TenPayV3Type.RSA\n};","handlingStrategy":"validation","validationCode":"if (!_tenpayV3Setting.EncryptionType.HasValue)\n    throw new InvalidOperationException(\"TenPayV3 EncryptionType must be configured (RSA for new merchants) before creating pay packages\");","typeGuard":"bool ReadyForPayPackage(TenPayV3Setting s) => s != null && s.EncryptionType.HasValue;","tryCatchPattern":"try { var pkg = api.CreateJsApiPayPackage(prepayId); }\ncatch (WeixinException ex) when (ex.Message.Contains(\"EncryptionType\"))\n{ logger.LogCritical(ex, \"TenPayV3 EncryptionType missing in configuration\"); throw; }","preventionTips":["Set EncryptionType in appsettings and assert it at startup","Prefer RSA for all new WeChat Pay V3 integrations","Add a startup config validation step for all TenPayV3 settings"],"tags":["tenpay","medical-insurance","config","signing"],"backgroundTag":"missing-required-config-field","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"}