next.jsstripe-paymentse-commerceprisma

Best Approach for Managing E-commerce Product Data with Stripe?


I am building an e-commerce project using Next.js, Prisma with MongoDB, and Stripe.

As I started designing the schema for a Product, I ran into some confusion.

Should I directly create the products in my database? With this approach, I realized that I would be creating a new product in stripe for every checkout session. However, Stripe automatically archives these products, so I'm unsure if this is a good solution.

Alternatively, should I create products through the Stripe dashboard/API and then update my database based on webhook events?

Which approach is better, and why do you think so?


Solution

  • I think it's really subjective / largely depends on what requirements your app has.

    What you're referring to in your post are Stripe Checkout's price_data and product_data constructors, which are really powerful, as they allow you to create a Product and Price in line with a Checkout session.
    As you noted, these Products and Prices are only available for the session they're created in, and are auto-archived.

    The other solution is to create a Product and Price, which you then use / re-use in your Checkout sessions by their ID.

    While again this really depends on what you need for your app, I personally prefer using price_data and product_data, as it generally streamlines the project:

    If you want to go for that and want to keep track of extra information, you have many ways (again depending on how you prefer to implement things):

    • you can get them from the checkout session's line_items
    • if you lost the checkout session, you can get it by listing sessions by payment_intent (for payment mode) or by subscription (for subscription mode). API docs
    • in payment mode, set your metadata as payment_intent_data.metadata, and the metadata will be found on the downstream payment_intent.
    • in subscription mode, set your metadata as subscription_data.metadata, and the metadata will be found on the downstream subscription.

    Again, this is up to you, and nothing stops you from using both methods (you can also set metadata at session level with metadata). Personally I find adding metadata to the downstream objects easier because they're easier to fetch.