#slimhakz-checkout-metadata
1 messages ยท Page 1 of 1 (latest)
Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- slimhakz, 5 hours ago, 18 messages
Hey
Hi, what do you mean by 'it's noworking'? What event are you listening to? Can you share the event where the metadata is not sent?
Sure I can share you the code
the cost works, but the metadata is not populated against the transaction.
\Stripe\Stripe::setApiKey($stripe_secret_key);
$checkout_session = \Stripe\Checkout\Session::create([
'customer_email' => $Customer_Email,
'metadata' => [ "BookingID" => $Customer_BookingID, "CustomerEmail" => $Customer_Email ],
'mode' => 'payment',
'metadata' => [
'order_id' => '6735'
],
'success_url' => $Success_URL,
'cancel_url' => 'https://example.com/cancel',
'line_items' => [
[
"quantity" => 1,
"price_data" => [
"currency" => "gbp",
"unit_amount" => 100,
"product_data" => [
"name" => "Booking Deposit"
]
]
],
],
]);
And what event are you expecting to see the metadata on?
Can you share the event id where you're not seeeing it?
No not easily i'm afraid sorry
I'm programming this in PHP, using the stripe API
As above i have created the checkout, all the otherfields are populating, but the metadata field for the session is not being stored in the "payments" section for the order on my stripe dashboard
Ah, that is because you're setting the metadata on the Checkout object.
So how do I do it?? :/
You'd want to pass the metadata on the Payment Intent object, https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
So do I still need the checkout object?
What do you mean by that?
I don't know how to set it ๐ฆ
When you create the checkout session, you can pass it here: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
slimhakz-checkout-metadata
I'm really not understanding im so sorry
Under "Create a session" it has a metadata associative array...
Totally fine, we're here to help! But we do need you to try things as the developer writing the code. You asked how to have metadata to appear on the PaymentIntent that the Checkout Session creates. It's a separate API resource so you need to configure the Checkout Session to have the right metadata on that object.
Right now you added metadata to the Session itself. You want to add it to the PaymentIntent so you pass payment_intent_data as a parameter (like price_data) and in it you put your metadata
$checkout_session = \Stripe\Checkout\Session::create([
// ... everything else
'payment_intent_data' => [
'metadata' => [
'order_id' => '6735'
],
],
]);```
I'm so dumb hahahaha
So sorry
Perfect
It works straight away thank you so much.
Am I allowed to ask additional questions in this chat or do I have to create new "ticket"
ask away here! I'll reply, just the server is a bit busy so might take a few minutes
It can, you control the PaymentMethod(s) to show in your Dashboard. Highly recommend keeping it, it helps with conversions from Customers, more and more people use it
Ok will leave it for now
Thank you ๐ and lastly
When I complete the checkout I return to "success.php"
But I want it to display the meta data
I have done this and it doesn't work lol
/stripe/success.php?session_id=cs_test_a1dQex2pHF9aL9HgxcC6w79ScLICwphSfFtXZjsyRjm7l0Rr3CIc3IBbnj
error 500
Error 500 simply means you have a bug in your code. Extremely common crash for any PHP developers. So the next step is to debug your code you wrote as the developer. Add clear logs, catch errors (see https://stripe.com/docs/error-handling) and figure out what the problem is
<?php
require 'vendor/autoload.php';
$stripe = new \Stripe\StripeClient('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
try {
$session = $stripe->checkout->sessions->retrieve($_GET['session_id']);
$customer = $stripe->customers->retrieve($session->customer);
echo "<h1>Thanks for your order, $customer->name!</h1>";
http_response_code(200);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
?>
This is the code as per the website exactly
please pause
Try to breathe, add clear logs, debug things. You're the developer here. You own the code.
I can help but I won't just fix the issue for you in seconds. You have to first figure out the problem or try to at least
PHP Fatal error: Uncaught Error sending request to Stripe: (Status 404) (Request req_UqT1wdGmzphle4) No such checkout.session: cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u
Stripe\Exception\InvalidRequestException: No such checkout.session: cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u
I understand I appreciate all of your help, just a little confusing when the example code on the documentation does not work ๐ฆ
the code works totally fine you're just going way too fast and missing things
I've copied the code from the website word for word
The success_url is set as per the green line
You are trying to retrieve the Checkout Session cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u on your Stripe account, but it was created on a completely different Stripe account. You likely mixed up your API keys somewhere
I will check thank you
Sorry pasted the wrong error..
[08-Feb-2024 00:03:39 Europe/London] PHP Fatal error: Uncaught Stripe\Exception\InvalidArgumentException: The resource ID cannot be null or whitespace. in /home/xxxxxx/google.co.uk/stripe/vendor/stripe/stripe-php/lib/Service/AbstractService.php:105
Stack trace:
#0 /home/xxxxxx/google.co.uk/stripe/vendor/stripe/stripe-php/lib/Service/CustomerService.php(277): Stripe\Service\AbstractService->buildPath()
#1 /home/xxxxxx/google.co.uk/stripe/success.php(13): Stripe\Service\CustomerService->retrieve()
#2 {main}
thrown in /home/xxxxxx/google.co.uk/stripe/vendor/stripe/stripe-php/lib/Service/AbstractService.php on line 105
$customer = $stripe->customers->retrieve($session->customer);
Because there is no customer.... DUR oh god i'm sorry hah
Kepy dying at that line
yeah sorry I'm quite strict when helping here, so I mostly let your rubber duck yourself and will answer if you have a real actionable question ๐
:S
๐
No best way to learn thank you!
Just trying to figure out how I can retrieve the metadata I posted and order lines.
Thank you for all of your help
You're almost there, you're just going too fast ๐
Got the session data dumped ๐
Just gotta dump ["metadata"]=> object(Stripe\StripeObject)#48
{
echo "$extraString<br><pre>\n" . json_encode($obj, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) . "\n</pre><br>";
}```
that's my helper method, I have this in a config PHP file and I can use it to dump all Stripe API resources
it makes it nicer than var_dump and equivalent
Remember: metadata is per object.
Yes, so should be in the "session"
Earlier I showed you how to put the metadata on the PaymentIntent. It's separate from the metadata on the Session
did you figure it out?
uh huh ๐
nice!
$session = $stripe->checkout->sessions->retrieve($_GET['session_id']);
//$customer = $stripe->customers->retrieve($session->customer);
echo "<h1>Thanks for your order, $customer->name!</h1>";
stripeDump($session);
// Get Metadata and dump
$pi = $session->payment_intent;
$x = $stripe->paymentIntents->retrieve($pi, []);
Great little function you write thank you so much
Protip: metadata is super poweful but people mistake it often. When I debug this I usually put different mtadata like this:
// ... everything else
// metadata for Session
'metadata' => [
'where' => 'This is the Session metadata',
'order_id' => '6735',
],
// metadata for the resulting PaymentIntent
'payment_intent_data' => [
'metadata' => [
'where' => 'This is the PaymentIntent metadata',
'order_id' => '6735'
],
],
]);```
If you do ^ you'll see the different metadata on the objects and it will "click"
Also, you don't need to fetch the Session and then fetch the PaymentIntent. You can get both at once using a feature called Expand: https://stripe.com/docs/expand
$session = $stripe->checkout->sessions->retrieve(
$_GET['session_id'],
[
'expand' => ['customer', 'payment_intent'],
]
);
If you do this code, and dump the Session you will see that instead of customer: 'cus_123' you get the full Customer object and same for the PaymentIntent