Просмотр исходного кода

Fix email sending in NOW Payments webhook handler

This commit addresses an issue where emails were not being sent after
successful NOW Payments orders. The key changes include:

1. Improved error handling around the product inventory updates with
   proper try/except blocks to prevent transaction failures from
   blocking email sending

2. Fixed the product variation ID retrieval in the NOW Payments webhook
   to match the approach used in the Stripe webhook implementation

3. Added better error logging for failed email attempts to help with
   debugging future issues

4. Restructured the email sending code to ensure that TypeErrors during
   order preparation are handled separately from other exceptions
brid 1 год назад
Родитель
Сommit
278d54cd07
1 измененных файлов с 49 добавлено и 16 удалено
  1. 49 16
      app/main.py

+ 49 - 16
app/main.py

@@ -822,8 +822,17 @@ async def stripe_webhook(request: Request, background_tasks: BackgroundTasks):
                             "block_types": settings.block_types,
                         }
 
-                        order = prepare_email_order(order_id, email_settings)
-                        await send_email(email_settings, order, background_tasks)
+                        # It's possible the order was just created and not all data is available yet
+                        # The prepare_email_order function should handle this gracefully
+                        try:
+                            order = prepare_email_order(order_id, email_settings)
+                            await send_email(email_settings, order, background_tasks)
+
+                        except TypeError as e:
+                            # Specifically catch the 'NoneType' object is not iterable error
+                            main_logger.warning(f"stripe-webhook warning: Email preparation issue, possibly due to timing: {e}")
+                            # Consider implementing a retry mechanism here if needed
+                            # For example, add the email task to a queue to try again later
 
                     except Exception as e:
                         main_logger.error(f"stripe-webhook error: Failed to send email: {e}")
@@ -998,22 +1007,46 @@ async def now_webhook(request: Request, background_tasks: BackgroundTasks):
 
                     if product_list:
                         for product_info in product_list:
-                            update_product_inventory(
-                                product_info.filename,
-                                product_info.quantity,
-                                product_info.product_variation_id,
-                                settings,
-                            )
+                            try:
+                                product = read_file(
+                                    settings.git_repo,
+                                    f"products/{product_info.filename}",
+                                    settings.document_match,
+                                    settings.block_types,
+                                )
+            
+                                # Get the variation ID based on size and style
+                                product_variation_id = get_variation_id(product,
+                                                                        product_info.size,
+                                                                        product_info.style)
+                                
+                                update_product_inventory(
+                                    product_info.filename,
+                                    product_info.quantity,
+                                    product_variation_id,
+                                    settings,
+                                )
+
+                            except Exception as e:
+                                main_logger.error(f"now-webhook error: Failed to update inventory: {e}")
+                                # Continue processing other products
 
                     # -- send email
-                    email_settings = {
-                        "git_repo": settings.git_repo,
-                        "document_match": settings.document_match,
-                        "block_types": settings.block_types,
-                    }
-
-                    order = prepare_email_order(order_id, email_settings)
-                    await send_email(email_settings, order, background_tasks)
+                    try:
+                        email_settings = {
+                            "git_repo": settings.git_repo,
+                            "document_match": settings.document_match,
+                            "block_types": settings.block_types,
+                        }
+
+                        try:
+                            order = prepare_email_order(order_id, email_settings)
+                            await send_email(email_settings, order, background_tasks)
+                        except TypeError as e:
+                            main_logger.warning(f"now-webhook warning: Email preparation issue: {e}")
+
+                    except Exception as e:
+                        main_logger.error(f"now-webhook error: Failed to send email: {e}")
 
                 elif event_type == "failed":
                     main_logger.error(