stripe-payments

How to add cycle limit to a Stripe Subscription when creating a subscription using the .NET SDK?


I am trying to utilized the Stripe.NET SDK to create a subscription in Stripe.

I am using the SubscriptionCreateOptions class to create a new subscription. However, I do not see a option to add an end-date to the subscription or adding a cycles limit to auto complete the subscription after X amount of payments.

I came across the SubscriptionScheduleService which allows me to create a schedule and in the schedule phases, I can specify start-date and end-date which seems promising. But, I don't see a way to associate the subscription schedule with the subscription.

Here is how I created the schedule

var subscriptionScheduleOptions = new SubscriptionScheduleCreateOptions
{
    Customer = model.CustomerId,
    StartDate = now,
    Phases =
    [
        new SubscriptionSchedulePhaseOptions
        {
            Items =
            [
                new()
                {
                    Price = model.PriceId,
                    Quantity = 1,
                },
            ],
            StartDate = now,
            EndDate = now.AddMonths(10),
        }
    ]
};
var subscriptionScheduleService = new SubscriptionScheduleService();
var subscriptionSchedule = subscriptionScheduleService.Create(subscriptionScheduleOptions);

Here is how I am creating the subscription

var now = DateTime.UtcNow;

var subscriptionOptions = new SubscriptionCreateOptions
{
   Customer = model.CustomerId,
   Items =
   [
       new()
       {
            Price = model.PriceId,
            Quantity = 1,
       },
   ],
   PaymentBehavior = "allow_incomplete",
   DefaultPaymentMethod = model.PaymentMethodId,
};

await subscriptionService.CreateAsync(subscriptionOptions);

How can I set a billing cycle limit to a stripe subscription using C#?


Solution

  • I think the Subscription Schedules approach would be the best fit here. But instead of specifying an EndDate, you would specify a number of Iterations.

    This idea of canceling the subscription after a set number of billing cycles is essentially what the Use Cases documentation refers to as the Installment Plans use case. So if you wanted your customer to pay a monthly price for 5 months as an example, you would create a SubscriptionSchedule with your monthly price, set the Iterations = 5, and specify the EndBehavior = "cancel".

    That last parameter, EndBehavior determines whether the Subscription just keeps billing until you cancel it or if, after the phases are complete, the Subscription cancels.