简体   繁体   中英

Getting this error in production but works fine in development - StripeInvalidRequestError: The `line_items` parameter is required in payment mode

I have a node app that I just recently deployed and am getting an error from the stripe/checkout/sessions api in production but never in development. The error: "StripeInvalidRequestError: The line_items parameter is required in payment mode" is pretty straightforward so I know it's my line_items object that is empty but I don't know why since this only happens in production but works fine in development.

Here is the code for that route

router.get('/checkout', async (req, res) => {
if (!req.session.cart) {
    return res.redirect('shop/shopping-cart');
}

try {
    let cart = new Cart(req.session.cart);
    const cartProducts = await cart.generateArray();
    let lineItems = [];
    cartProducts.forEach(async (cartProduct) => {
        let productImages = new Array();
        cartProduct.item.images.forEach((img) => {
            productImages.push(img.url);
        });
        const product = await stripe.products.create({
            name: cartProduct.item.name,
            images: productImages
        });
        const price = await stripe.prices.create({
            product: product.id,
            unit_amount: cartProduct.price,
            currency: 'usd'
        });
        lineItems.push({ price: price.id, quantity: cartProduct.qty });
    });
    let custObj = {};
    if (process.env.NODE_ENV !== 'production') {
        custObj = {
            payment_method: 'pm_card_visa',
            invoice_settings: {
                default_payment_method: 'pm_card_visa',
            }
        }
    }
    const customer = await stripe.customers.create(custObj);
    const success_url = process.env.NODE_ENV !== 'production' ?
    'http://localhost:3000/shop/success?session_id={CHECKOUT_SESSION_ID}' :
    'https://aboutthatwedding.com/shop/success?session_id={CHECKOUT_SESSION_ID}';
    const cancel_url = process.env.NODE_ENV !== 'production' ?
    'http://localhost:3000/shop/shopping-cart' :
    'https://aboutthatwedding.com/shop/shopping-cart';

    const session = await stripe.checkout.sessions.create({
        customer: customer.id,
        payment_method_types: ['card'],
        line_items: lineItems,
        success_url: success_url,
        cancel_url: cancel_url,
        mode: "payment",
        billing_address_collection: 'required',
        shipping_address_collection: {
            allowed_countries: ['US', 'CA'],
        }
    });

    res.render('shop/checkout', {
        sessionId: session.id,
        products: cartProducts,
        totalPrice: cart.totalPrice,
        stripePubKey: process.env.NODE_ENV !== 'production' ? 
                      process.env.STRIPE_PUBLIC_KEY_TEST :
                      process.env.STRIPE_PUBLIC_KEY_LIVE
    });
} catch (error) {
    console.log(error);
}

});

Looks like it was a foreach( async...) issue. I changed the function to that iterates over the cartProducts and images to for of loops instead of forEach loops.

Got that information from This similar question that I found after posting my question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM