Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
settheline Avatar asked Nov 18 '14 00:11

settheline


People also ask

How long is Stripe webhook?

Stripe waits an hour after receiving a successful response to the invoice. created event before attempting payment. If we don't receive a successful response within 72 hours, we attempt to finalize and send the invoice.

Do I need Webhooks for Stripe?

Stripe uses webhooks to notify your application when an event happens in your account. Webhooks are particularly useful for asynchronous events like when a customer's bank confirms a payment, a customer disputes a charge, a recurring payment succeeds, or when collecting subscription payments.

Does Stripe retry Webhooks?

In test mode, Stripe retries three times over a few hours. Webhooks can be manually retried after this time in the Dashboard, and you can also query for missed events to reconcile the data over any time period.


2 Answers

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()
like image 194
nerdburn Avatar answered Oct 10 '22 04:10

nerdburn


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.

like image 42
Larry Ullman Avatar answered Oct 10 '22 05:10

Larry Ullman