Can someone explain to me why 'map' is undefined? I provided my server.js and checkout.js below
The problem is that when I click a product to add to the cart to checkout it shows all the products, including the prices and total, on the stripe session instead of the item I selected. My goal is to only show one of the items selected from the actual website
server.js
//require("dotenv").config()
// This is your test secret API key.
const stripe = require("stripe")(
"sk_test_XXXXXX"
);
const express = require("express");
const app = express();
app.use(express.static("public"));
app.use(express.json());
const YOUR_DOMAIN = "http://localhost:4242";
const storeItems = new Map([
[1, { priceId: "price_XXXXX", name: "Black Sweatsuit" }],
[2, { priceId: "price_XXXXX", name: "Grey Sweatsuit" }],
[3, { priceId: "price_XXXXX", name: "Red Sweatsuit" }],
[4, { priceId: "price_XXXXX", name: "Blue Sweatsuit" }],
[5, { priceId: "price_XXXXX", name: "Black T-Shirt" }],
[6, { priceId: "price_XXXXX", name: "Blue T-Shirt" }],
[7, { priceId: "price_XXXXX", name: "Purple T-Shirt" }],
]);
app.post("/create-checkout-session", async (req, res) => {
const session = await stripe.checkout.sessions.create({
line_item: req.body.items.map((item) => {
const storeItem = storeItems.get(item.id);
return {
price_data: {
currency: "usd",
product_data: {
name: storeItem.name,
},
unit_amount: storeItem.priceId,
},
};
}),
mode: "payment",
success_url: `${YOUR_DOMAIN}/success.html`,
cancel_url: `${YOUR_DOMAIN}/cancel.html`,
automatic_tax: { enabled: true },
});
res.redirect(303, session.url);
});
app.listen(4242, () => console.log("Running on port 4242"));
checkout.js
const button = document.getElementById("checkout-button");
button.addEventListener("click", () => {
fetch("/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
items: [
// { id: 1, quantity: 1 },
// { id: 2, quantity: 1 },
// { id: 3, quantity: 1 },
// { id: 4, quantity: 1 },
// { id: 5, quantity: 1 },
// { id: 6, quantity: 1 },
// { id: 7, quantity: 1 },
],
}),
})
.then((res) => {
if (res.ok) return res.json();
return res.json().then((json) => Promise.reject(json));
})
.then(({ url }) => {
window.location = url;
})
.catch((e) => {
console.error(e.error);
});
});