Issue
I am getting the below error while Stripe calling the defined Web hook
No signatures found matching the expected signature for payload
I am following this article: https://stripe.com/docs/billing/subscriptions/checkout#provision-and-monitor
And I have following code:
@csrf_exempt
def saaswebhookview(request):
try:
stripe.api_key = settings.STRIPE_SECRET_KEY
webhook_secret = 'stripe_key'
request_data = request.POST
if webhook_secret:
try:
signature = request.headers.get('stripe-signature')
# signature = request.META['stripe-signature']
event = stripe.Webhook.construct_event(
payload=request.POST, sig_header=signature, secret=webhook_secret)
data = event['data']
except Exception as e:
print(str(e))
return JsonResponse({'status': 'error', 'error': str(e)})
event_type = event['type']
else:
data = request_data['data']
event_type = request_data['type']
data_object = data['object']
if event_type == 'checkout.session.completed':
print(data)
elif event_type == 'invoice.paid':
print(data)
elif event_type == 'invoice.payment_failed':
print(data)
else:
print('Unhandled event type {}'.format(event_type))
return JsonResponse({'status': 'success'}, safe=False)
except Exception as e:
return JsonResponse({'status': 'success', 'error': str(e)}, safe=False)
But strangely this throws me the error don't know why?
Solution
The Stripe library requires the raw body of the webhook request for signature verification to work. It looks like you're supplying request.POST
, which is an altered version of the body.
If you use request.body
instead it should work as expected.
Answered By - Justin Michael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.