#Kevin Duvignau

1 messages · Page 1 of 1 (latest)

lost falconBOT
golden cliff
#

Which ones you listen to depends on your use case and requirements.

#

You can listen to some or all of the associated Events; whatever works best for your situation.

dark timber
#

Hi Rubeus, thanks for you quick answer!

If I listen to customer.subscription.created, is there a a way to know if the subscription has been created from a checkout session?

golden cliff
#

I don't think so, at least not from the Event itself.

bleak valve
#

Hey Kevin,

I'm not affiliated with the stripe team/product but I do this for our current product:

Here's the events we listen for.

#

In our case, we care mostly about customer.subscription.deleted and checkout.session.completed

The checkout completion means that they actually put in a valid payment method and subscribed.

We're able to tell when they cancel their sub(from the portal) with the subscription.deleted event

dark timber
#

Hello Shib and thank you!

I suppose you persist subscription info in your own database. If that's the case, when do you create the subscription? On checkout.session.completed only?

bleak valve
#

Yes, in our own DB. So here's how we tie the two together.

  • Webhook notifies us of a successful checkout
  • We query our database to find the user who just checked out, we create a user in stripe when they sign up
  • We then query the subscription from stripe await stripe.subscriptions.retrieve(data.object.subscription);
  • We verify the subscription exists and is active and then update our user's subscription status and tier

Super high level looks like this:

// inside a switch

case "checkout.session.completed":

  team = await Team.findOne({
        stripeId: data.object.customer,
      });

  let subscription = await stripe.subscriptions.retrieve(data.object.subscription);

  // If they both exist and match up
  // verify the plan's id: subscription.plan.id

  team.activeSubscription = true;
  team.markModified("activeSubscription");
  team.save();

break;

// rest of cases etc...
lost falconBOT
thick wing
#

Yeah checkout.session.completed would be the event to listen to to associate a specific checkout session to a subscription though you are right that you couldn't guaruntee that that event would come first. At that point it might make sense to pass something to subscription_data.metadata when creating your checkout sessions, that way that data will be on the subscription in the subscription created event if you just want to listen to that

dark timber
#

Thanks Pompey! We could indeed add a flag in metadata!

Thank you for all those details Shib, I appreciate! 😊 Do you handle subscriptions that are created and paid without using checkout? For example, if a sales person creates manually a subscription from the dashboard?
In that case, no checkout.session.completed event will be generated

bleak valve
dark timber
#

I see! In your situation, your implementation looks perfect! I think we'll do the same for subscriptions created using Stripe Checkout!

thick wing
#

@dark timber just checking in, any further questions about which events to listen to here or how to try to tell dashboard vs API subscriptions apart?

dark timber
#

All good thank you so much!