Home > Mobile >  Getting the line_items from Stripe webhook
Getting the line_items from Stripe webhook

Time:08-04

I have a script that retrieves a webhook event that was working well. With stripe new API, the sku of my product doesn't show anymore. Until now, I was getting the webhook event in PHP like this :

$event = \Stripe\Webhook::constructEvent(
    $payload,
    $sig_header,
    $secretEndpoint
);

and then

$event->data->object->display_items[0]->sku->id

As I'm reading the documentation, I think I have to expand the line_items object (don't see anything related to line_items in webhook request btw) : https://stripe.com/docs/api/expanding_objects?lang=php

But don't understand how to implement this in my PHP enpoint.

For further informations, the payment is made on a Checkout page, the button that redirect to this page is the classic shortcode given by Stripe, and contains the sku of my products :

 stripe.redirectToCheckout({
 lineItems: [{
 price: 'sku_MB4fzaySUCJC9T',
 quantity: 1
 }],
 mode: 'payment',

CodePudding user response:

You don't mention what object you are trying to retrieve line_items from so I'm going to assume it's a Checkout Session object. This answer will still apply if that is not correct but the API you need to hit will be different.

So, webhook events do not support expansion by themselves. You will need to retrieve the object from the API using the ID in the $event->data->object->id. This is where you can specify that you want to expand the [line_items](https://stripe.com/docs/api/checkout/sessions/object?lang=php#checkout_session_object-line_items.

I tested the code below both with and without the expansion parameter and can confirm the line_items property is returned when passing the expand parameter:

$checkout = $stripe->checkout->sessions->retrieve(
    $checkoutId,
    ['expand' => ['line_items']]
);

echo $checkout;

CodePudding user response:

Actually just found what's happening.

I'm still using stripe API version from 2018, this can be changed manually on stripe dashboard.

That being said, when you create a new product in Stripe, the shorcode provided is a shorcode that fits the most recent version of the API,

My endpoint works well with the older type of shortcode :

stripe.redirectToCheckout({
items: [{sku: 'sku_MB1fzaySUCJC9T',
quantity: 1}],

but not with the new one :

 stripe.redirectToCheckout({
 lineItems: [{
 price: 'sku_MB1fzaySUCJC9T',
 quantity: 1
 }],
 mode: 'payment',

Just modified to use the old fashion way.

  • Related