#Asad-webhooks

1 messages ยท Page 1 of 1 (latest)

pastel blade
#

๐Ÿ‘‹ happy to help

green scroll
#

really appreciated

pastel blade
#

could you please share your webhook endpoint code?

green scroll
#

sure

#

@csrf_exempt
def stripe_webhook(request):
stripe.api_key = settings.STRIPE_SECRET_KEY
endpoint_secret = settings.STRIPE_ENDPOINT_SECRET
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
# Invalid payload
return HttpResponse(status=401)
except stripe.error.SignatureVerificationError as e:
# Invalid signature
# JsonResponse(data={"error" :e} )
msg = payload
return HttpResponse(msg, status=404)

# Handle the checkout.session.completed event
if event['type'] == 'checkout.session.completed':
    session = event['data']['object']
    invoice = stripe.PaymentIntent.retrieve(
        session['payment_intent']
    )
    meta_data = session["metadata"]
    membership_pk = meta_data['plan_pk']
    user = meta_data['user_pk']
    membership_obj = Membership.objects.get(pk=membership_pk)
    sub_obj = Subscription.objects.get(user=user)
    sub_obj.expiry_date = date.today() + relativedelta(days=+ membership_obj.membership_duration)
    sub_obj.membership = membership_obj
    sub_obj.save()
    user_obj = get_object_or_404(User, pk=user)
    payment_obj = Payments.objects.create(user=user_obj, subscription=sub_obj,
                                          amount=membership_obj.membership_price, invoice=invoice)

return HttpResponse(status=200)
pastel blade
#

could you please explain what are you trying to achieve when the checkout.session.completed event is fired?

green scroll
#

i am changing the subscription plan of the user

#

and storing the payment history to our database

#

but whenever webhook is called it give 500 on "checkout.session.completed".

pastel blade
#

if it's taking to much time to do then we assume that something wrong went there

green scroll
#

i have confirmed the logics while testing it on local environment and it works perfect. but on live server webhook gives error

pastel blade
#

this is why you should always return a 200 response as fast as you can and handle the "heavy-lifting" code asynchronously

green scroll
#

can you please point out the issue?

#

please

molten perch
#

we can't I'm afraid, the 500 is an error on your side, the first step is to check your server logs to see what might have happened on your side to crash when handling the webhook.

green scroll
#

why is it happening on live server

#

?

molten perch
#

I don't know, what do your logs say the actual error is?

green scroll
#

let me forward those

#

this is the response that i am recieving from event

molten perch
#

that's the response your server sends yes.

#

as the developer of the code running on that server, what do your logs tell you?

green scroll
#

on server side?

molten perch
#

yes

green scroll
#

there is everything perfect

#

nothing crashin on my server side

molten perch
#

your server is returning a generic 500 error. Thats all that HTML page is. Why does it return a 500? Look at the application logs

molten perch
green scroll
#

ok let me check those again

#

that is the response i am recieving from stripe

#

not from my server

molten perch
#

100% wrong sorry

green scroll
#

so?

molten perch
#

that is the response your server sent to Stripe, and we show it to you through your Stripe dashboard so you can see it to help you debug.

green scroll
#

okay

#

let me see if something is lagging from my side

#

thanks for your response

#

hey

#

this is the "2022/08/12 10:43:01 [crit] 787062#787062: *91 SSL_do_handshake() failed (SSL: error:14201044:SSL routines:tls_choose_sigalg:internal error) while SSL handshaking, client: 23.239.9.55, server: 0.0.0.0:443" recursive error that i had on my server

#

is that related to stripe crashing event?

molten perch
#

I don't know, 23.239.9.55 is not a Stripe IP address(https://stripe.com/docs/ips) at least, so whatever your server is trying to handshake with is likely something else.

green scroll
#

this could be a reason for crash of webhook?

molten perch
#

it could be, you'd need to do a bit more digging I think.

green scroll
#

thanks