Search code examples
c#asp.net-core.net-corestripe-payments

How to predefine subscription prices and trial period?


I am creating subscriptions for each new customer with an initial 1-month trial.

// Create subscription
SubscriptionCreateOptions options = new()
{
    Customer = customerId,
    Items = [],
    TrialEnd = DateTime.UtcNow.AddMonths(1)
};

// Add subscription items
foreach (string price in Settings.ParsePriceIds())
    options.Items.Add(new() { Price = price });

// Create subscription
var service = new SubscriptionService();
await service.CreateAsync(options);

The documentation for TrailEnd states:

If set, trial_end will override the default trial period of the plan the customer is being subscribed to.

My question is: How can I specify a plan that specifies a trial period, as mentioned in the documentation, and then reference that plan when creating a subscription?

In the code above, ParsePriceId() parses any number of price IDs from the configuration file and adds them to the subscription. But if I could define a plan within Stripe that specifies all pricing and any trials, that seems like it would be easier.


Solution

  • Specifying a trial on a Plan is a legacy feature and is not recommended. It's mentioned in the documentation for people using a legacy approach, but you should not build something new that sets a trial period on a Plan.

    Indeed, Plans themselves are legacy and have been replaced by Prices, as mentioned in Stripe's documentation:

    You can now model subscriptions more flexibly using the Prices API. It replaces the Plans API and is backwards compatible to simplify your migration.

    That said, it is still technically possible to create a Plan and specify trial_period_days, but again, this is not recommended.