JeffreySu/WeiXinMPSDK · critical · Senparc.Weixin.Exceptions.WeixinException

没有设置证书加密类型(EncryptionType)

Error message

没有设置证书加密类型(EncryptionType)

What it means

BasePayApis' constructor validates the TenpayV3SenparcWeixinSetting and throws a WeixinException when the EncryptionType property has no value. EncryptionType (e.g. RSA with public/private key) is required to encrypt sensitive fields for WeChat Pay v3, so the client cannot operate without it.

Solutions

  1. Set EncryptionType in your TenpayV3Setting configuration (e.g. "EncryptionType": "RSA").
  2. Verify Config.SenparcWeixinSetting.TenpayV3Setting is fully populated before constructing BasePayApis.
  3. If passing a custom ISenparcWeixinSettingForTenpayV3, explicitly assign EncryptionType in the initializer.

Example fix

// before
var setting = new TenpayV3SenparcWeixinSetting { AppId = appId, MchId = mchId };
var apis = new BasePayApis(setting);
// after
var setting = new TenpayV3SenparcWeixinSetting { AppId = appId, MchId = mchId, EncryptionType = EncryptionTypeEnum.RSA };
var apis = new BasePayApis(setting);
Defensive patterns

Strategy: validation

Validate before calling

if (setting.EncryptionType is null)
    throw new InvalidOperationException("TenpayV3Setting.EncryptionType must be set before creating BasePayApis.");

Type guard

bool encryptionConfigured = setting?.EncryptionType is { };

Try / catch

try { var apis = new BasePayApis(setting); }
catch (WeixinException ex) { logger.Fatal(ex, "EncryptionType missing from TenpayV3Setting"); throw; }

Prevention

When it happens

Trigger: Instantiating BasePayApis with a settings object where EncryptionType is not assigned — e.g. config JSON/appsettings missing the EncryptionType field, or constructing ISenparcWeixinSettingForTenpayV3 in code without setting it.

Common situations: Upgrading from older SDK versions that had no EncryptionType property and not updating config; incomplete appsettings sections; copying config templates that omit the new field.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/f7177ac6b3b7359d. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/BasePay/BasePayApis.cs:99

namespace Senparc.Weixin.TenPayV3.Apis
{
    public partial class BasePayApis
    {

        private ISenparcWeixinSettingForTenpayV3 _tenpayV3Setting;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="senparcWeixinSettingForTenpayV3"></param>
        public BasePayApis(ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null)
        {
            _tenpayV3Setting = senparcWeixinSettingForTenpayV3 ?? Senparc.Weixin.Config.SenparcWeixinSetting.TenpayV3Setting;

            if (!_tenpayV3Setting.EncryptionType.HasValue)
            {
                throw new Senparc.Weixin.Exceptions.WeixinException("没有设置证书加密类型(EncryptionType)");
            }
        }

        //private readonly IServiceProvider _serviceProvider;

        //public BasePayApis(IServiceProvider serviceProvider)
        //{
        //    this._serviceProvider = serviceProvider;
        //}

        /// <summary>
        /// 返回可用的微信支付地址(自动判断是否使用沙箱)
        /// </summary>
        /// <param name="urlFormat">如:<code>https://api.mch.weixin.qq.com/{0}pay/unifiedorder</code></param>
        /// <returns></returns>
        internal static string GetPayApiUrl(string urlFormat, string sp_mchid = "")
        {
            //注意:目前微信支付 V3 还没有支持沙箱,此处只是预留

View on GitHub (pinned to be573f6f94)