#fobor10362-checkout
1 messages · Page 1 of 1 (latest)
I need something like if event.type == 'checkout.session.completed' and price_id = settings.MONTHLY_PRICE_ID or if event.type == 'checkout.session.completed' and price_id = settings.YEARLY_PRICE_ID
For cancels I was able to do something like this: event.type == 'customer.subscription.deleted' and event.data.object['items'].data[0].price.product == settings.STRIPE_MEMBERSHIP_PRODUCT_ID
hi! once you have the ID of the session (event.data.object.id) then you need to make a GET request to https://stripe.com/docs/api/checkout/sessions/retrieve and use the 'expand' feature (https://stripe.com/docs/expand/) to expand the line_items field. The Price is part of the line items(same place where you passed it as a parameter)
hmm ok let me see if I can figure it out
Would this be the session id? "id": "cs_test_a16UAFHb1t6eeuXfZuhgB1P2kZxLnhlrlLvnETKPQWsxbob1wra60HcieJ"
yes
ok so in the webhook itself I would do stripe.checkout.Session.retrieve(
event.data.object.id,
)
with expand=['customer'], inside of it too?
stripe.checkout.Session.retrieve(event.data.object.id, expand=['customer'],)
yep!
well, expand=["line_items", "customer"] I would say, since you want the line items since that's where the Price ID is.
ok let me try
ok now I see it that worked
So now to get the price ID from that would be event.data.object['items'].data[0].price.id ?
I think so
or maybe just event.data.object['items'].data[0].price, not sure if the Price is expanded or not, but it's easy enough for you to test
I will show you the output
checkout_session.line_items.data[0].price.id
I think this is it
checkout_session is the retrieve line from above
cool!
I have another question
I want to have 2 webhooks in 1 python file
I suppose I need to have 2 separate endpoint_secret variables
They should probably be named differently?
like endpoint_secret_1 and endpoint_secret_2
you should not do it that way, use different URLs and different scripts for each.
Then in the event have event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret_1
)
Yes each function is its own URL
I have def stripe_cancel_webhook(request) as 1 webhook function and def stripe_membership_payment_webhook(request) as another webhook function. Both functions have their own URLs
cool, then you configure it.
Like you set it up as
-on Stripe : example.com/webhook-1
-and in your code, you configure that the code running at webhook-1 uses webhook secret X
-on Stripe : example.com/webhook-2
-and in your code, you configure that the code running at webhook-2 uses webhook secret Y
Right exactly that is what I was thinking
makes sense then
Ok cool
thank you i think those are all my questions for now
Oh wait 1 more question. Why is it that when I navigate to my webhook URL I always get an error regarding request.META['HTTP_STRIPE_SIGNATURE']
But when I complete the checkout session there are no errors
Hi 👋 Jumping in as @sweet geode needs to step away.
ok
If you're referring to the actual endpoint that you're hosting, then we won't know as that is all your code. However, it sounds like that route is expecting to find the Stripe header, but isn't because that's not added when you just browse to the location.
Yea it only seems to be added when the webhook completes or the checkout session gets created
It's a header that is included by Stripe when we send events to your endpoint.
Should I set the header to None to prevent the error or put it in a try except block ?
If you want to still be able to view that page, and the framework you're using allows it, then you can host a GET and POST route at the same location to have separate flows based on the type of request.
ok
I am wondering how you could tell based on the type of request it is
Would the payload body tell us that?
I am basically using this code here
We can help you understand what the Stripe requests will look like, but we won't have any insight into what the rest of your site traffic looks like as that depends on how you structure your site.
The site is not live and I am using ngrok locally
someone would have to type in the webhook URL which they probably would not know
in order to expose this error
It's up to you how you'd like to handle the returned response if people hit that page directly. For example, you could look for the Stripe header and return a 404 if it isn't present so it looks like the page doesn't exist, but I'm not sure if that would be a flow that you desire.
ok i will try doing something like that the thanks
Any time!