Compare commits
2 Commits
787fa63f68
...
0f6ae71997
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f6ae71997 | ||
|
|
79c254905a |
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user