Stripe API - Can not retrieve products purchased from a pricing table using webhook or api

734 views Asked by At

From the Stripe dashboard, I create some Products (standard pricing, one-time purchase), and then I create a Pricing Table that includes those products.

I then setup a webhook from Stripe to send the charge.succeeded event, the payment_intent.succeeded event and the checkout.session.completed event to my back end server.

When a product from the pricing table is purchased, I successfully receive the above webhook events.

But looking thru these events, there is no product information transmitted with any of the above events, so I do not know what product was purchased.

Each of the above event objects includes id's for other objects, so using the Stripe API I can retreive these other objects (like customer object), but I am still unable to find any product information in these other objects as well.

So I can retreive the payment_intent object, the charge object, the checkout_session object, and the customer object, but I am unable to figure out how to get to the product that was actually purchased.

NOTE: It looks like the checkout_session object id is the one I need (products purchased are called 'line items'), but for some reason, when I retrieve a Checkout Session object using 'expand line_items', it will not return any line items... I just get the exact same normal checkout session object as if I did not use expand line_items. According to the Stripe API docs, this is supposed to work, but it is not working for me. Per the Stripe API docs, this should work: https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items

Code examples for PHP ... all of these calls below simply return the normal Checkout Session object with no line items:

\Stripe\Stripe::setApiKey('stripe_api_secret_key');

$get_line_items = \Stripe\Checkout\Session::retrieve( 'cs_checkout_session_object_id', [ 'expand' => ['line_items'] ] );
                        
$get_line_items = \Stripe\Checkout\Session::retrieve( 'cs_checkout_session_object_id', [ 'expand' => ['data.line_items'] ] );

$get_line_items = \Stripe\Checkout\Session::retrieve( 'cs_checkout_session_object_id', [ 'expand' => ['line_items.data'] ] );
2

There are 2 answers

0
jsherk On BEST ANSWER

I could not get ['expand' => ['line_items']] to work, but I was able to retrieve the products that a customer purchased in a checkout_session using the allLineItems call in PHP (other languages use listLineItems or list_line_items calls).

The Stripe API docs are here for Retrieve a Checkout Session's line items:

https://stripe.com/docs/api/checkout/sessions/line_items

EDIT: I have resolved the issue with 'expand line items'...

All the Stripe docs now refer to the newer interface method, but the old interface method is still available. However when using the old interface method, there were some quirks in how certain requests are made. Below is the correct way, in PHP, to make these calls using the old interface method.

// Setup for old interface method. 
\Stripe\Stripe::setApiKey('stripe_api_secret_key');

// Expand line_items with retrieve checkout_session object. Note the poistion of square brackets and inclusion of 'id' to make this call work.
$get_line_items = \Stripe\Checkout\Session::retrieve(['id'=>'cs_test_123','expand'=>['line_items'], ]);

// Retrieve all line_items object. Note: check that your stripe-sdk-php version is new enough to support the allLineItems call.
$get_line_items = \Stripe\Checkout\Session::allLineItems('cs_test_123');
3
pgs On

To attain the product that was purchased via a CheckoutSession, you can look at the line_items property. This is what they call an includable property where it’s not included by default. So, you’d need to use the expand request parameter when you retrieve the CheckoutSession. Here is a cURL request from their documentation:

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