#jcodog-webhook-issue
1 messages · Page 1 of 1 (latest)
@upper fable hello! I'm happy to help but I'm going to need a lot more specific information about what exactly is not working, what your code is doing, what's not set as expected, etc.
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->data;
$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['ID']."'");
$num_rows = mysqli_num_rows($balResult);
$creditDB = mysqli_fetch_all($balResult, MYSQLI_ASSOC);
if ($num_rows > 0) {
// update
$bal = $creditDB['creditBalance']+$item->quantity;
mysqli_query($conn, "UPDATE credits SET creditBalance=".$bal);
} else {
// insert
mysqli_query($conn, "INSERT INTO credits SET userID=".$_SESSION['userID'].", creditBalance=".$item->quantity);
}
};
};```
I previously had an issue where I was being told lineitem data was not defined but that was fixed, now from the line $custID = $checkout->customer->id I start getting these errors coming up
<br />
<b>Warning</b>: Undefined array key "ID" in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/webhooks/payments.php</b> on line <b>62</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>72</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>72</b><br />
okay so that means you have multiple real PHP errors in your code
First, look at this: $checkout = $stripe->checkout->sessions->retrieve( $session->id, ['expand' => ['line_items']] );
what this does is taking the Session id cs_test_123 and then retrieving it from the API. You are passing expand to say "please also give me the line items!" right?
Yes, but I need the customer ID and that is erroring as all i have is customer...
The problem here is that if you look at our documentation for the Checkout Session object, you can see that the customer property is a string by default and it's expandable
Would I need to expand customer as well as line_items?
yes!
basically right now you get { id: 'cs_test_123', object: 'checkout.session', customer: 'cus_123', ... }
but what you want is { id: 'cs_test_123', object: 'checkout.session', customer: { id: 'cus_123', object: 'customer', email: 'myemail@stripe.com', ... }, ... }
and that is done with expand!
otherwise if you just want the id and not anything else
then change $custID = $checkout->customer->id; to $custID = $checkout->customer; instead
Ok expanded customer and all the errors went away
but from what I am seeing in your explanation instead of doing $checkout->customer->id I can just do $checkout->customer as the customer field before expanding is the ID?
exactly
you basically expand only when you need more information
since otherwise you'd get the id and then call https://stripe.com/docs/api/customers/retrieve which is slower/more work
but if all you care about is the customer id then the change you just mentioned is what you needed!