Sun Nov 10 2024
There are many, many examples of Stripe integration on the web, including videos. Here's the problem with every single other example: they HARD CODE values for processing payments. 😩
So, I will walk through my implementation of a Stripe Checkout session using ASP.NET Core Minimal API. This approach leverages the simplicity and performance of minimal APIs while integrating with Stripe for payment processing. I'll cover setting up the project, configuring CORS, and implementing the Stripe Checkout session.
TL;DR: The secret sauce is model binding in the API call, you must build up the dynamic cart contents from your front end.
Create a new ASP.NET Core Minimal API project using the following command:
dotnet new web -n StripeCheckoutSession
cd StripeCheckoutSession
Add the Stripe.NET NuGet package to the project:
dotnet add package Stripe.net
Create a local.settings.json file in the root of your project to store your Stripe API key:
{
"Stripe": {
"SecretKey": "sk_test_51"
}
}
Add the following code to the Program.cs
file:
using Stripe;
using Stripe.Checkout;
var builder = WebApplication.CreateBuilder(args);
// Add CORS services to the DI container.
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin", builder =>
{
builder.WithOrigins("http://localhost:4000", "http://127.0.0.1:4000", "https://yourfrontend.com")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Add Application Insights telemetry
builder.Services.AddApplicationInsightsTelemetry();
var app = builder.Build();
app.UseCors("AllowSpecificOrigin");
StripeConfiguration.ApiKey = builder.Configuration["StripeApiKey"];
app.MapPost("/api/CreateCheckoutSession", async (List<SessionLineItemOptions> lineItems) =>
{
var options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string>
{
"cashapp",
"card",
},
LineItems = lineItems,
Mode = "payment",
SuccessUrl = "https://example.com/success",
CancelUrl = "https://example.com/cancel",
};
var service = new SessionService();
Session session = await service.CreateAsync(options);
return Results.Ok(session);
});
app.Run();
MapPost
method, which accepts a list of SessionLineItemOptions
as a parameter. This allows you to pass dynamically sized cart contents from the front end to the API. Not just a single T-shirt. Pop this into a .http file in Visual Studio Code and run it with the REST Client extension:
### Create Checkout Session
POST https://localhost:8080/api/CreateCheckoutSession
Content-Type: application/json
[
{
"Price": "price_1ofMany",
"Quantity": 1
}
]
You can specify lots of other properties if you would like to get more specific, but building items in the dashboard handles a lot of this for you. The above is the minimal implementation to get you started. It will work for that T-shirt if you set it up correctly in the Stripe dashboard. Building the front-end will likely change every two weeks anyways, so I leave that part up to you, dear reader.
In this article, we implemented a Stripe Checkout session in an ASP.NET Core Minimal API project. We configured CORS, set up the Stripe API key, and created a Stripe Checkout session endpoint. This approach allows you to build a dynamic cart on the front end and pass the cart contents to the API for processing payments. I hope this helps you integrate Stripe Checkout in your ASP.NET Core Minimal API project. Happy coding! 🚀