diff --git a/src/BCards.Web/Controllers/StripeWebhookController.cs b/src/BCards.Web/Controllers/StripeWebhookController.cs index 9d8443e..72f7736 100644 --- a/src/BCards.Web/Controllers/StripeWebhookController.cs +++ b/src/BCards.Web/Controllers/StripeWebhookController.cs @@ -62,7 +62,7 @@ public class StripeWebhookController : ControllerBase throwOnApiVersionMismatch: false ); - _logger.LogInformation($"Processing Stripe webhook: {stripeEvent.Type}"); + _logger.LogInformation($"[DEBUG] Processing Stripe webhook: {stripeEvent.Type}"); switch (stripeEvent.Type) { @@ -275,14 +275,19 @@ public class StripeWebhookController : ControllerBase { try { + _logger.LogInformation($"[DEBUG] HandleSubscriptionCreated started"); + if (stripeEvent.Data.Object is Subscription stripeSubscription) { - _logger.LogInformation($"Subscription created: {stripeSubscription.Id} for customer: {stripeSubscription.CustomerId}"); + _logger.LogInformation($"[DEBUG] Subscription created: {stripeSubscription.Id} for customer: {stripeSubscription.CustomerId}"); // Get subscription record from our database + _logger.LogInformation($"[DEBUG] Looking for existing subscription with ID: {stripeSubscription.Id}"); var subscription = await _subscriptionRepository.GetByStripeSubscriptionIdAsync(stripeSubscription.Id); + if (subscription != null) { + _logger.LogInformation($"[DEBUG] Found existing subscription: {subscription.Id}"); var service = new SubscriptionItemService(); var subItem = service.Get(stripeSubscription.Items.Data[0].Id); @@ -316,7 +321,8 @@ public class StripeWebhookController : ControllerBase } else { - _logger.LogWarning($"Subscription not found in database: {stripeSubscription.Id}"); + _logger.LogInformation($"[DEBUG] Subscription not found in database: {stripeSubscription.Id}"); + _logger.LogInformation($"[DEBUG] Calling HandleSubscriptionCreatedForNewSubscription"); // Try to find user by Stripe Customer ID and create/update subscription await HandleSubscriptionCreatedForNewSubscription(stripeSubscription); @@ -389,15 +395,20 @@ public class StripeWebhookController : ControllerBase { try { + _logger.LogInformation($"[DEBUG] HandleSubscriptionCreatedForNewSubscription started for customer: {stripeSubscription.CustomerId}"); + // Find user by Stripe Customer ID using a MongoDB query // Since IUserRepository doesn't have GetByStripeCustomerIdAsync, we'll use MongoDB directly + _logger.LogInformation($"[DEBUG] Getting MongoDB database"); var mongoDatabase = HttpContext.RequestServices.GetRequiredService(); var usersCollection = mongoDatabase.GetCollection("users"); + _logger.LogInformation($"[DEBUG] Searching for user with StripeCustomerId: {stripeSubscription.CustomerId}"); var user = await usersCollection.Find(u => u.StripeCustomerId == stripeSubscription.CustomerId).FirstOrDefaultAsync(); if (user != null) { + _logger.LogInformation($"[DEBUG] Found user: {user.Id} ({user.Email})"); _logger.LogInformation($"Found user {user.Id} for customer {stripeSubscription.CustomerId}"); var service = new SubscriptionItemService(); @@ -440,7 +451,15 @@ public class StripeWebhookController : ControllerBase } else { - _logger.LogError($"User not found for Stripe customer ID: {stripeSubscription.CustomerId}"); + _logger.LogError($"[DEBUG] User not found for Stripe customer ID: {stripeSubscription.CustomerId}"); + _logger.LogInformation($"[DEBUG] Will try to list some users to debug"); + + // Debug: list some users to see what we have + var allUsers = await usersCollection.Find(_ => true).Limit(5).ToListAsync(); + foreach (var u in allUsers) + { + _logger.LogInformation($"[DEBUG] User in DB: {u.Id} - {u.Email} - StripeCustomerId: '{u.StripeCustomerId}'"); + } } } catch (Exception ex)