You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Stripe API与.NET创建订阅:如何将元数据存入首次付款?

解决方案

要将元数据保存到首次订阅付款对应的PaymentIntent对象中,你需要在创建订阅时,通过SubscriptionCreateOptions.PaymentIntentData属性来设置元数据,而非直接给订阅对象设置Metadata。

修改后的代码如下:

var subscriptionOptions = new SubscriptionCreateOptions
{
    Customer = customerID,
    Items = new List<SubscriptionItemOptions>
    {
        new SubscriptionItemOptions
        {
            Price = priceID,
            Quantity = 1
        },
    },
    PaymentBehavior = "default_incomplete",
    // 若需要保留订阅对象的元数据可保留此部分,否则可移除
    // Metadata = new Dictionary<string, string>
    // {
    //     { "id", "123"},
    //     { "name", "david" },
    // },
    // 设置PaymentIntent的元数据
    PaymentIntentData = new PaymentIntentCreateOptions
    {
        Metadata = new Dictionary<string, string>
        {
            { "id", "123"},
            { "name", "david" },
        }
    }
};
subscriptionOptions.AddExpand("latest_invoice.payment_intent");
var subscriptionService = new SubscriptionService();
try
{
    Subscription subscription = await subscriptionService.CreateAsync(subscriptionOptions);

    return new SubscriptionCreateResponse
    {
        SubscriptionId = subscription.Id,
        ClientSecret = subscription.LatestInvoice.PaymentIntent.ClientSecret,
    };
}

说明

  • PaymentIntentData属性用于配置订阅生成的首次付款对应的PaymentIntent参数,其中的Metadata会直接附加到该PaymentIntent对象上。
  • 如果你同时需要订阅对象也保留元数据,可以继续保留订阅本身的Metadata设置,两者互不冲突。

内容的提问来源于stack exchange,提问作者davidasor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:55:58