From 0634b52af063c932435e5ced3f3b7e3a2de050e8 Mon Sep 17 00:00:00 2001
From: Pelle Koster <pelle.koster@geant.org>
Date: Thu, 27 Mar 2025 15:12:51 +0100
Subject: [PATCH] better exception handling

---
 .../management/commands/processevents.py      | 19 ++++++++++++++-----
 stripe_checkout/stripe_checkout/visit.py      |  8 ++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/stripe_checkout/stripe_checkout/management/commands/processevents.py b/stripe_checkout/stripe_checkout/management/commands/processevents.py
index 9cdc7e0..36fcf16 100644
--- a/stripe_checkout/stripe_checkout/management/commands/processevents.py
+++ b/stripe_checkout/stripe_checkout/management/commands/processevents.py
@@ -16,7 +16,7 @@ VALID_EVENTS = [
     PAYMENT_INTENT_CANCELED,
 ]
 
-RAISE_EXCEPTIONS = True
+RAISE_EXCEPTIONS = False
 
 UNPROCESSED_PAYMENT_EMAIL_TEMPLATE = """\
 A payment was made in Stripe that could not be linked to a Visitor. Please process this
@@ -43,11 +43,11 @@ class Command(BaseCommand):
                     else:
                         msg = self.style.SUCCESS(f"{prefix} success!")
                 except Exception:
-                    msg = self.style.ERROR(f"{prefix} error!")
+                    self.stdout.write(self.style.ERROR(f"{prefix} error!"))
                     if RAISE_EXCEPTIONS:
                         raise
                     traceback.print_exc()
-                finally:
+                else:
                     event.handled = True
                     event.save()
                     self.stdout.write(msg)
@@ -77,7 +77,11 @@ class Command(BaseCommand):
         if order is None:
             return self._notify_unprocessed_payment(payment_intent)
         api = VisitorAPI()
-        visitor = api.get_visitor(order.visitor_id)
+        visitor = api.get_visitor_or_none(order.visitor_id)
+        if visitor is None:
+            self.stdout.write(self.style.ERROR("Visitor not found!"))
+            return self._notify_unprocessed_payment(payment_intent)
+
         for item in order.items.all():
             if item.kind == ItemKind.REGISTRATION_TYPE:
                 visitor.paid = True
@@ -94,7 +98,12 @@ class Command(BaseCommand):
         if order is None:
             return False
         api = VisitorAPI()
-        visitor = api.get_visitor(order.visitor_id)
+        visitor = api.get_visitor_or_none(order.visitor_id)
+        if visitor is None:
+            self.stdout.write(
+                self.style.ERROR("Visitor not found! Not processing cancellation")
+            )
+            return
         visitor.canceled = True
         api.update_visitor(visitor)
         order.canceled = True
diff --git a/stripe_checkout/stripe_checkout/visit.py b/stripe_checkout/stripe_checkout/visit.py
index fb0df50..2751cc4 100644
--- a/stripe_checkout/stripe_checkout/visit.py
+++ b/stripe_checkout/stripe_checkout/visit.py
@@ -76,6 +76,14 @@ class VisitorAPI:
             raise Http404()
         return Visitor.from_api(result)
 
+    def get_visitor_or_none(
+        self, visitor_id: str, allow_deleted=False
+    ) -> Optional[Visitor]:
+        try:
+            return self.get_visitor(visitor_id, allow_deleted)
+        except Http404:
+            return None
+
     def update_visitor(
         self, visitor: Union[str, Visitor], payload: Optional[dict] = None
     ):
-- 
GitLab