使用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
相关产品推荐
相关产品推荐

