2
votes

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

1
"stripe.sessions.create() doesn't execute and the Checkout page doesn't load" Is there an error message in your console, or in the Stripe developer logs?ceejayoz
As @ceejayoz says: can you provide more detail? This looks correct/valid, so we'll need any errors to help you debug. Can you share which version of stripe-node you're using and what your default API version is?Nolan H
My stripe-node version is 8.119.0. I'm not getting any issues in my backend, but my Stripe developer console shows there is a 400 error for POST /v1/checkout/sessions: { "error": { "code": "parameter_invalid_integer", "doc_url": "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" } }Mr Fake
Are your quantities coming out of a variable or database somewhere? Or are they always hard-coded to quantity: 1 as shown in the example above? The Stripe developer console should show what you sent as well as what they responded; does it match up?ceejayoz
@creejayoz Yeah, my quantities are temporarily hardcoded. But in a previous version I had them dependent on a state starting from value 0, and it looks like I hadn't fully updated my backend. It's working now that every quantity is hardcoded to 1, so it seems that quantities set to 0 caused the issue. If you don't mind me asking another question, does this mean I can't always have all 6 products defined in the checkout session? My approach was going to be to have all 6 defined in every session, but have quantity vary (starting from 0) depending on how much product is selected in the frontend.Mr Fake

1 Answers

0
votes

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?