#JCoDog - Webhook

1 messages ยท Page 1 of 1 (latest)

onyx axle
#

Can you describe what is not working?

smoky bloom
#

That is what I am not sure about, I ran the checkout and did the payment, but no webhook event was sent

#

The checkout code.

#

Everything works up to the payment then nothing.

#

and webhook code.

onyx axle
#

When you look in your Event logs in the dashboard, what events do you see?

smoky bloom
onyx axle
#

Your webhook is only configured to listen to two events. What events are those?

smoky bloom
#

success and failure

onyx axle
#

Of what?

smoky bloom
#

payment

onyx axle
smoky bloom
#

What is being triggered by the checkout session?

#

checkout.session.completed?

onyx axle
#

One of the best ways to test this out is to use stripe listen

#

Personally I use this all the time

#

Because I can walk through testing a checkout session and then see, in real time, all the events that are triggered.

smoky bloom
#

Would the vscode extension be where that is best?

#

checkout.session.completed did not trigger the endpoint either

onyx axle
#

Did you change the events your endpoint is listening to?

#

And I would just run stripe listen in a terminal window and watch the events stream in

smoky bloom
#

ok

smoky bloom
#

No webhook ran

onyx axle
#

Okay so those are webhooks, using Stripe listen.

smoky bloom
#

Yes but why is the webhook dash not showing them?

onyx axle
#

Are you running this is test mode?

#

Is your webhook running in test mode?

smoky bloom
#

yes all test mode

onyx axle
#

Can you share the ID of your webhook?

smoky bloom
#

we_1KHOm8EOJsqRXL0RoL6OCrNm

onyx axle
#

Thanks, taking a look

smoky bloom
#

Will I have this issue when I use the live one later?

onyx axle
#

Okay I just logged in as you and I can see the 3 checkout.session.completed events for this webhook

smoky bloom
#

then IDK because this should have updated...

#

[02-Mar-2022 19:15:48 UTC] Stripe Notice: Undefined property of Stripe\LineItem instance: data
[02-Mar-2022 19:17:43 UTC] Stripe Notice: Undefined property of Stripe\LineItem instance: data
[02-Mar-2022 19:20:11 UTC] Stripe Notice: Undefined property of Stripe\LineItem instance: data

onyx axle
#

Okay, that is a different question.

#

So, in this case you are getting completed checkout sessions. And now you are saying that your account balance is not reflecting the amount in the Session?

smoky bloom
#

yes

#

and the errors from the webhook being handles say line_items->data is not a thing

#

or even session

#

I built it from this guide but it says that data is not defined

onyx axle
#

Well this guide is using a payment intent. If you responded to the payment_intent.succeeded event, you would be getting the payment intent in your webhook event.

#

In this case your object is different. The first thing I would do is try to convert the object to JSON and print it out so you can examine the shape.

smoky bloom
#

Please check event ID evt_1KYy6HEOJsqRXL0R96Yrh0a9

#

Is this why?

#

The error log shows nothing new

onyx axle
#

Okay, right. There are no line items on that object

#

So trying to access them should result in a key error

smoky bloom
#

I am seeing it

onyx axle
#

In the event response?

smoky bloom
#

yes

#

it returns the checkout session id that then has the line items that I am getting

onyx axle
#

Right, because you turn around and retrieve it again.

smoky bloom
#

yes

onyx axle
#

So, after you retrieve it, can you try printing out the JSON just to verify the shape of the object?

smoky bloom
#

how would I do that because I dont view the page that the webhook is on

onyx axle
#

I mean, in your code. when you return $checkout, try putting an echo $checkout in there to see what is happening.

smoky bloom
#

Yes but that is in the webhook page...

#

I wont see the echo

#

the user is returned to a success page and the webhook runs on another page in the background on the server

onyx axle
#

This is why I would suggest using stripe listen to forward the event to a localhost endpoint where you can test this out and log the objects.

smoky bloom
#

for checkout

#

[02-Mar-2022 19:53:10 UTC] Stripe Notice: Undefined property of Stripe\LineItem instance: data

zenith vale
#

line_items is an includable attribute of the checkout session, it is not included by default in API requests unless you request it with expansion, ie expand[]=line_items
https://stripe.com/docs/api/expanding_objects
https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items

smoky bloom
zenith vale
#

Expansion is not supported/performed on webhook events, so this will never be included

#

If you need the session line_items you'll need to explicitly retrieve the session from the API after you receive the event

#

(and include the above-noted expansion in the request)

smoky bloom
#

I have done that

zenith vale
#

OK

smoky bloom
# smoky bloom

This was talked about a few hours ago when i couldnt get the checkout to work

zenith vale
#

Apologies, i thought i reviewed the conversation and did not see that -- my mistake

smoky bloom
#

previous support issue from this morning...

#

But the code has been said that I know that I have retrieved the right checkout session by putting it to echo out.

#

This is what was found in checkout

zenith vale
smoky bloom
zenith vale
#

Right, but that's the raw json structure

#

Can you echo out line_items from the parsed object?

smoky bloom
#

yes

#

evt_1KYyETEOJsqRXL0RfzisbkW7

#
    "object": "list",
    "data": [
        {
            "id": "li_1KYyEMEOJsqRXL0RlnViLP57",
            "object": "item",
            "amount_subtotal": 200,
            "amount_total": 200,
            "currency": "gbp",
            "description": "JCoNet Credits",
            "price": {
                "id": "price_1KH7rtEOJsqRXL0RUDVDJU5c",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1641997925,
                "currency": "gbp",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": "1500 credits",
                "product": "prod_Kx1vkcIuKc2pah",
                "recurring": null,
                "tax_behavior": "exclusive",
                "tiers_mode": null,
                "transform_quantity": {
                    "divide_by": 1500,
                    "round": "up"
                },
                "type": "one_time",
                "unit_amount": 200,
                "unit_amount_decimal": "200"
            },
            "quantity": 1500
        }
    ],
    "has_more": false,
    "url": "\/v1\/checkout\/sessions\/cs_test_b17cnmPJ1EfhOfkUVUFNFluKzBF0GfaNz2lGLlmugr9U5b17SiEFYtSAT9\/line_items"
}<br />
<b>Warning</b>:  Array to string conversion in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>54</b><br />
Array<br />
<b>Warning</b>:  Attempt to read property "id" on string in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>58</b><br />
<br />
<b>Warning</b>:  Undefined array key 0 in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>63</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>63</b><br />
<br />
<b>Warning</b>:  Undefined variable $_SESSION in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>73</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>73</b><br />
<br />
<b>Warning</b>:  Attempt to read property "quantity" on null in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>73</b><br />
Received unknown event type checkout.session.completed```Response to the webhook
zenith vale
#

OK, let me try to test this a moment

smoky bloom
#

ok

#

Do you need the full webhook code?

#
switch ($event->type) {
  case 'checkout.session.completed':
    $session = $event->data->object;
    $checkout = $stripe->checkout->sessions->retrieve(
      $session->id,
      ['expand' => ['line_items']]
    );
    echo $checkout->line_items;
    $items = $checkout->line_items->data;
    foreach ($items as $item) {
      if ($item->price->product == 'prod_Kx1vkcIuKc2pah') {
        // add credits to the user
        $custID = $checkout->customer->id;

        $result = mysqli_query($conn, "SELECT ID FROM customers WHERE stripeID ='".$custID."'");
        $customerDB = mysqli_fetch_all($result, MYSQLI_ASSOC);

        $balResult = mysqli_query($conn, "SELECT * FROM credits WHERE userID ='".$customerDB[0]['ID']."'");
        $num_rows = mysqli_num_rows($balResult);
        $creditDB = mysqli_fetch_all($balResult, MYSQLI_ASSOC);

        if ($num_rows > 0) {
          // update
          $bal = $creditDB[0]['creditBalance']+$item->data->quantity;
          mysqli_query($conn, "UPDATE credits SET creditBalance=".$bal);
        } else {
          // insert
          mysqli_query($conn, "INSERT INTO credits SET userID=".$_SESSION['userID'].", creditBalance=".$item->data->quantity);
        }
      };
    };
  // ... handle other event types
  default:
    echo 'Received unknown event type ' . $event->type;
}```
smoky bloom
#

is there any clue why this is happening because i cant see why line_items->data is undefined

zenith vale
#

Still working to test ๐Ÿ™‚

smoky bloom
#

Ok thanks. let me know I am going to work on setting up subscriptions. ๐Ÿ™‚

zenith vale
#

๐Ÿ‘

zenith vale
#

Was having some issues with my PHP environment but this all works as expected for me:

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

    echo count($sessionWithItems -> line_items);
    echo $sessionWithItems -> line_items->data[0];
    $items = $sessionWithItems -> line_items -> data;
    foreach($items as $item) {
        echo $item -> price -> product;
    }
#

What version of the library are you using?

#

I think your error is coming from another place:
$bal = $creditDB[0]['creditBalance']+$item->data->quantity;