Skip to content

Commit

Permalink
Shore up check for already received webhook (#329)
Browse files Browse the repository at this point in the history
* Shore up check for already recieved webhook

* Again, with feeling
  • Loading branch information
rechner committed Aug 27, 2024
1 parent f8b6d70 commit d9f61ef
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions registration/views/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def square_webhook(request):
event_id = request_body["event_id"]
event_type = request_body.get("type")

# Check to see if webhook was already stored:
existing = PaymentWebhookNotification.objects.filter(event_id=event_id)
if existing.count() > 0:
return common.abort(409, f"Conflict: event_id {event_id} already_exists")

# Store the verified event notification
notification = PaymentWebhookNotification(
event_id=event_id,
Expand All @@ -50,9 +55,13 @@ def square_webhook(request):
headers=dict(request.headers),
)
try:
process_webhook(notification)
except IntegrityError:
return common.abort(409, f"Conflict: event_id {event_id} already exists")
notification.save()
except PaymentWebhookNotification.IntegrityError as e:
logger.error("Conflict: event_id already exists:")
logger.error(e)
return common.abort(409, str(e))

process_webhook(notification)

return common.success(200)

Expand Down

0 comments on commit d9f61ef

Please sign in to comment.