Stripe API for Automation

This post is the second part of a complete and free course on Stripe, which is available both on this website as well as on YouTube.

Both the course and its code are openly and freely available. If you enjoy the content, you know you can support the website by becoming a premium member, or buying my book.

In the YouTube video, the content of this post starts at minute 95:20.

 

 

Now that we have a clear understanding of how Stripe, the API, the CLI, and even the SDK for the code work, let's modify our project to automate as many processes as possible.  

 

 

1 - Creating Products and Prices through the Stripe API

 

Every time we create a product in the database, it will also be created in Stripe with the price we've set, instead of having to create it manually and link the price. 

 

To achieve this, we only need to modify the use case as follows: 

public class CreateProductUseCase(ApplicationDbContext applicationDbContext){	public async Task<bool> Execute(CreateProductRequest createProduct)	{		//TODO: validations ETC but i'm not gonna waste time on that.		string productId = await CreateProductInStripe(createProduct.Name, createProduct.ImageUrl);		string priceId = await CreatePriceStripe(productId, Convert.ToInt64(createProduct.Price * 100));		applicationDbContext.Products.Add(new Data.Entities.ProductEntity()		{			ImageUrl = createProduct.ImageUrl,			Name = createProduct.Name,			Price = createProduct.Price,			StripePriceId = priceId,		});		return await applicationDbContext.SaveChangesAsync() > 0;	}	private async Task<string> CreateProductInStripe(string name, string imageUrl)	{		var options = new ProductCreateOptions		{			Name = name,			Images = new List<string>()			{				imageUrl,			}		};		var service = new ProductService();		Product product = await service.CreateAsync(options);		return product.Id;	}	private async Task<string> CreatePriceStripe(string productId, long priceInCents)	{		var options = new PriceCreateOptions()		{			Active = true,			Currency = "eur",			UnitAmount = priceInCents,			Product = productId		};		PriceService priceService = new PriceService();		var price = await priceService.CreateAsync(options);		return price.Id;	}}

 

The main difference now is that we will get the priceId through the API instead of doing it manually in the browser as we saw in the first chapter of the course, which is essential if we're adding many products. 

 

If you notice, in the code we are following the same process we used in the web interface: first we create the product and then the price.

 

We can easily create a couple of books through the API we used in previous chapters and see the result:  

If we click to buy, we will go to Stripe's checkout page and be able to make the purchase without any issue. 

 

 

2 - Payments with User Accounts

 

One of the most important aspects of web applications is being able to have registered users and process payments through these accounts so that information such as subscriptions can be maintained; 

 

Another very important reason can be to send information from our website to Stripe. 

 

I'm not going to go into detail about user management since there are many ways to do it, and I have used the default method that comes with the .NET 8 template as seen in the first chapter. In production, I recommend having a dedicated user management service.

 

For this case, I have created a user called [email protected] now, when this user makes a payment, their email will be embedded into the checkout process and won't be changeable. 

SessionCreateOptions options = new SessionCreateOptions{	SuccessUrl = "https://localhost:7265/payment-completed",	CustomerEmail = User?.FindFirstValue(ClaimTypes.Email) ?? null, 👈	LineItems = new List<SessionLineItemOptions>	{		new SessionLineItemOptions		{			Price = stripePriceId,			Quantity = 1,		},	},	Mode = "payment",};

 

When clicking to buy, we can see the email field is completely locked.

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café