Reputation: 4110
What happens if a customer re-subscribes to a plan that has a trial period?
To be more precise:
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
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
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