JeffreySu/WeiXinMPSDK · error · ArgumentException

out_trade_no、transaction_id、sub_order_no 和 sub_order_id…

Error message

out_trade_no、transaction_id、sub_order_no 和 sub_order_id 至少填写一个。

What it means

Validate() on the base customs request data requires at least one order identifier: out_trade_no, transaction_id, sub_order_no, or sub_order_id. If all four are null/empty, an ArgumentException with this message is thrown. WeChat Pay's customs APIs identify the order by at least one of these keys.

Solutions

  1. Set out_trade_no (merchant order number) — the most common identifier.
  2. Alternatively set transaction_id (WeChat payment transaction id), or sub_order_no/sub_order_id for split orders.
  3. Ensure at least one is non-whitespace before calling the customs API.

Example fix

// before
var data = new CustomsDeclareQueryRequestData(key, appid, mch_id, mch_customs_no, null, null, null, null);
// after
var data = new CustomsDeclareQueryRequestData(key, appid, mch_id, mch_customs_no, outTradeNo /* out_trade_no */, null, null, null);
Defensive patterns

Strategy: validation

Validate before calling

// C#
bool hasOrderKey = new[]{ outTradeNo, transactionId, subOrderNo, subOrderId }.Any(s => !string.IsNullOrWhiteSpace(s));
if (!hasOrderKey) throw new InvalidOperationException("Provide at least one order identifier for customs request");

Try / catch

try { await tenPayV3.CustomsDeclareQueryAsync(data); }
catch (ArgumentException ex) { log.LogWarning("Customs request missing order identifier: {Msg}", ex.Message); }

Prevention

When it happens

Trigger: Constructing a customs declare/query/redeclare request and calling the API (Validate runs before SetParameters) without populating any of the four order identifier fields.

Common situations: Developers intend to query by WeChat transaction_id but the value came back null from their own order system, or they only set order fields for the domestic side (sub-order fields) and none of the four accepted identifiers.

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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPay/V3/Universal/Customs/CustomsRequestData.cs:190

        /// <summary>微信支付订单号,与其他订单标识至少填写一个。</summary>
        public string transaction_id { get; set; }

        /// <summary>商户子订单号,与其他订单标识至少填写一个。</summary>
        public string sub_order_no { get; set; }

        /// <summary>微信子订单号,与其他订单标识至少填写一个。</summary>
        public string sub_order_id { get; set; }

        /// <inheritdoc />
        protected override void Validate(string key)
        {
            base.Validate(key);
            if (string.IsNullOrWhiteSpace(out_trade_no) &&
                string.IsNullOrWhiteSpace(transaction_id) &&
                string.IsNullOrWhiteSpace(sub_order_no) &&
                string.IsNullOrWhiteSpace(sub_order_id))
            {
                throw new ArgumentException(
                    "out_trade_no、transaction_id、sub_order_no 和 sub_order_id 至少填写一个。");
            }
        }

        /// <inheritdoc />
        protected override void SetParameters(RequestHandler handler)
        {
            base.SetParameters(handler);
            handler.SetParameterWhenNotNull("sign_type", sign_type);
            handler.SetParameterWhenNotNull("out_trade_no", out_trade_no);
            handler.SetParameterWhenNotNull("transaction_id", transaction_id);
            handler.SetParameterWhenNotNull("sub_order_no", sub_order_no);
            handler.SetParameterWhenNotNull("sub_order_id", sub_order_id);
        }
    }

    /// <summary>
    /// 海关重新申报请求。

View on GitHub (pinned to be573f6f94)