How to require stripe in VS code file: const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
You don't want to give away your secret key, so you add require('dotenv').config(); and then a new file with your secret key inside labelled .env with STRIPE SECRET KEY_(secret key number) inside so it fetches it there and doesn't upload it to github
In order to get the pictures and prices in a helpful format we first GET the products and prices, and then associate the products with the prices, and then send back a list of the products and their prices together (see code below)
Today I learned about stripe
app.get('/products', async (_request, response) => {
const products = await stripe.products.list({ limit: 100, }); const prices = await stripe.prices.list({ limit: 100, });
prices.data.forEach(price => { const theAssociatedProduct = products.data.find( product => product.id === price.product ); theAssociatedProduct.price = price; });
const cleanedUpProducts = products.data.map(product => ({ name: product.name, description: product.description, image: product.images[0], category: product.metadata.category, currency: product.price.currency, price_cents: product.price.unit_amount, })); response.json(cleanedUpProducts); });
app.listen(3000, () => console.log(
Server is on!
));