Webhook strip for when the trial ends

I know the event customer.subscriptions.trial_will_end. It works 3 days before the end of the trial version.

I could not find an event that really fires when the trial version is completed and the client did not pay. It would be useful to do something simple to disable functions:

customer.update_attributes(active_account: false)

Without such a web host, I look at scheduling some tasks to periodically check unconfirmed clients and disable features accordingly. Webhook seems cleaner, albeit less error prone on my side. Is there an event / web hockey in line with these goals? FYI, customers don’t need to insert a card when they start the trial version, so auto business is not an option.

+4
source share
2 answers

When the trial period ends, an event customer.subscription.updatedand an event will occur invoice.created. After an hour (or so on) you will either see the event invoice.payment_succeeded, or the event invoice.payment_failed. From this you will find out whether the payment passed or not.

Cheers, Larry

PS I am working on support on Stripe.

+9
source

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

In invoice.payment_failedwebhook, I checked:

  • Is this the first account since the launch of the subscription?
  • Does the client retain any cards?

If these checks fail, I assume that the trial has just ended without entering billing information, and I canceled the subscription.

An 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()
Hide result
+4

All Articles