Title: add multiple products to stripe checkout nodejs
Tags: javascript;html;node.js;stripe-payments
Question: I am creating an e-commerce site, where the customer will be able to add products to their cart. on checkout, I need all the items in the cart to be put into the stripe checkout page -- what I mean by this is, in my code, I need to pass the image, name, and price of all the products in my cart, into the stripe checkout page...
here's my code so far!
const YOUR_DOMAIN = 'http://localhost:1208';
app.post('/create-checkout-session', async (req, res) =&gt; {
var user = &quot;select * from &quot; + tableID + &quot;&quot;;
ibmdb.open(db2ConnString, function(err, conn) {
if (err) return console.log(err);
conn.query(user, function(err, rows) {
if (err) {
console.log(err);
}
var productName = &quot;&quot;;
var image_url = &quot;&quot;;
var price = &quot;&quot;;
for (var i = 0; i < rows.length; i++ ) {
productName = rows[i].NAME;
image_url = rows[i].IMAGE_URL;
price = rows[i].PRICE;
}
conn.close(function() {
console.log(&quot;closed function&quot;)
});
});
})
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'cad',
product_data: {
name: ProductName,
images: image_url,
},
unit_amount: price,
},
quantity: 1,
},
],
mode: 'payment',
success_url: `${YOUR_DOMAIN}/success.html`,
cancel_url: `${YOUR_DOMAIN}/cancel.html`,
res.json({ id: session.id });
there will be multiple products in the cart, so how do I fix this?


Here is another answer: If I am reading your code right
for (var i = 0; i < rows.length; i++ ) {
productName = rows[i].NAME;
image_url = rows[i].IMAGE_URL;
price = rows[i].PRICE;
}
You need to put these values into an array of objects, where each object is formatted like the price_data hash that CheckoutSession is expecting.
So after your for-loop, your array e.g. price_data_array would look like:
{
price_data: {
currency: 'cad',
product_data: {
name: ProductName,
images: image_url,
},
unit_amount: price,
},
quantity: 1,
},
{
price_data: {
currency: 'cad',
product_data: {
name: ProductName,
images: image_url,
},
unit_amount: price,
},
quantity: 1,
},
.....
Then when you create your CheckoutSession, just pass that array under line_items: price_data_array.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/01/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><html><node.js><stripe-payments>', 'Title': 'add multiple products to stripe checkout nodejs', 'ViewCount': 332, 'id': 66908407, 'user_AboutMe': None, 'user_AccountId': 19640009.0, 'user_DisplayName': 'Gianluca', 'user_Reputation': 802.0, 'user_Views': 201.0, 'user_id': 14376347.0}}