I have been struggling with Stripe http 400 error in production server. The node.js (Express) code below works perfectly with localhost: it connects stripe-service and gets the payment session-id. But the same code in production server (iisnode.exe) returns all the time just http 400 error. My frontend cart-code (Angular) is in different domain than the backend-api where the endpoint below locates. Can anyone figure out what is wrong? Stripe docs for errors (https://stripe.com/docs/api/errors) mention that http 400 often is due to a missing required parameter. But I cannot see that in my case.
app.post('/create-checkout-session', async (req, res) => {
const cartData = req.body
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card', 'sepa_debit'],
shipping_rates: ['shr_1Jmh85AuqIBxGbL5Z7JbTJaV'],
shipping_address_collection: {
allowed_countries: ['FI'],
},
line_items: [{
"price_data": {
"currency": "eur",
"product_data": {
"name": "Product 1",
"description": "Further info here"
},
"unit_amount": 6800
},
"quantity": 2
}],
phone_number_collection: {
enabled: true,
},
mode: 'payment',
success_url: `${stripeUrl}/myynti/success?true&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${stripeUrl}/myynti/cancel`
});
res.json({ id: session.id });
} catch (e) {
res.status(400);
return res.send({
error: {
message: e.message,
}
});
}
}));
(ADDED after comment by alex) Here is what I have in Stripe Dashboard behind the url alex provided:
POST /v1/checkout/sessions Status 400 ERR ID req_so02k9kRthmCZ7 Time 10/21/21, 5:20:23 PM IP address ******** API version 2020-08-27 Latest Source Stripe/v1 NodeBindings/8.178.0 Idempotency Key — 461bb3f4-e2e1-421d-b177-9713176448af
ACTUAL ERROR MESSAGE: invalid_request_error The line_items parameter is required in payment mode.
{
"payment_method_types": {
"0": "card",
"1": "sepa_debit"
},
"shipping_rates": {
"0": "shr_1Jmh85AuqIBxGbL5Z7JbTJaV"
},
"shipping_address_collection": {
"allowed_countries": {
"0": "FI"
}
},
"phone_number_collection": {
"enabled": "true"
},
"mode": "payment",
"success_url": "http://localhost:4200/myynti/success?true&session_id={CHECKOUT_SESSION_ID}",
"cancel_url": "http://localhost:4200/myynti/cancel"
}
The error message says that the "line_items parameter" is lacking but that should not be the case? - any ideas why see also my original code where there is line_items parameter included.