#JCoDog - Webhook
1 messages ยท Page 1 of 1 (latest)
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.
When you look in your Event logs in the dashboard, what events do you see?
Your webhook is only configured to listen to two events. What events are those?
success and failure
Of what?
Yeah that's not what will get triggered here. HEre's a list of all Stripe events. Take a look as there are likely better ones that apply here
https://stripe.com/docs/api/events/types
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.
Would the vscode extension be where that is best?
checkout.session.completed did not trigger the endpoint either
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
ok
This came up and I did change the event that is being listened to
No webhook ran
Okay so those are webhooks, using Stripe listen.
Yes but why is the webhook dash not showing them?
yes all test mode
Can you share the ID of your webhook?
we_1KHOm8EOJsqRXL0RoL6OCrNm
Thanks, taking a look
Will I have this issue when I use the live one later?
Okay I just logged in as you and I can see the 3 checkout.session.completed events for this webhook
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
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?
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
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.
Please check event ID evt_1KYy6HEOJsqRXL0R96Yrh0a9
Is this why?
The error log shows nothing new
Okay, right. There are no line items on that object
So trying to access them should result in a key error
I am seeing it
In the event response?
yes
it returns the checkout session id that then has the line items that I am getting
Right, because you turn around and retrieve it again.
So, after you retrieve it, can you try printing out the JSON just to verify the shape of the object?
how would I do that because I dont view the page that the webhook is on
I mean, in your code. when you return $checkout, try putting an echo $checkout in there to see what is happening.
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
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.
for checkout
[02-Mar-2022 19:53:10 UTC] Stripe Notice: Undefined property of Stripe\LineItem instance: data
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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)
I have done that
OK
This was talked about a few hours ago when i couldnt get the checkout to work
Apologies, i thought i reviewed the conversation and did not see that -- my mistake
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
What call leads to this error/warning?
Right, but that's the raw json structure
Can you echo out line_items from the parsed object?
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
OK, let me try to test this a moment
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;
}```
is there any clue why this is happening because i cant see why line_items->data is undefined
Still working to test ๐
Ok thanks. let me know I am going to work on setting up subscriptions. ๐
๐
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;