JeffreySu/WeiXinMPSDK · error · WeixinException

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

Error message

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

What it means

TenPaySignHelper.CreatePaySign builds JSAPI pay signatures and needs to know the certificate/encryption type (TenPayV3_CertType, e.g. RSA vs SM2). If TenPayV3Info.TenPayV3_CertType is not set, a WeixinException is thrown.

Solutions

  1. Set TenPayV3_CertType on your TenPayV3Setting (e.g. TenPayV3CertType.RSA) when initializing config.
  2. Update configuration initialization code to the current Senparc TenPayV3 setup API.
  3. If using SM2 merchant, set the corresponding cert type so signing uses the right algorithm.

Example fix

// before
var tenPayV3Info = new TenPayV3Info(appId, mchId, key, appSecret, certPath, certPassword);
// after
var tenPayV3Info = new TenPayV3Info(appId, mchId, key, appSecret, certPath, certPassword);
tenPayV3Info.TenPayV3_CertType = TenPayV3CertType.RSA;
Defensive patterns

Strategy: try-catch

Validate before calling

if (tenPayV3Info.TenPayV3_CertType == null)
    throw new InvalidOperationException("请先设置 TenPayV3_CertType (RSA/SM2)");

Try / catch

try { var sign = TenPaySignHelper.CreatePaySign(timeStamp, nonceStr, package, tenPayV3Info); }
catch (WeixinException ex) { logger.Error(ex, "证书类型未配置"); }

Prevention

When it happens

Trigger: Calling CreatePaySign when the TenPayV3Setting/TenPayV3Info used to construct tenPayV3Info was created without specifying TenPayV3_CertType (null), typically after upgrading to a Senparc version where CertType became mandatory.

Common situations: Older configuration code that predates the CertType field, or config loaded from legacy sources that don't include the certificate type.

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/0bcfd57e28175574. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Helpers/TenPaySignHelper.cs:144

            return CreatePaySign(timeStamp, nonceStr, package, new TenPayV3Info(senparcWeixinSettingForTenpayV3));
        }

        /// <summary>
        /// 获取调起支付所需的签名
        /// </summary>
        /// <param name="timeStamp">时间戳</param>
        /// <param name="nonceStr">随机串</param>
        /// <param name="package">格式:prepay_id={0}</param>
        /// <param name="tenPayV3Info">支付配置信息(TenPayV3Info)</param>
        /// <returns></returns>
        public static string CreatePaySign(string timeStamp, string nonceStr, string package, TenPayV3Info tenPayV3Info)
        {
            var appId = tenPayV3Info.AppId;
            var privateKey = tenPayV3Info.TenPayV3_PrivateKey;

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

            return CreatePaySign(timeStamp, nonceStr, package, appId, privateKey, tenPayV3Info.TenPayV3_CertType.Value);
        }

        /// <summary>
        /// 获取调起支付所需的签名
        /// </summary>
        /// <param name="timeStamp">时间戳</param>
        /// <param name="nonceStr">随机串</param>
        /// <param name="package">格式:prepay_id={0}</param>
        /// <param name="privateKey">商户证书私钥</param>
        /// <returns></returns>
        public static string CreatePaySign(string timeStamp, string nonceStr, string package, string appId, string privateKey, CertType certType)
        {
            string contentForSign = $"{appId}\n{timeStamp}\n{nonceStr}\n{package}\n";
            return CreateSign(certType, contentForSign, privateKey);
        }

View on GitHub (pinned to be573f6f94)