#leak-webhook-questions

1 messages · Page 1 of 1 (latest)

lean siloBOT
cinder skiff
#

Hello

#

Basically you just want to handle this in your Webhook handler.

#

So first thing you do when you receive these Events is you respond with a 200 to let Stripe know you received them.

#

Then, based on the data in the Event you can check if you already have that customer in your database or whether you need to create a new one.

pure sphinx
cinder skiff
#

Okay. So what error are you seeing exactly?

pure sphinx
#

error:

File "/Users/lauri/Desktop/company_full/django_rest_api/api/views.py", line 480, in post
subscription = Subscription.objects.get(customer_id=customer_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lauri/Desktop/company_full/venv/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lauri/Desktop/company_full/venv/lib/python3.11/site-packages/django/db/models/query.py", line 637, in get
raise self.model.DoesNotExist(
api.models.Subscription.DoesNotExist: Subscription matching query does not exist.
[11/Oct/2023 16:25:02] "POST /api/payment/webhook-test/ HTTP/1.1" 500 132727

cinder skiff
#

Okay that is an issue with your database interaction.

#

That's not a Stripe-specific issue so it isn't really something I can help with

pure sphinx
#

on which event_type should i create a user in my database

#

because then i should be getting this error anymore when the user is already created in the database.

cinder skiff
#

Depends on your flow

#

But usually you would use checkout.session.completed for this

pure sphinx
#

what do you mean with the flow

cinder skiff
#

I mean what API requests you make.

#

Like you could create a Customer object before you redirect to Checkout

#

If you want

#

So you could store them in your database at that point potentially

#

All depends on what you want really

pure sphinx
#

i want to create a customer and store there their customer_id and supscription_id and the price_id

cinder skiff
#

Then I would still recommend doing that upon checkout.session.completed

pure sphinx
#

but why is it calling the customer.subscription.updated before that ?

cinder skiff
#

Because there are updates to the Subscription happening as well, but you should have your code ignore that in these cases.

pure sphinx
#

how can i do that

cinder skiff
#

You look at the Subscription object that is returned in the Event and determine whether it is a renewal of an active Subscription or a new Subscription

#

The best way to figure that out is to test out both things so you know what the Event will look like

pure sphinx
#

so i should look at every event_type if the subscription is new or already in the database, and based on that create a new subscrption

lean siloBOT
pure sphinx
#

do you have some example code only for a finished webhook that updates the database on the events. i am working on the stripe intergration for a 4 days and i am always getting new errors it would help me to get on the right track.

cinder skiff
#

No we don't have example code for this.

#

You need to decide which Events you care about, then have your Webhook endpoint only listen for those Events, then you need to write code to ensure that you are ingesting the Events properly when you want to use them.