#slimhakz-checkout-metadata

1 messages ยท Page 1 of 1 (latest)

twin crescentBOT
#

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.

winter crane
#

Hey

craggy kestrel
#

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?

winter crane
#

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"
]
]
],
],
]);

craggy kestrel
#

And what event are you expecting to see the metadata on?

#

Can you share the event id where you're not seeeing it?

winter crane
#

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

twin crescentBOT
craggy kestrel
#

Ah, that is because you're setting the metadata on the Checkout object.

winter crane
#

So how do I do it?? :/

craggy kestrel
winter crane
#

So do I still need the checkout object?

craggy kestrel
#

What do you mean by that?

winter crane
#

I don't know how to set it ๐Ÿ˜ฆ

craggy kestrel
digital vortex
#

slimhakz-checkout-metadata

winter crane
#

I'm really not understanding im so sorry

#

Under "Create a session" it has a metadata associative array...

digital vortex
#

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'
    ],
  ],
]);```
winter crane
#

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"

digital vortex
#

ask away here! I'll reply, just the server is a bit busy so might take a few minutes

winter crane
#

Sure, thanks

#

can this be removed?

digital vortex
#

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

winter crane
#

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

digital vortex
#

that doc will walk you through it

winter crane
#

I have done this and it doesn't work lol

#

/stripe/success.php?session_id=cs_test_a1dQex2pHF9aL9HgxcC6w79ScLICwphSfFtXZjsyRjm7l0Rr3CIc3IBbnj

#

error 500

digital vortex
#

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

winter crane
#

<?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

digital vortex
#

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

winter crane
#

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 ๐Ÿ˜ฆ

digital vortex
#

the code works totally fine you're just going way too fast and missing things

winter crane
#

I've copied the code from the website word for word

#

The success_url is set as per the green line

digital vortex
#

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

winter crane
#

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

digital vortex
#

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 ๐Ÿ™‚

winter crane
#

: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

digital vortex
#

You're almost there, you're just going too fast ๐Ÿ™‚

winter crane
#

Got the session data dumped ๐Ÿ™‚

#

Just gotta dump ["metadata"]=> object(Stripe\StripeObject)#48

digital vortex
#
{
    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

winter crane
#

So pretty ๐Ÿ˜„

#

Thank you ๐Ÿ™‚

#

Weirdly... metadata is not filled hmmm

digital vortex
#

Remember: metadata is per object.

winter crane
#

Yes, so should be in the "session"

digital vortex
#

Earlier I showed you how to put the metadata on the PaymentIntent. It's separate from the metadata on the Session

winter crane
#

ah

#

dumped ๐Ÿ˜‰

digital vortex
#

did you figure it out?

winter crane
#

uh huh ๐Ÿ™‚

digital vortex
#

nice!

winter crane
#

$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

digital vortex
#

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

winter crane
#

Awesome thank you ๐Ÿ™‚

#

You've both been so helpful

#

I will keep this open for now and will contionue tomorro wgetting late!

#

thank you again!!