#derrick_code
1 messages · Page 1 of 1 (latest)
👋 Welcome to your new thread!
⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1225042405724520459
📝 Have more to share? Add more details, code, screenshots, videos, etc. below.
hi there!
are you saying the API call stripe.prices.search() returns duplicate products?
hey @tulip kayak no i tried doing it that way to search for a product and if the product exists if does not create it in the product catalogue...does that make sense ..Trying something like this https://youtu.be/2_-6QpARFqc?si=HNuxqROIvBGgOouT
Learn how to upload a JSON object and create a product catalog in Stripe.
Presenter
CJ Avilla - Developer Advocate at Stripe - https://twitter.com/cjav_dev
Table of contents
00:00 Introduction
00:57 Product object API reference
02:08 Price object API reference
02:56 Read in and map JSON
07:22 Create price object
08:22 Executing the sc...
if the product exists if does not create it in the product catalogue...does that make sense ..
yes that makes sense. but I still don't understand your issue.
also note that you don't have to create Product/Prices at all with Checkout. instead you could useprice_dataandproduct_data: https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-line_items-price_data
oooh okay okay let me try and explain the best that i can ...my issue was , since am fetching the data from the fakeStoreApi ...i wanted that data being returned to be made dynamically in the product catalogue ...What my code did or rather does is that whenever someone checkouts the items that person decides to buy will be made dynamically in the product catalogue.. the issue arose where for example another user comes in a buys the same thing ..well the products are still created in the catalogue again causing to have duplicate products ..so i was trying a way to avoid.. If i still don't makes sense don't be afraid to tell me
have you checked the price_data I shared above? this way you don't need to use Price at all
refactoring my code right now don't know if you would still be around but i'll give you feedback when done...but since you are here and probably someone who's a master of this ..is what am doing advisable lol or what are your thoughts
but since you are here and probably someone who's a master of this ..is what am doing advisable lol or what are your thoughts
if you have a small number of Price, then create the Price in advance and reuse them. If you have a very large number of Price, it might be simpler to create them dynamically withprice_data.
hey this is what i did and it works and
require('dotenv').config();
const express = require('express');
const app = express();
const cors = require('cors');
app.use(express.json());
app.use(cors());
app.use(express.static('public'));
const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY);
app.post('/create-checkout-session', async (req, res) => {
const cartProducts = req.body.items;
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: cartProducts.map((item)=>{
return{
price_data:{
currency: 'usd',
unit_amount: item.price *100,
product_data:{
name: item.title,
description:item.description,
images:[item.image]
}
},
quantity: item.quantity
}
}),
mode: 'payment',
success_url: 'http://localhost:5173/success',
cancel_url: 'http://localhost:5173/cancel',
});
res.json({ url: session.url });
} catch (error) {
console.error('Error creating checkout session:', error);
res.status(500).json({ error: 'Failed to create checkout session' });
}
});
app.listen(5173, () => console.log('Server is running on port 5173'));
was even able to add images which is pretty awsome
so there's no need to even create products in the product catalogue
This will create a lot of one-off Products. If you don't plan to use a different image/description/name for each Checkout Session, I would recommend creating one Product and reusing it across Sessions.
i tried creating them dynamically but ended up creating duplicates every time i bought the same item...for more context ( right now i add a t-shirt in the cart and proceed to checkout the item would dynamically be created. That's great .. I go back again to the app and choose the same t-shirt and proceed to checkout again , it is created in the product catalogue resulting to duplictes) i really hope you can understand me ....
Yes, that's expected, since you create them every time you start a Checkout Session. This is only useful if every Checkout session has unique Products. If you have a fixed catalogue of products in your shop, you can pre-create it in the Stripe Dashboard (or write a script to do it via API), and then just use the Product ID when creating a Checkout Session.
ooh man thank you for your time , appreciate it and will try and in cooperate what you just said.
Happy to help.