JeffreySu/WeiXinMPSDK · error · TenpayApiRequestException

当 为 时, 必填!

Error message

当 {nameof(data.type)} 为 {data.type} 时,{nameof(data.name)} 必填!

What it means

AddProfitsharingReceiverAsync validates the AddProfitsharingReceiverRequestData: if type is "MERCHANT_ID" the merchant name (name) is mandatory; when it is null, TenpayApiRequestException is thrown indicating data.name must be filled.

Solutions

  1. Provide data.name with the merchant's full registered name when type is "MERCHANT_ID".
  2. Add pre-call validation on your request DTO before invoking the API.
  3. Only omit name for receiver types that don't require it (e.g. PERSONAL_OPENID).

Example fix

// before
var data = new AddProfitsharingReceiverRequestData { type = "MERCHANT_ID", account = "819321922" };
// after
var data = new AddProfitsharingReceiverRequestData { type = "MERCHANT_ID", account = "819321922", name = "商户全称" };
Defensive patterns

Strategy: validation

Validate before calling

if (data.type == "MERCHANT_ID" && string.IsNullOrEmpty(data.name))
    throw new InvalidOperationException("MERCHANT_ID 分账接收方必须提供 name");

Try / catch

try { await apis.AddProfitsharingReceiverAsync(data); }
catch (TenpayApiRequestException ex) { logger.Error(ex, "添加分账接收方失败"); }

Prevention

When it happens

Trigger: Calling AddProfitsharingReceiverAsync to register a profit-sharing receiver with type="MERCHANT_ID" and name==null. Types like "PERSONAL_OPENID" don't trigger this check.

Common situations: Onboarding sub-merchants for profit sharing where the DB record lacks the merchant full name, or using deserialized request data where name was not supplied by the client.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Profitsharing/ProfitsharingApis.cs:320

            TenPayApiRequest tenPayApiRequest = new(_tenpayV3Setting);
            return await tenPayApiRequest.RequestAsync<FinishProfitsharingReturnJson>(url, data, timeOut);
        }

        /// <summary>
        /// 添加分账接收方接口
        /// <para>商户发起添加分账接收方请求,建立分账接收方列表。后续可通过发起分账请求,将分账方商户结算后的资金,分到该分账接收方</para>
        /// <para>普通商户 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_8.shtml </para>
        /// <para>服务商 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_8.shtml </para>
        /// <para>服务商连锁品牌 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_7_7.shtml</para>
        /// </summary>
        /// <param name="data">微信支付请求数据</param>
        /// <param name="timeOut">超时时间,单位为ms </param>
        /// <returns></returns>
        public async Task<AddProfitsharingReceiverReturnJson> AddProfitsharingReceiverAsync(AddProfitsharingReceiverRequestData data, int timeOut = Config.TIME_OUT)
        {
            if (data.type == "MERCHANT_ID" && data.name == null)
            {
                throw new TenpayApiRequestException($"当 {nameof(data.type)} 为 {data.type} 时,{nameof(data.name)} 必填!");
            }

            // name加密
            var basePayApis = new BasePayApis(_tenpayV3Setting);
            var publicKeys = await basePayApis.GetPublicKeysAsync();
            var publicKeyKv = publicKeys.FirstOrDefault();
            SecurityHelper.FieldEncrypt(data, publicKeyKv.Value, _tenpayV3Setting.EncryptionType.Value, _tenpayV3Setting.TenPayV3_TenPayPubKeyEnable);

            //string algorithmType = _tenpayV3Setting.EncryptionType == CertType.SM.ToString() ? "SM2" : "RSA";
            //var certificateResponse = await basePayApis.CertificatesAsync(algorithmType);
            //SecurityHelper.FieldEncrypt(data, certificateResponse, _tenpayV3Setting.TenPayV3_APIv3Key, _tenpayV3Setting.EncryptionType);

            var url = ReurnPayApiUrl(Senparc.Weixin.Config.TenPayV3Host + "/{0}v3/{1}profitsharing/receivers/add", data.brand_mchid);
            TenPayApiRequest tenPayApiRequest = new(_tenpayV3Setting, httpClient =>
            {
                httpClient.DefaultRequestHeaders.Add("Wechatpay-Serial", publicKeyKv.Key);
            });
            return await tenPayApiRequest.RequestAsync<AddProfitsharingReceiverReturnJson>(url, data, timeOut);

View on GitHub (pinned to be573f6f94)