I am working on the Stripe payment API and I have a form to collect data that is written in HTML and would like to retrieve the collected data to another javascript file (server.js). I have tried changing the ${AMOUNT} to req.body.amount but seems that is not the case. Is it actually possible to retrieve the data via the form method? Or it is only be done in the fetch method?
HTML:
<form action="/create-checkout-session" method="POST" id="create-checkout-session">
<div className="description">
<span>Price:</span>
<input type="number" className="field" id="amount" name="amount" min="500"/>
<button type="submit" id="checkout">
Checkout
</button>
</div>
</form>
Javascript (server.js):
app.post('/create-checkout-session', async (req, res) => {
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: 'hkd',
product_data: {
name: "Test",
},
unit_amount: ${AMOUNT},
},
quantity: 1,
},
],
mode: 'payment',
success_url: `${YOUR_DOMAIN}?success=true`,
cancel_url: `${YOUR_DOMAIN}?canceled=true`,});
res.redirect(303, session.url);
});
app.listen(4242, () => console.log('Running on port 4242'));
I have solved this by adding express.urlencoded in the server.js, which looks like this:
app.use(express.urlencoded({ extended: true }));