如何更新订阅产品与价格?后端修改已有订阅方案失败求助
How to Update a Stripe Subscription's Product/Price Correctly
Ah, I see the issue here! Using Metadata to change the product or price of a subscription won’t work—metadata is only for storing custom, arbitrary data alongside the subscription, not modifying its core billing attributes.
To actually switch the subscription to a new product/price, you need to update the subscription’s items instead. Here’s how to adjust your code:
var options = new SubscriptionUpdateOptions { ProrationBehavior = "create_prorations", Items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions { Id = subscription.Items.Data[0].Id, // Grab the existing subscription item ID Price = price.Id // Set your new price ID here } } }; var updateResult = subscriptionService.Update(subscription.Id, options);
Key Details to Keep in Mind:
- Subscription Items: Every subscription is made up of one or more items that reference a specific price. To change the product/price, you must modify this item (or replace it if necessary).
- Target the Right Item: Make sure you’re using the correct ID from the existing subscription’s
Items.Datacollection. If there are multiple items, adjust the index or loop through them as needed. - Price ID Directly: Using the
Priceproperty tells Stripe to swap the subscription to the new price, which automatically links to the associated product—no need to set a product ID separately. - Proration Behavior: Your existing
create_prorationssetting will still apply here, so prorations will be generated as expected.
After running this code, the subscription should reflect the new product and price right away (or according to your proration rules). You can confirm the changes by checking the updateResult object or verifying in your Stripe Dashboard.
内容的提问来源于stack exchange,提问作者user17724634
相关产品推荐
相关产品推荐

