pythondjangostripe-payments

Why does Stripe's webhook send the same event twice at about the same time?


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:

  1. Why are two of the same events sent at the same time?
  2. Is it possible to send an event only once? Or is it possible to send a second event a little later?
  3. How can I solve this problem?

If anyone knows about this issue, please help me. thank you.


Solution

  • You should write to Stripe Support, since only them can look into the event (evt_1LP4n0BB5Q5Dr2x7QDEfWFak) here.

    My assumption is that you may mixed up and double triggered it by:

    1. Made a payment
    2. Manually calling the resend command (Why would you need to use resend in the first place?)

    If you only make a payment but have 2 exactly same events came, then it's best to talk to Stripe Support as mentioned