Get purchased product from PaymentIntent
Asked Answered
E

3

8

in my app i want to list past purchases for a stripe customer. So I was looking at the relevant API objects like PaymentIntents, Sessions or Charges. But they all do not seem to contain any reference to Product or Price, which I would need to list the purchased products.

Subscriptions contain a list of items that are contained in that subscription, so I was expecting PaymentIntents to have something like that too.

Does anyone has an idea how to archive my list of past purchases? Thanks!

Envenom answered 15/12, 2021 at 18:31 Comment(3)
How are you creating the purchases? PaymentIntent, Checkout Session, Charge? What are the arguments passed in?Backwoods
I am using stripe checkout, so I am only manually creating the Session Object, where I passs the customer and the Prices and some other parameters related to the checkout page. PaymentIntent and Charge are then created by the stripe-hosted checkout page. I think you can add a description to a PaymentIntent, where you could store the product ids or something like that but this seems like a hack for someething I think would be a common use case.Envenom
You can always append metadata to Stripe objects stripe.com/docs/api/metadata It's how lots of companies associate things like Order ID from their systems to the Stripe data.Backwoods
B
3

I did some digging through the Stripe API docs[1] and, out of the three objects you referrenced (PaymentIntent, Session, Charges), the only one that I can see being able to trace back to a product is the Session.

Session objects have a line_items property[2] which can be followed all the way down to line_items.data.price.product[3]. To access this you’ll need to inlcude the expand=["data.line_items"] parameter to your call to list Checkout Sessions. You can read more about expanding API responses here[4]

So for all the charges to your customers that were done using Checkout Sessions, you could list them all, use the customer property to associate earch session with a customer in your application, traverse the the returned data, and then query the API for the product details. If you have a lot of customers & products these API calls will add up fast so I would store this data in your back-end to avoid hitting rate limits[5].

Alternatively, you could just save the product ID (either Stripe or your local version) as metadata[6] for any of the above Stripe payment objects listed. That would allow you to link any payment object you wish to a product.


  1. https://stripe.com/docs/api
  2. https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items
  3. https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-price-product
  4. https://stripe.com/docs/expand
  5. https://stripe.com/docs/rate-limits
  6. https://stripe.com/docs/api/metadata
Backwoods answered 15/12, 2021 at 23:35 Comment(3)
Thank you Ryn! I wanted to test this solution but then I noticed, that you can not query for Sessions and filter by Customer. PaymentIntent and Charge both allow you to do that. So I would have to query all Sessions and then filter for Customer in my backend which would be kind of inefficient. I find it strange that there seems to be no easy way to archive that.Envenom
Yeah, I was looking for a customer filter as well but Sessions can't do that. In that case I would use the auto-pagination feature (stripe.com/docs/api/pagination/auto) to loop through all Sessions and assign the related products to customers in your back-end.Backwoods
This is so backwards... I mean... who would want their payment API to tell them just which product the payment was for 😅Aquarius
B
1

I solved by adding product info (eg. id, name, description) to the payment_intent_data.metadata in Stripe.Checkout.SessionCreateParams https://docs.stripe.com/api/checkout/sessions/create

When you retrieve the Charges using stripe.charges.list, you get those info in the metadata field.

Bramble answered 3/9, 2024 at 7:29 Comment(0)
S
-1

I had the same question I found a way to retrieve the products related to a specific PaymentIntent.

You need to get the sessions that was linked to PaymentIntent when the Checkout process was made. I will give you the code (in PHP) that I use to to this.

        \Stripe\Stripe::setApiKey(STRIPE_API_SECRET);
        // Find the Session for that PaymentIntent
        $sessions = \Stripe\Checkout\Session::all([
            'payment_intent' => "pi_xxxxxxxxxx",
            'expand' => ['data.line_items'],
        ]);

You will then have an key line_items that contain the products linked.

Enjoy

Surly answered 25/12, 2022 at 22:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.