Skip to content

Commit

Permalink
Update Stripe event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
filipbekic01 committed Sep 30, 2024
1 parent 8599edd commit 05028d9
Showing 1 changed file with 110 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,126 @@ public async Task<OperationResult<EventHandlerResponse>> ExecuteAsync(EventHandl
settings.Value.StripeSecretWebhook
);

if (stripeEvent.Type == Events.CustomerSubscriptionDeleted)
switch (stripeEvent.Type)
{
var subscription = stripeEvent.Data.Object as Subscription;

var user = await userManager.FirstOrDefaultByStripeId(subscription!.CustomerId);
if (user is null)
case Events.CustomerSubscriptionDeleted or Events.CustomerSubscriptionUpdated:
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
var subscription = stripeEvent.Data.Object as Subscription;

var user = await userManager.FirstOrDefaultByStripeId(subscription!.CustomerId);
if (user is null)
{
Title = "Unauthorized Access",
Detail = "The user could not be found or is not authorized.",
Status = StatusCodes.Status401Unauthorized
});
}
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "Unauthorized Access",
Detail = "The user could not be found or is not authorized.",
Status = StatusCodes.Status401Unauthorized
});
}

if (user.Subscription?.StripeId != subscription.Id)
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "Invalid Subscription",
Detail = "The subscription does not match the user's records.",
Status = StatusCodes.Status400BadRequest
});
}

if (user.Subscription?.StripeId != subscription.Id)
user.Subscription.StripeStatus = subscription.Status;

var filter = Builders<User>.Filter.Eq(q => q.Id, user.Id);
var update = Builders<User>.Update
.Set(q => q.Subscription, user.Subscription);

await usersCollection.UpdateOneAsync(filter, update);
break;
}
case Events.InvoicePaymentFailed or Events.InvoicePaymentSucceeded:
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
var invoice = stripeEvent.Data.Object as Invoice;
var subscriptionId = invoice?.SubscriptionId;
var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(subscriptionId);

var user = await userManager.FirstOrDefaultByStripeId(subscription.CustomerId);

if (user is not null)
{
Title = "Invalid Subscription",
Detail = "The subscription does not match the user's records.",
Status = StatusCodes.Status400BadRequest
});
if (user.Subscription is not null)
{
user.Subscription.StripeStatus = subscription.Status;

var filter = Builders<User>.Filter.Eq(q => q.Id, user.Id);
var update = Builders<User>.Update.Set(q => q.Subscription!.StripeStatus,
user.Subscription.StripeStatus);
await usersCollection.UpdateOneAsync(filter, update);
}
else
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "Subscription Not Found",
Detail = "No subscription found for provided user.",
Status = StatusCodes.Status404NotFound
});
}
}
else
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "User Not Found",
Detail = "No user found with the provided Stripe customer ID.",
Status = StatusCodes.Status404NotFound
});
}

break;
}
case Events.PaymentIntentSucceeded or Events.PaymentIntentPaymentFailed:
{
var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
var subscriptionId = paymentIntent?.Metadata["subscription_id"];
var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(subscriptionId);

var user = await userManager.FirstOrDefaultByStripeId(subscription.CustomerId);

user.Subscription.StripeStatus = subscription.Status;
if (user is not null)
{
if (user.Subscription is not null)
{
user.Subscription.StripeStatus = subscription.Status;

var filter = Builders<User>.Filter.Eq(q => q.Id, user.Id);
var update = Builders<User>.Update
.Set(q => q.Subscription, user.Subscription);
var filter = Builders<User>.Filter.Eq(q => q.Id, user.Id);
var update = Builders<User>.Update.Set(q => q.Subscription!.StripeStatus,
user.Subscription.StripeStatus);
await usersCollection.UpdateOneAsync(filter, update);
}
else
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "Subscription Not Found",
Detail = "No subscription found for provided user.",
Status = StatusCodes.Status404NotFound
});
}
}
else
{
return OperationResult<EventHandlerResponse>.Failure(new ProblemDetails
{
Title = "User Not Found",
Detail = "No user found with the provided Stripe customer ID.",
Status = StatusCodes.Status404NotFound
});
}

await usersCollection.UpdateOneAsync(filter, update);
}
else if (stripeEvent.Type == Events.InvoicePaymentFailed)
{
// todo: send e-mail to the user
break;
}
}

return OperationResult<EventHandlerResponse>.Success(new EventHandlerResponse());
Expand Down

0 comments on commit 05028d9

Please sign in to comment.