Ben
Ben

Reputation: 4110

How to handle re-subscription to a Stripe plan with a trial period?

What happens if a customer re-subscribes to a plan that has a trial period?

To be more precise:

  1. A customer subscribes to a plan with a 30 day trial period.
  2. When this trial period ends, the customer decides to cancel the subscription.
  3. The customer later re-subscribes to the plan.

Will they have access to the trial days again?

How am I able to determine if the user has already consumed the trial period so I can process their re-subscription without a trial period?

Upvotes: 6

Views: 1653

Answers (2)

Kent Robin
Kent Robin

Reputation: 2596

Since Plan has been replaced by Price it's still possible to get multiple free trials if the price changes, so what I did was to put a metaData on the Subscription with my local id of the product that is subscribed to and then iterate over the cancelled subscriptions for the customer in question and check if there's an item matching the given id like:

type IsEligableForTrialPeriodType = {
  productId: string;
  stripeCustomerId: string;
};

export const isEligableForTrialPeriod = async ({
  productId,
  stripeCustomerId,
}: IsEligableForTrialPeriodType): Promise<boolean> => {
  for await (const subscription of StripeApi.subscriptions.list({
    status: "canceled",
    customer: stripeCustomerId,
    limit: 100,
  })) {
    if (subscription.metadata.productId === productId) return false;
  }
  return true;
};

This is not optimal if a customer has a lot of cancelled subscriptions, but in our case at least this will probably for 99% of the cases be less than 10 cancelled subscriptions per customer.

Upvotes: 0

Ben
Ben

Reputation: 4110

My solution:

I check if the customer has a cancelled subscription for this plan. If it's the case, I create a subscription with trial_end to 'now':

if len(stripe.Subscription.list(status='canceled', customer=stripe_customer_id, plan=plan_id)['data']) > 0:
    stripe.Subscription.create(
        customer=stripe_customer_id,
        plan=plan_id,
        trial_end='now')     
else:                
    stripe.Subscription.create(
        customer=stripe_customer_id,
        plan=plan_id)  

Upvotes: 5

Related Questions