I'm building a payment system using Stripe. After making a payment with Stripe, we use the 'invoice.paid' event to authorize the user. I've tried it several times and noticed that the webhook sends the same event twice at about the same time.
This reproduced the same problem both when I made a payment in test mode from a web page and when I ran the following command from the terminal.
stripe events resend evt_xxxxxxxxxxxxxxx
However, this is a phenomenon confirmed only in the development environment by executing the following command.
stripe listen --forward-to http://0.0.0.0:8080/webhook/
I haven't tried it in production yet.
I get the same event twice, so I saved the event ID and set it to return 200 status when I received the second event.
def webhook_view (request):
payload = json.loads(request.body)
event_id = payload.get('id')
event_type = payload.get('type','')
logger.info (f'type: {event_type} event_id: {event_id}')
if Event.objects.filter (id = event_id).exists ():
logger.info (f'{event_id} already exists')
return HttpResponse (status = 200)
Event (id = event_id, created_at = timezone.now(), data = json.dumps(payload)).save()
However, the interval between the two requests is so short that the second event passes the check.
[2022-07-24 22:28:53,677] [webhook_view: 60] type: invoice.paid event_id: evt_1LP4n0BB5Q5Dr2x7QDEfWFak
[2022-07-24 22:28:53,682] [webhook_view: 60] type: invoice.paid event_id: evt_1LP4n0BB5Q5Dr2x7QDEfWFak
There are 3 questions I would like to ask:
- Why are two of the same events sent at the same time?
- Is it possible to send an event only once? Or is it possible to send a second event a little later?
- How can I solve this problem?
If anyone knows about this issue, please help me. thank you.