#2Clutch-webhook

1 messages · Page 1 of 1 (latest)

supple belfry
#

hi! no, you can't assume that

#

one way to handle it is if you receive an event that references a cus_xx you don't recognise yet, you would kick off the same process you use for a customer.created event by retrieving the referenced customer from the API and passing it to that code instead

grim forge
#

making an api call to stripe while processing a webhook event usually throws a 500 error.

#

i asked about that in a previous inquiry, and was told not to do that.

grim forge
#

i'm not sure.

supple belfry
#

do you have an example of a request that returned a 500?

#

req_xxx

#

I see we asked for one last time but I'm not sure you shared one

grim forge
supple belfry
#

but like you should 100% be able to call the API from a webhook endpoint, that's a core flow

grim forge
#

i was doing this:

subscription = stripe.Subscription.list(customer=customer_id)
supple belfry
#

yep

#

do you have a request ID req_xxx?

#

as my colleague said it might be a timeout(listing subscriptions for a customer that has a lot of them can take a lot of processing work and take too long to process internally so we timeout with a 500 after 30 seconds) but I'd expect that to be rare

grim forge
#

i see.

#

a new subscription was created every time i ran my script so i can understand how that became an issue.

#

let me bring that back, and clear out the current subscription and see if it makes a difference.

supple belfry
#

yeah, I can't imagine most real customers having so many subscriptions that a single list call should fail

#

would love to look at a specific failure to see what exactly the problem is

grim forge
#

ok so here's what i came up with:

            if _event['type'] == 'customer.subscription.created' or _event['type'] == 'customer.subscription.updated':

                stripe_object.customer_id = str(stripe_session['customer'])
                price_id = stripe_session['items']['data'][0]['price']['id']

                record = await get_user_from_customer_id(stripe_object.customer_id)

                if record:
                    # plan, subscription_id
                    stripe_object.subscription_plan, _subscription_id = await fetch_subscription_metadata(price_id)

                    _auth0_sub = await get_auth0_sub_from_email(record.email)

                    _update_auth0_request = await update_subscription_data(_auth0_sub, stripe_object.dict())
                    _update_subscription_id_request = await update_subscription_id(_subscription_id, stripe_object.customer_id)
                else:
                    subscription = stripe.Subscription.list(customer=stripe_object.customer_id)
                    price_id = subscription['data'][0]['items']['data'][0]['price']['id']

                    stripe_object.subscription_plan, _subscription_id = await fetch_subscription_metadata(price_id)

                    _auth0_sub = await get_auth0_sub_from_email(record.email)

                    _update_auth0_request = await update_subscription_data(_auth0_sub, stripe_object.dict())
                    _update_subscription_id_request = await update_subscription_id(_subscription_id, stripe_object.customer_id)
#

adding it here just for reference.

#

i cleaned out the current subscription. my code is going through the pipeline, and i'll test soon and let you know if i run into any issues.

#

actually, i made a mistake in my else clause.

#

what's the best way to get the email associated with a given customer?

supple belfry
#

customer.email

grim forge
#

i'm looking at get_customer() but there's no email field.

grim forge
#

my mistake

grim forge
#

ok so i just pushed and everything works fine.

#

this chat can be closed now.

azure gulch
#

Nice to hear! Glad you could figure that out

#

Have a nice rest of your day!