I'm not getting any response from postman due to this error. I tried to add app.use(express.json()); in index.js but still not working. Any idea on how to solve this?
const stripeAPI = require('../stripe');
async function createCheckoutsession(res, req) {
const domainUrl = process.env.WEB_APP_URL;
const { line_items, customer_email } = req.body;
if (!line_items || !customer_email) {
return res.status(400).json({ error: 'missing required session paramaters' });
}
let session;
try {
session = await stripeAPI.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'payment',
line_items,
customer_email,
success_url: `${domainUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${domainUrl}/canceled`,
shipping_address_collection: { allowed_countries: ['GB', 'US'] }
});
res.status(200).json({ sessionId: session.id, });
} catch (error) {
console.log(error);
res.status(400).json({ error: 'an error occurred, unable to create session' });
}
}
module.exports = createCheckoutsession;
(index.js)
const express = require('express'); const cors = require('cors');
require('dotenv').config({ path: './.env' });
const createCheckoutSession = require('./api//checkout');
const app = express();
const port = 8080;
app.use(express.json());
app.use(cors({origin: true}));
app.get('/', (req, res) => res.send('HELLO WORLD!'));
app.post('/create-checkout-session', createCheckoutSession);
app.listen(port, () => console.log('server listening on port', port));
(stripe.js)
const stripeAPI = require('stripe')(process.env.SECRET_KEY);
module.exports = stripeAPI;enter code here
app.use(express.json())
and where you bindcreateCheckoutsession
to the/create-checkout-session
path? Note that your JSON middleware should be registered beforeapp.post("/create-checkout-session", createCheckoutsession)
(or however you've set up that handler) – Phil