How do you preview a proration using Stripe.NET?

半世苍凉 提交于 2021-01-28 03:08:34

问题


I'm following the example provided in the Stripe documentation on Previewing Proration using the Stripe.NET library to try to find the amount that will be charged when a customer upgrades from Plan A to Plan B.

However, when I use the code sample in the documentation, I get an error:

UpcomingInvoiceOptions options = new UpcomingInvoiceOptions()
{
    CustomerId = "cus_XXXXXXXXXXXXX",
    SubscriptionProrationDate = DateTime.UtcNow,
    SubscriptionItems = new List<InvoiceSubscriptionItemOptions>()
    {
        new InvoiceSubscriptionItemOptions()
        {
            Id = "si_XXXXXXXXXXXXXX", // Current Sub Item
            PlanId = "plan_XXXXXXXXXXXX" // New plan
        }
    }
};
InvoiceService service = new InvoiceService();
var result = service.Upcoming(options);

The last line throws a Stripe.StripeException: You cannot update a subscription item without a subscription.


回答1:


Turns out options.SubscriptionId is a required field for this action if you don't call service.Get first.

The following code produces the correct results:

UpcomingInvoiceOptions options = new UpcomingInvoiceOptions()
{
    CustomerId = "cus_XXXXXXXXXXXXX",
    SubscriptionId = "sub_XXXXXXXXXXXX",
    SubscriptionProrationDate = DateTime.UtcNow,
    SubscriptionItems = new List<InvoiceSubscriptionItemOptions>()
    {
        new InvoiceSubscriptionItemOptions()
        {
            Id = "si_XXXXXXXXXXXXXX", // Current Sub Item
            PlanId = "plan_XXXXXXXXXXXX" // New plan
        }
    }
};
InvoiceService service = new InvoiceService();
var result = service.Upcoming(options);


来源:https://stackoverflow.com/questions/55736969/how-do-you-preview-a-proration-using-stripe-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!