Stripe - Retrieving the line_items of checkout session
Asked Answered
W

2

7

I'm trying to get the line_items of the checkout session in Stripe so that I can send an email consisting of the product download link but unfortunately, When I try to retrieve the line_items in the checkout_session I get a null value.

Frontend: Next.js

Here is my code in /pages/api/webhooks/index.ts:

Check the line: 77

import { buffer } from "micro";
import Cors from "micro-cors";
import { NextApiRequest, NextApiResponse } from "next";

import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  // https://github.com/stripe/stripe-node#configuration
  apiVersion: "2020-08-27",
});

const webhookSecret: string = process.env.STRIPE_WEBHOOK_SECRET!;

// Stripe requires the raw body to construct the event.
export const config = {
  api: {
    bodyParser: false,
  },
};

const cors = Cors({
  allowMethods: ["POST", "HEAD"],
});

const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  if (req.method === "POST") {
    const buf = await buffer(req);
    const sig = req.headers["stripe-signature"]!;

    let event: Stripe.Event;

    try {
      event = stripe.webhooks.constructEvent(
        buf.toString(),
        sig,
        webhookSecret
      );
    } catch (err) {
      // On error, log and return the error message.
      console.log(`❌ Error message: ${err.message}`);
      res.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }

    // Successfully constructed event.
    console.log(`βœ… Success: ${event.id}`);

    // Cast event data to Stripe object.
    if (event.type === "payment_intent.created") {
      const paymentIntent = event.data.object as Stripe.PaymentIntent;
      console.log(`πŸ’° PaymentIntent status: ${paymentIntent.status}`);
      console.log('-----------------------------------------------------');
    } else if (event.type === "payment_intent.payment_failed") {
      const paymentIntent = event.data.object as Stripe.PaymentIntent;
      console.log(
        `❌ Payment failed: ${paymentIntent.last_payment_error?.message}`
      );
      console.log('-----------------------------------------------------');
    } else if (event.type === "customer.created") {
      const customer = event.data.object as Stripe.Customer;
      console.log(`Customer created: ${customer.id}`);
      console.log('-----------------------------------------------------');
    } else if(event.type === 'payment_intent.requires_action') {
      const paymentIntent = event.data.object as Stripe.PaymentIntent;
      console.log(`❌ Payment requires action: ${paymentIntent.id}`);
    } else if (event.type === "payment_intent.succeeded") {
      const paymentIntent = event.data.object as Stripe.PaymentIntent;
      console.log(`πŸ’° Payment succeeded: ${paymentIntent.id}`);
      console.log('-----------------------------------------------------');
    } else if (event.type === "charge.succeeded") {
      const charge = event.data.object as Stripe.Charge;
      console.log(`πŸ’΅ Charge id: ${charge.id}`);
      console.log('-----------------------------------------------------');
    } else if (event.type === "checkout.session.completed") {
      const checkout_session = event.data.object as Stripe.Checkout.Session;
      console.log(`πŸ’³ Session id: ${checkout_session.id}`);
      console.log(`Customer email: ${checkout_session.customer_details.email}`);
      console.log(`Order products: ${checkout_session.line_items}`);
      console.log('-----------------------------------------------------');
    } else {
      console.warn(`πŸ€·β€β™€οΈ Unhandled event type: ${event.type}`);
      console.log('-----------------------------------------------------');
    }

    // Return a response to acknowledge receipt of the event.
    res.json({ received: true });
  } else {
    res.setHeader("Allow", "POST");
    res.status(405).end("Method Not Allowed");
  }
};

export default cors(webhookHandler as any);

When I try to log the line_items in the checkout_session I get a null value.

Wabash answered 12/11, 2021 at 6:22 Comment(0)
G
14

As noted in the API reference, the line_items of a Session are "expandable" and not included by default. This means that they are not included in the object when delivered in a webhook event, and you must explicitly request the value via expansion when retrieving the object.

To get these, you'll need to retrieve the Session and request the line_items be included in the response:

const session = await stripe.checkout.sessions.retrieve(
  'cs_test_123', {
  expand: ['line_items']
});
Gaal answered 12/11, 2021 at 6:52 Comment(2)
Surprisingly, this option isn't in their docs but it works. stripe.com/docs/api/checkout/sessions/retrieve – Revisory
It's a global parameter for all API requests: docs.stripe.com/api/expanding_objects – Gaal
C
3

There is also a separate function listLineItems in API reference, which can be used for retrieving a checkout session's line items.

const lineItems = await stripe.checkout.sessions.listLineItems("cs_test_123");
console.log(lineItems);
console.log(lineItems.data);
Consequence answered 6/7, 2022 at 9:32 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.