JeffreySu/WeiXinMPSDK · error · ArgumentException

out_trade_no 和 transaction_id 至少填写一个。

Error message

out_trade_no 和 transaction_id 至少填写一个。

What it means

This Validate overload (for a customs request type that also requires mch_customs_no) enforces that either out_trade_no or transaction_id is provided — sub-order identifiers alone are not accepted for this API. Additionally Require(mch_customs_no, ...) must pass. Otherwise ArgumentException is thrown.

Solutions

  1. Set out_trade_no to your merchant order number before calling the API.
  2. Or set transaction_id to the WeChat transaction id returned by the payment.
  3. Also confirm mch_customs_no is set, since this Validate requires it first.

Example fix

// before
var data = new ...RequestData(key, appid, mch_id, mch_customs_no, null, null, subOrderNo, null);
// after
var data = new ...RequestData(key, appid, mch_id, mch_customs_no, outTradeNo, null, subOrderNo, null);
Defensive patterns

Strategy: validation

Validate before calling

// C#
if (string.IsNullOrWhiteSpace(outTradeNo) && string.IsNullOrWhiteSpace(transactionId))
    throw new InvalidOperationException("This customs API requires out_trade_no or transaction_id (sub-order ids are not accepted)");

Try / catch

try { await api.CallAsync(data); }
catch (ArgumentException ex) { log.LogWarning("{Msg}", ex.Message); }

Prevention

When it happens

Trigger: Calling the corresponding customs API (e.g. declare/order query variant) with only sub_order_no or sub_order_id set, or with both out_trade_no and transaction_id left empty.

Common situations: Developers confuse this endpoint's stricter requirement (only out_trade_no/transaction_id) with the looser variant that also accepts sub_order_no/sub_order_id, and pass split-order identifiers only.

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

Appendix: source

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

        /// <summary>微信支付订单号,与 out_trade_no 至少填写一个。</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);
            Require(mch_customs_no, "mch_customs_no");
            if (string.IsNullOrWhiteSpace(out_trade_no) &&
                string.IsNullOrWhiteSpace(transaction_id))
            {
                throw new ArgumentException(
                    "out_trade_no 和 transaction_id 至少填写一个。");
            }
        }

        /// <inheritdoc />
        protected override void SetParameters(RequestHandler handler)
        {
            base.SetParameters(handler);
            handler.SetParameterWhenNotNull("sign_type", sign_type);
            handler.SetParameter("mch_customs_no", mch_customs_no);
            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);
        }
    }
}

View on GitHub (pinned to be573f6f94)