Menu

Post image 1
Post image 2
1 / 2
0

How to prevent Foreign Key crashes with Stripe Webhooks in .NET 10.

DEV Community·Francisco Simões·5 months ago
#LTQnvCcG
#dotnet#saas#sql#aspdotnet#discuss#stripe
Reading 0:00
15s threshold

When integrating Stripe Subscriptions in a .NET application using Entity Framework Core, one of the most common issues developers face is dealing with the checkout.session.completed webhook. If you have a relational database, you probably have a SubscriptionPlans table and a UserSubscriptions table. A frequent mistake is forgetting to map the Stripe Price ID back to your internal Database Plan ID when the webhook fires. If you just try to insert the subscription, EF Core will default your Foreign Key to 0, resulting in a nasty Foreign Key constraint violation (Error 500). Here is the clean way to handle the webhook and map the Stripe Price ID to your internal database ID: private async Task HandleCheckoutSessionCompleted ( Session session ) { var userId = session . ClientReferenceId ; // We passed this during checkout var subscriptionService = new SubscriptionService (); var stripeSubscription = await subscriptionService . GetAsync ( session . SubscriptionId ); // 1.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More