#2Clutch-webhooks
1 messages · Page 1 of 1 (latest)
evt_1KRFB0JF8oW0qG5siah549LA
for context, here's what i'm doing once I receive an event from the webhook:
async def webhook_listener(request, response):
_event = None
payload = await request.body()
stripe_object = StripeSubscription
try:
_event = json.loads(payload)
except Exception as e:
print('⚠️ Webhook error while parsing basic request.' + str(e))
return await response(success=False)
if endpoint_secret:
sig_header = request.headers.get('stripe-signature')
try:
_event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except stripe.error.SignatureVerificationError as e:
print('⚠️ Webhook signature verification failed.' + str(e))
return await response(success=False)
# Handle the event
if _event:
stripe_session = _event['data']['object']
stripe_object.customer_id = stripe_session['customer']
if _event['type'] == 'checkout.session.completed':
subscription = stripe.Subscription.retrieve(stripe_session['subscription'])
price_id = subscription['items']['data'][0]['price']['id']
await update_membership_info(price_id, stripe_object)
elif _event['type'] == 'customer.subscription.updated':
price_id = stripe_session['items']['data'][0]['price']['id']
await update_membership_info(price_id, stripe_object)
elif _event['type'] == 'customer.subscription.deleted':
stripe_object.subscription_plan = 'Essentials'
return stripe_object
async def update_membership_info(price_id, stripe_object):
if price_id == enhanced_price_id:
stripe_object.subscription_plan = 'Enhanced'
elif price_id == premier_price_id:
stripe_object.subscription_plan = 'Premier'
return stripe_object
what's the most efficient or viable way to access the object i'm trying to return?
So I'm clear, you're forwarding these events with the CLI? Or an external endpoint?
external endpoint
Can you share the Stripe webhook ID? we_xxx
I can't see that event being sent to any endpoints other than the CLI. Are you sure it's configured to listen for invoice.payment_succeeded events?
Yeah, invoice.payment_succeeded events aren't enabled for that webhook
The events will always fire, regardless of whether you're listening for them
You need to re-configure that webhook to add invoice.payment_succeeded as an event type it should listen for
right now, i'm listening to checkout.session.completed
is invoice.payment_succeeded the only event type i should be listening to given my use case?
the event shown in the screenshot above reflect what i will be getting from my users.
What is your 'use case'? What you trying to do in the webhook handler?
i'm using checkout to handle subscriptions
this is for when people initially sign up.
Ok, so looking at the code you shared you don't even handle invoice.payment_suceeded events? Is there a different event (checkout.session.completed) that you aren't receiving?
well no. since i'm using checkout for sign ups, it made sense to listen to successful checkout events?
or at least that's what i thought.
then once they sign up, i can leverage the customer portal to enable them to update their info, while listening to customer.subscription.update and customer.subscription.delete events.
if i listen to invoice.payment_suceeded then i shouldn't have to any other event?
I'm confused. You said you weren't receiving events, then shared an invoice.payment_succeeded event which the webhook you shared wasn't listening for
So is there an issue receiving checkout.session.completed events?
let take a step back for a moment. can you tell me what's confusing with my explanation of what I'm trying to do?
Well your initial question states:
but it's not processing any events
I asked you to share an Event ID you've expected to be processed, and it was aninvoice.payment_succeededevent which the webhook you have wasn't listening for
Can you share a checkout.session.completed event that isn't being handled/processed by your webhook?
evt_1KRF6KJF8oW0qG5sMYhzk98C
let me rephrase my question then.
i'm using checkout to handle subscription sign-ups
what event should i be listening to process those first-time signups? which are completed using checkout
furthermore, when dealing with the customer portal, what events should i be listening to process upgrades, downgrades, and cancellations?
Your issue is actually related to the configuration of the webhook
i don't understand. do you mind expanding?
Sure, one moment
It's configured as a Connect webhook:
Connect webhooks are for activity on any connected account. This includes the important account.updated event for any connected account and direct charges.
https://stripe.com/docs/connect/webhooks
So it'll listen for any events on connected accounts. That event you just shared (evt_1KRF6KJF8oW0qG5sMYhzk98C) was on the platform account (acct_1IXvZ2JF8oW0qG5s) because the Checkout Session was created on the platform account
Doesn't seem like you're a Connect platform?
So perhaps you just need to re-configure the webhook to be a non-Connect webhook
what do you mean by because the Checkout Session was created on the platform account?
It was created using the secret key of acct_1IXvZ2JF8oW0qG5s, which I think is intentional
The issue is you've just mistakenly setup your webhook for Connect, so it's not receiving events on acct_1IXvZ2JF8oW0qG5s
so i shouldn't check this box?
I would say no, everything I can see doesn't seem to suggest you're event using Connect
also, i'm not sure i understand the difference between connect and direct.
because it seems we're on the same on what i want to do, but not the how
Are you routing payments to other parties/businesses?
nope. not for this piece
Then you don't need to worry about it. Disable the Connect option on the webhook and it'll start picking up the checkout.session.completed events as expected
ok.
doesn't look like i can disable it so i'll just create a new webhook.
give me a moment to set this up
Yeah, sounds right. Think its immutable
ok. looks like things are working are they should
however, the return here, shouldn't be null
how do i update this?
Well I guess that's the return value of update_membership_info(price_id, stripe_object)
You probably want to return some JSON
this shouldn't be null though.
can you pls leave this open for a couple of hours? i'll try to troubleshooting and revert if i have a particular question.
It's null because that's what your webhook handler is returning