#2Clutch-webhooks

1 messages · Page 1 of 1 (latest)

errant merlin
#

Hey! Can you share an Event ID that you were expecting to handle?

proud brook
#

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?

errant merlin
#

So I'm clear, you're forwarding these events with the CLI? Or an external endpoint?

proud brook
#

external endpoint

errant merlin
#

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?

proud brook
#

we_1KREmPJF8oW0qG5s2dCHKAtF

#

i can see a list of events on my dashboard

errant merlin
#

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

proud brook
#

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.

errant merlin
#

What is your 'use case'? What you trying to do in the webhook handler?

proud brook
#

i'm using checkout to handle subscriptions

#

this is for when people initially sign up.

errant merlin
#

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?

proud brook
#

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?

errant merlin
#

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?

proud brook
#

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?

errant merlin
#

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 an invoice.payment_succeeded event 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?

proud brook
#

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?

errant merlin
#

Your issue is actually related to the configuration of the webhook

proud brook
#

i don't understand. do you mind expanding?

errant merlin
#

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

Learn how to use webhooks with Connect to be notified of Stripe activity.

#

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

proud brook
errant merlin
#

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

proud brook
#

so i shouldn't check this box?

errant merlin
#

I would say no, everything I can see doesn't seem to suggest you're event using Connect

proud brook
#

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

errant merlin
#

Are you routing payments to other parties/businesses?

proud brook
#

nope. not for this piece

errant merlin
#

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

proud brook
#

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

errant merlin
proud brook
#

ok. looks like things are working are they should

#

however, the return here, shouldn't be null

#

how do i update this?

errant merlin
#

Well I guess that's the return value of update_membership_info(price_id, stripe_object)

#

You probably want to return some JSON

proud brook
#

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.

errant merlin
#

It's null because that's what your webhook handler is returning