{"record":{"id":"cd22295806dbbc56","repo":"JeffreySu/WeiXinMPSDK","slug":"nameof-each-type-each-type-nameof-each-name","errorCode":null,"errorMessage":"当 {nameof(each.type)} 为 {each.type} 时，{nameof(each.name)} 必填！","messagePattern":"当 (.+?) 为 (.+?) 时，(.+?) 必填！","errorType":"validation","errorClass":"TenpayApiRequestException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Profitsharing/ProfitsharingApis.cs","lineNumber":108,"sourceCode":"        #region 分账接口\n\n        /// <summary>\n        /// 请求分账接口\n        /// <para>微信订单支付成功后，商户发起分账请求，将结算后的资金分到分账接收方</para>\n        /// <para>普通商户 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_1.shtml </para>\n        /// <para>服务商 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_1.shtml </para>\n        /// <para>服务商连锁商户 更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_7_1.shtml </para>\n        /// </summary>\n        /// <param name=\"data\">微信支付请求数据</param>\n        /// <param name=\"timeOut\">超时时间，单位为ms</param>\n        /// <returns></returns>\n        public async Task<CreateProfitsharingReturnJson> CreateProfitsharingAsync(CreateProfitsharingRequestData data, int timeOut = Config.TIME_OUT)\n        {\n            foreach (var each in data.receivers)\n            {\n                if (each.type == \"MERCHANT_ID\" && each.name == null)\n                {\n                    throw new TenpayApiRequestException($\"当 {nameof(each.type)} 为 {each.type} 时，{nameof(each.name)} 必填！\");\n                }\n            }\n\n            // name加密\n            var basePayApis = new BasePayApis(_tenpayV3Setting);\n            var publicKeys = await basePayApis.GetPublicKeysAsync();\n            var publicKeyKv = publicKeys.FirstOrDefault();\n            foreach (var each in data.receivers)\n            {\n                SecurityHelper.FieldEncrypt(each, publicKeyKv.Value, _tenpayV3Setting.EncryptionType.Value, _tenpayV3Setting.TenPayV3_TenPayPubKeyEnable);\n            }\n\n            //string algorithmType = _tenpayV3Setting.EncryptionType == CertType.SM.ToString() ? \"SM2\" : \"RSA\";\n            //var certificateResponse = await basePayApis.CertificatesAsync(algorithmType);\n            //foreach (var each in data.receivers)\n            //{\n            //    SecurityHelper.FieldEncrypt(each, certificateResponse, _tenpayV3Setting.TenPayV3_APIv3Key, _tenpayV3Setting.EncryptionType);\n            //}","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Profitsharing/ProfitsharingApis.cs#L90-L126","documentation":"CreateProfitsharingAsync validates each entry in data.receivers before the profit-sharing request. When a receiver's type is \"MERCHANT_ID\", WeChat Pay requires the merchant name; if name is null a TenpayApiRequestException is thrown naming the type and field.","triggerScenarios":"Calling CreateProfitsharingAsync with a receivers array containing an item with type=\"MERCHANT_ID\" and name==null; note other types (e.g. \"PERSONAL_OPENID\") do not require name.","commonSituations":"Building CreateProfitsharingRequestData dynamically where name is only filled for personal receivers, or copying sample code that omits the merchant name field.","solutions":["Set each.receiver.name to the merchant's registered name when type is \"MERCHANT_ID\".","Filter or fix receivers entries before constructing CreateProfitsharingRequestData.","Wrap the call in try-catch for TenpayApiRequestException if receivers are user-supplied."],"exampleFix":"// before\nreceivers: new[] { new CreateProfitsharingReceiver { type = \"MERCHANT_ID\", account = \"819321922\", amount = 100 } }\n// after\nreceivers: new[] { new CreateProfitsharingReceiver { type = \"MERCHANT_ID\", account = \"819321922\", name = \"商户全称\", amount = 100 } }","handlingStrategy":"validation","validationCode":"foreach (var r in receivers)\n    if (r.type == \"MERCHANT_ID\" && string.IsNullOrEmpty(r.name))\n        throw new InvalidOperationException($\"receiver {r.account} 需要填写 name (MERCHANT_ID)\");","typeGuard":null,"tryCatchPattern":"try { await apis.CreateProfitsharingAsync(data); }\ncatch (TenpayApiRequestException ex) { logger.Error(ex, \"分账参数校验失败\"); }","preventionTips":["Validate receiver DTOs at your API boundary before calling the SDK","Keep merchant full name stored alongside merchant ID in your DB","Remember name is only required for MERCHANT_ID type"],"tags":["validation","profitsharing","required-field","wechat-pay"],"backgroundTag":"missing-required-argument","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"}