settheline
settheline

Reputation: 3383

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.

Upvotes: 18

Views: 9791

Answers (4)

Nuriddin Kudratov
Nuriddin Kudratov

Reputation: 518

I think there is another way that can be handled easly. So invoice.payment_failed should be listened, in all invoice related events, inside event.data.object, there is subscription id or subscription object, you should get subscription id and retrieve subscription then you can get both product id and price id. By price id or by product id you can know current subscription.

Upvotes: -1

zedkira
zedkira

Reputation: 21

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'

Upvotes: 2

nerdburn
nerdburn

Reputation: 712

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()

Upvotes: 16

Larry Ullman
Larry Ullman

Reputation: 2281

When the trial period ends, there will be a customer.subscription.updated event and an invoice.created event. An hour (or so) later, you'll then either see an invoice.payment_succeeded event or an invoice.payment_failed event. From those, you'll know whether the payment went through or not.

Cheers, Larry

PS I work on Support at Stripe.

Upvotes: 44

Related Questions