Stripe "Missing required param: line_items[0][currency]." Node js

Viewed 3861

I'm creating subscription on node js backend. It had been working good but today I got this error. I didn't made any changes in code - it just starts returning this error. backend code:

 app.post('/api/subscription', async (req, res) => {
  const { priceId } = req.body;
  
  try {
    const session = await stripe.checkout.sessions.create({
      mode: "subscription",
      payment_method_types: ["card"],
      line_items: [
        {
          price: priceId,
          // For metered billing, do not pass quantity
          quantity: 1,
        },
      ],
      // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
      // the actual Session ID is returned in the query parameter when your customer
      // is redirected to the success page.

      success_url: 'https://someurl',
      cancel_url: 'https://someurl',
    });

    res.send({
      sessionId: session.id,
    });
  } catch (e) {
    console.log(e)
    res.status(400);
    return res.send({
      error: {
        message: e.message,
      }
    });
  }
})

from client I'm sending

fetch("http://localhost:8000/api/subscription", {
      method: "POST",
      body: JSON.stringify({ priceId }),
    });

I've taken this code from official stripe example here https://stripe.com/docs/billing/subscriptions/checkout And as I said it works fine, and now I've tested it on two different stripe accounts and getting the same error. It looks like something changed on stripe but not in their documentation

1 Answers

If priceId is undefined/null it won't be sent in the request. If the Price isn't present the API assumes you're trying to specify information about a line item without using a Price, and one of the first checks it performs is for a valid currency (which you don't have), which results in the Missing required param: line_items[0][currency]. error.

To fix the issue you'll need to figure out why priceId isn't being populated as expected, and you may also want to add a check to make sure priceId is valid before proceeding to the Checkout Session creation step.

Related