Compare commits

...

2 Commits

Author SHA1 Message Date
Ricardo Carneiro
0f6ae71997 fix: create plan
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 2s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 15m28s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Successful in 1m19s
BCards Deployment Pipeline / Deploy to Staging (x86 - Local) (push) Has been skipped
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s
2025-09-07 21:39:36 -03:00
Ricardo Carneiro
79c254905a fix: create plan 2025-09-07 21:39:32 -03:00
2 changed files with 381 additions and 316 deletions

View File

@ -24,7 +24,8 @@
"Bash(dotnet nuget locals:*)",
"Bash(/mnt/c/vscode/vcart.me.novo/clean-build.sh:*)",
"Bash(sed:*)",
"Bash(./clean-build.sh:*)"
"Bash(./clean-build.sh:*)",
"Bash(git add:*)"
]
},
"enableAllProjectMcpServers": false

View File

@ -78,6 +78,10 @@ public class StripeWebhookController : ControllerBase
await HandleSubscriptionUpdated(stripeEvent);
break;
case "customer.subscription.created":
await HandleSubscriptionCreated(stripeEvent);
break;
default:
_logger.LogInformation($"Unhandled webhook event type: {stripeEvent.Type}");
break;
@ -263,6 +267,66 @@ public class StripeWebhookController : ControllerBase
}
}
private async Task HandleSubscriptionCreated(Event stripeEvent)
{
try
{
if (stripeEvent.Data.Object is Subscription stripeSubscription)
{
_logger.LogInformation($"Subscription created: {stripeSubscription.Id} for customer: {stripeSubscription.CustomerId}");
// Get subscription record from our database
var subscription = await _subscriptionRepository.GetByStripeSubscriptionIdAsync(stripeSubscription.Id);
if (subscription != null)
{
var service = new SubscriptionItemService();
var subItem = service.Get(stripeSubscription.Items.Data[0].Id);
// Update subscription status to active
subscription.Status = "active";
subscription.UpdatedAt = DateTime.UtcNow;
subscription.CurrentPeriodStart = subItem.CurrentPeriodStart;
subscription.CurrentPeriodEnd = subItem.CurrentPeriodEnd;
// Update plan type based on Stripe price ID
var priceId = stripeSubscription.Items.Data.FirstOrDefault()?.Price.Id;
if (!string.IsNullOrEmpty(priceId))
{
subscription.PlanType = MapPriceIdToPlanType(priceId);
}
await _subscriptionRepository.UpdateAsync(subscription);
// Activate user pages that were pending payment or trial
var userPages = await _userPageService.GetUserPagesAsync(subscription.UserId);
foreach (var page in userPages.Where(p =>
p.Status == ViewModels.PageStatus.PendingPayment ||
p.Status == ViewModels.PageStatus.Expired))
{
page.Status = ViewModels.PageStatus.Active;
page.UpdatedAt = DateTime.UtcNow;
await _userPageService.UpdatePageAsync(page);
}
_logger.LogInformation($"Activated subscription and {userPages.Count()} pages for user {subscription.UserId}");
}
else
{
_logger.LogWarning($"Subscription not found in database: {stripeSubscription.Id}");
}
}
else
{
_logger.LogWarning($"Unexpected event type on HandleSubscriptionCreated: {stripeEvent.Type}");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error handling subscription created event");
throw new Exception("Error handling subscription created event", ex);
}
}
private string MapPriceIdToPlanType(string priceId)
{
// Map Stripe price IDs to plan types