Mr Fake
Mr Fake

Reputation: 147

Stripe, how to include multiple products in a Checkout session?

I'm new to the Stripe API and I am trying to implement the prebuilt Checkout page for my ecommerce site. I want a customer to be able to purchase at most 6 items, and I want those 6 items to be featured on the checkout page.

The documentation mentions that line_items is a "list of items the customer is purchasing," so I have 6 objects in the line_items array for each product; however, stripe.sessions.create() doesn't execute and the Checkout page doesn't load. If I remove all but 1 of the objects in line_items, the session works and the page is loaded.

Below is from my backend that handles the request for creating a Checkout session. Any help is appreciated!

app.post('/create-session', async (req, res) => {

    const YOUR_DOMAIN = 'http://localhost:3000/Home';

    const session = await stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      line_items: [
        {
          price_data: {
            currency: 'usd',
            product_data: {
              name: 'Product1',
              images: ['https://i.imgur.com/EHyR2nP.png'],
            },
            unit_amount: 1899,
          },
          quantity: 1,
        },
        {
            price_data: {
                currency: 'usd',
                product_data: {
                name: 'Product2',
                images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 1899,
            },
            quantity: 1,
        },
        {
            price_data: {
                currency: 'usd',
                product_data: {
                name: 'Product3',
                images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 1899,
            },
            quantity: 1,
        },
        {
            price_data: {
                currency: 'usd',
                product_data: {
                name: 'Product4',
                images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 1899,
            },
            quantity: 1,
        },
        {
            price_data: {
                currency: 'usd',
                product_data: {
                name: 'Product5',
                images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 1899,
            },
            quantity: 1,
        },
        {
            price_data: {
                currency: 'usd',
                product_data: {
                name: 'Product6',
                images: ['https://i.imgur.com/EHyR2nP.png'],
                },
                unit_amount: 1899,
            },
            quantity: 1,
        },
      ],
      mode: 'payment',
      success_url: `${YOUR_DOMAIN}?success=true`,
      cancel_url: `${YOUR_DOMAIN}?canceled=true`,
    });
    res.json({ id: session.id });
  });

EDIT: I am using stripe-node 8.119.0.

In the Stripe developer logs, I am getting a 400 error for POST /v1/checkout/sessions. This is the response body:

    {
  "error": {
    "code": "parameter_invalid_integer",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-invalid-integer",
    "message": "This value must be greater than or equal to 1.",
    "param": "line_items[1][quantity]",
    "type": "invalid_request_error"
  }
}

On the frontend, I am getting the following in my console: error message

Upvotes: 4

Views: 11138

Answers (1)

Scott Paterson
Scott Paterson

Reputation: 151

Here is what is working for me in PHP:

$checkout_session = \Stripe\Checkout\Session::create([
      'payment_method_types' => ['card'],
      'line_items' => [ 
      [
        'price_data' => [
          'currency' => 'usd',
          'unit_amount' => 2000,
          'product_data' => [
            'name' => 'test',
          ],
        ],
        'quantity' => 1,
      ],
      [
        'price_data' => [
          'currency' => 'usd',
          'unit_amount' => 1000,
          'product_data' => [
            'name' => 'test1',
          ],
        ],
        'quantity' => 1,
      ],
      ],
      'mode' => 'payment',
      'success_url' => $return_url,
      'cancel_url' => $return_url,
    ]);

Looks like your unit_amount may be not nested correctly?

Upvotes: 2

Related Questions