How do I retrieve the product id from a Checkout Session Completed object?

757 views
Skip to first unread message

Ron Parker

unread,
Feb 16, 2024, 11:28:00 AM2/16/24
to Stripe API Discussion
I've been going around and around with Stripe support on this.  I'm new to Stripe, so forgive me if I don't use the correct nomenclature.

It appears that for a one-time checkout, whether for a product or a subscription, I will receive a Checkout Session Completed object.

What I would like from that object is the product id of the product/service the user purchased.  But, amazingly, i the tests I've done so far, the product id is not included.  So I asked Stripe how could I retrieve the product id for a product a user has just purchased.  I was told 

In cases where you need information from a linked object, you can retrieve the linked object in a new call using its ID. However, this approach requires two API requests to access just one value. If you need information from multiple linked objects, each would also require separate requests, which all adds to the latency and complexity of your application.

The API has an Expand feature that allows you to retrieve linked objects in a single call, effectively replacing the object ID with all its properties and values. For example, say you wanted to access details on a customer tied to a given Checkout Session. You would retrieve the Checkout Session and pass the customer property to the expand array, which tells Stripe to include the entire Customer object in the response:


curl -G https://api.stripe.com/v1/checkout/sessions/{{SESSION_ID}} \ -u "sk_test_4eC39HqLyjWDarjtT1zdp7dc:" \ -d "expand[]"=customer

I'm not sure I understand this.  Below is a sample checkout session completed object   Could someone please tell me how I would go about constructing a curl request to get the product id for this object?

Checkout Session Completed

    {
      "id": "evt_1OMOv7JRE4EJdR2wHuSl3V50",
      "object": "event",
      "api_version": "2023-10-16",
      "created": 1702360457,
      "data": {
        "object": {
          "id": "cs_test_a1SGHM8PBa51O75YzSvnZFoiZF9LUqAkwQrYHk6I84FkxZO8K7K2N3mfEv",
          "object": "checkout.session",
          "after_expiration": null,
          "allow_promotion_codes": false,
          "amount_subtotal": 1500,
          "amount_total": 1500,
          "automatic_tax": {
            "enabled": false,
            "status": null
          },
          "billing_address_collection": "auto",
          "cancel_url": "https://stripe.com",
          "client_reference_id": null,
          "client_secret": null,
          "consent": null,
          "consent_collection": {
            "payment_method_reuse_agreement": null,
            "promotions": "none",
            "terms_of_service": "none"
          },
          "created": 1702360332,
          "currency": "usd",
          "currency_conversion": null,
          "custom_fields": [
          ],
          "custom_text": {
            "after_submit": null,
            "shipping_address": null,
            "submit": null,
            "terms_of_service_acceptance": null
          },
          "customer": "cus_PAkPVMHG9JMQDo",
          "customer_creation": "if_required",
          "customer_details": {
            "address": {
              "city": null,
              "country": "US",
              "line1": null,
              "line2": null,
              "postal_code": "90210",
              "state": null
            },
            "email": "us...@email.com",
            "name": "Ron Parker",
            "phone": "+123 436-7890",
            "tax_exempt": "none",
            "tax_ids": [
            ]
          },
          "customer_email": null,
          "expires_at": 1702446732,
          "invoice": "in_1OMOv4JRE4EJdR2wNVuPKlZ9",
          "invoice_creation": null,
          "livemode": false,
          "locale": "auto",
          "metadata": {
          },
          "mode": "subscription",
          "payment_intent": null,
          "payment_link": "plink_1OMN1XJRE4EJdR2wFDgO2U1f",
          "payment_method_collection": "always",
          "payment_method_configuration_details": null,
          "payment_method_options": null,
          "payment_method_types": [
            "card"
          ],
          "payment_status": "paid",
          "phone_number_collection": {
            "enabled": true
          },
          "recovered_from": null,
          "setup_intent": null,
          "shipping_address_collection": null,
          "shipping_cost": null,
          "shipping_details": null,
          "shipping_options": [
          ],
          "status": "complete",
          "submit_type": "auto",
          "subscription": "sub_1OMOv4JRE4EJdR2wl7hoZVSg",
          "success_url": "https://stripe.com",
          "total_details": {
            "amount_discount": 0,
            "amount_shipping": 0,
            "amount_tax": 0
          },
          "ui_mode": "hosted",
          "url": null
        }
      },
      "livemode": false,
      "pending_webhooks": 1,
      "request": {
        "id": null,
        "idempotency_key": null
      },
      "type": "checkout.session.completed"
    }

Much appreciated!

Remi J.

unread,
Feb 16, 2024, 11:36:06 AM2/16/24
to api-d...@lists.stripe.com
Hey Ron,

The Checkout Session object has the `line_items` property [1]. This is a List object in our API that contains all the Price(s) that were included for your end customer to pay. Most people will have only one or two Prices but some will have a lot more. That's what you want to look at! Now, there's a wrinkle here and that property is not in the Event payload you shared! It's expected and I'll explain why below.

To improve the performance of our API, there are properties that are not returned by default in the API. Instead, you have to use our Expand feature [2] to explicitly ask us to include them in the response. Those properties are called "includable" and that's what you can see on the API Reference for `line_items`. We give some code example specifically for the Checkout + `line_items` use-case. here [3].

What you need to do here is, once you receive the `checkout.session.completed` Event, you extract the id `cs_live_123` and then you call the Retrieve Checkout Session API [4] and explicitly expand that property using `expand: ['line_items']`. This will then give you the full Checkout Session object with the `line_items` property for you to inspect.

Hope this helps!
Remi


--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Ron Parker

unread,
Feb 16, 2024, 4:41:08 PM2/16/24
to Stripe API Discussion, re...@stripe.com
Yes, it does.  Finally figured out how this works:  https://stripe.com/docs/expand

Once I realized where to find the session id and what "-u" meant (secret key), I was off to the races.  Now getting the product id from the expanded checkout.session.completed event.

Thank you!

-Ron

Reply all
Reply to author
Forward
0 new messages