'Stripe webhook for when trial ends

I'm aware of the customer.subscriptions.trial_will_end event. It fires 3 days before a trial ends.

I couldn't find an event that actually fires when the trial is over and the customer hasn't paid. This would be useful to do something simple like this to turn off features:

customer.update_attributes(active_account: false)

Without a webhook like that, I'm looking at scheduling some tasks to check unconfirmed customers periodically and turn off features accordingly. The webhook seems cleaner though and less prone to errors on my side. Is there an event/webhook in line with these goals? FYI, customers don't have to put in a card when they start the trial - so autobilling is not an option.



Solution 1:[1]

To add to Larry's answer and share how I got around the lack of a trial ended webhook, here's what I did.

In invoice.payment_failed webhook, I checked:

  • Is this the first invoice since the subscription start?
  • Does the customer have any cards saved?

If these checks fail, then I assume the trial has just ended with no billing details entered, and I cancel the subscription.

Example in Python:

# get account from my database
account = models.account.get_one({ 'stripe.id': invoice['customer'] })

# get stripe customer and subscription
customer = stripe.Customer.retrieve(account['stripe']['id'])
subscription = customer.subscriptions.retrieve(account['stripe']['subscription']['id'])

# perform checks
cards_count = customer['sources']['total_count']
now = datetime.fromtimestamp(int(invoice['date']))
trial_start = datetime.fromtimestamp(int(subscription['start']))
days_since = (now - trial_start).days

# cancel if 14 days since subscription start and no billing details added
if days_since == 14 and cards_count < 1:
  subscription.delete()

Solution 2:[2]

Just add 3 days to the free trial period and use the customer.subscriptions.trial_will_end event and update the subscription with 'trial_end=now'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nerdburn
Solution 2 zedkira