#Asad-webhooks
1 messages ยท Page 1 of 1 (latest)
really appreciated
could you please share your webhook endpoint code?
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)
could you please explain what are you trying to achieve when the checkout.session.completed event is fired?
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".
if it's taking to much time to do then we assume that something wrong went there
i have confirmed the logics while testing it on local environment and it works perfect. but on live server webhook gives error
this is why you should always return a 200 response as fast as you can and handle the "heavy-lifting" code asynchronously
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.
I don't know, what do your logs say the actual error is?
that's the response your server sends yes.
as the developer of the code running on that server, what do your logs tell you?
on server side?
yes
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
then what is that response you posted just now if it's not a crash?
ok let me check those again
that is the response i am recieving from stripe
not from my server
100% wrong sorry
so?
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.
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?
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.
this could be a reason for crash of webhook?
it could be, you'd need to do a bit more digging I think.
thanks