#JCoDog-Checkout
1 messages · Page 1 of 1 (latest)
Hi! So you have a Checkout Session ID, and you would like to find the product and price ID associated with it?
So this is the code and at the bottom is an SQL statement I am trying to run to store the details about the purchase.
I cannot help you with your SQL queries. But if you are trying to find the price of a Checkout Session ID, then you can find it in the line_items property: https://stripe.com/docs/api/subscriptions/create#create_subscription-items-price_data
Note that this property is not available by default, you will need to expand it to see it, like this: https://stripe.com/docs/expand#includable-properties
Yes I have done that... Would it be lineitems->description?
I don't know if that will return the name of the product or the description of the product.
You would expand using 'line_items.data.price.product
And then you can find the product name in: line_items.data[].price.product.name
evt_1KmH5LEOJsqRXL0RHMNhGcfj getting this error
The event you shared is for a checkout.session.completed event, so I don't see any errors here.
it says invalid checkout session
if you look into the evenet
I dont know why the checkout session would be invalid as the test checkout sent the test webhook to the test endpoint and it tried to then retrieve the test checkout session with the test session ID
Can you share the request ID (req_xxx) that generated the error? Here's how you can find it: https://support.stripe.com/questions/finding-the-id-for-an-api-request
req_wwvVGL8HMFZtkf
Why is it saying invalid checkout session id if it says 200 ok in logs?
What code are you running, and exactly is the error? The request you just shared is to confirm a payment and it worked succesfully.
Response
200 OK
Event triggered.<br />
<b>Fatal error</b>: Uncaught (Status 404) (Request req_fjWkJwkWT1CXWq) Invalid checkout.session id: cs_test_b1BdNc2pvHNjtdT0cBkobFWfDubDsPWTy0Rcu5qoAH8xrjdZCPEUXnY9Rp
thrown in <b>/home/u201085483/domains/jconet.co.uk/public_html/resources/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php</b> on line <b>38</b><br />
// Handle the event
switch ($event->type) {
case 'checkout.session.completed':
echo("Event triggered.");
$session = $event->data->object;
$checkout = $stripe->checkout->sessions->retrieve(
$session->id,
['expand' => ['line_items']]
);
$items = $checkout->line_items->data;
foreach ($items as $item) {
// if ($item->price->product == 'prod_LFXPGLvHr6Q5Pq') {
if ($item->price->product == 'prod_Kx1vkcIuKc2pah') {
echo("Correct product selected.");
// add credits to the user
$custID = $checkout->customer;
$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
echo("Update.");
$bal = $creditDB[0]['creditBalance']+$item->quantity;
echo("newbal: ".$bal.".");
if(mysqli_query($conn, "UPDATE credits SET creditBalance=".$bal." WHERE userID=".$customerDB[0]['ID'])) {
echo("Update completed successfully.");
} else {
echo("Update failed.");
};
} else {
// insert
echo("Insert");
if(mysqli_query($conn, "INSERT INTO credits SET userID=".$_SESSION['userID'].", creditBalance=".$item->quantity)) {
echo("Insert completed successfully.");
} else {
echo("Insert failed.");
};
}
$sql = "INSERT INTO purchases SET checkoutID=".$session->id.", customerID=".$custID.", productName=".$item->description.", quantity=".$item->quantity.", priceTotal=".$item->price->unit_amount;
if (mysqli_query($conn, $sql)) {
echo("Purchase recorded");
} else {
echo("Failed to record purchase");
}
};
};
// ... handle other event types```
HI 👋 jumping in as my teammate needs to step away. You're trying to retrieve an object (Checkout Session) that resides in test mode with your live mode key. This is something that can't be done, the key used to retrieve an item must match the mode that it belongs to.
oops...
Yes ok so that worked. However the new issue is the SQL to save the details about the purchase didnt work.
We're not going to have any insight into your SQL. If I have spare time I can come back and try to help, but currently I need to focus on helping address the Stripe related questions that are being asked.
the statement is not incorrect, it is caused by something with the values being inserted from the stripe opjects.
Can you elaborate?
nevermind I found the issue and its pointing to something with the sql not reading the value as string when it needs to be a string