Stripe webhook setup - Step-by-step

I’ve finally fixed this, I’m not sure if I’ve complicated it but it works! I am now able to show the following info;

  1. When a subscription is created I can say “You are on the Pro plan. Your next payment of £8.99 will be taken on xx.xx.xxxx”.

  2. When a subscription is cancelled and due to expire I can say “Your plan will be cancelled, but is still available until the end of your billing period on xx.xx.xxxx”.

  3. When a user renews their subscription immediately after un-subscribing I can say “You just re-subscribed to the Pro plan. Your next payment of £8.99 will be taken on xx.xx.xxxx”.

Stripe Events

The default Stripe ‘Status’ event tells me if a user is currently on a paid plan. But, if they cancel a plan their Status will remain ‘active’ until then end of the current period. So, I needed to know the instant a user cancels their plan, this is done by listening to the Event cancel_at_period_endand updating my database.

I created a field in my database called ‘just_cancelled’ which is either ‘yes’ or ‘no’, depending on if cancel_at_period_end is yes or no.

To be clear, the instant a user cancels their plan, their ‘just_cancelled’ field in my DB becomes ‘yes’. And, the instant a user subscribes their ‘just_cancelled’ field in my DB becomes ‘no’.

Backend

The backend has two steps when for the Event “subscription_updated”. The first updates my ‘just_cancelled’ field with a ‘yes’ when the cancel_at_period_end is ‘yes’. This means the user has just cancelled their subscription and they are still ‘active’ until the end of the current period.

The other step marks the same field with a ‘no’ when thecancel_at_period_end is 'no. This means the user has an active subscription.

1. When a subscription is created

When cancel_at_period_endis empty AND their status is ‘active’ I know that they have literally just clicked subscribe for the first time.

1

2. When a subscription is cancelled and due to expire

When cancel_at_period_endis ‘yes’ AND their status is ‘active’ I know their subscription is cancelled and due to expire. Again, thanks to @adamhholmes for this tip.

2

3. When a user renews their subscription immediately after unsubscribing

I know that when an ‘active’ user cancels their subscription, my DB shows;
Status = Active
Just_cancelled = Yes

So, when the same user renews their subscription immediately, my DB shows;
Status = Active
Just_cancelled = No

So I can perform the following search to find when a user re-subscribes immediately after unsubscribing;

3

1 Like