#emily24-checkout-description
1 messages ยท Page 1 of 1 (latest)
// Set API key
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);
// Fetch the Checkout Session to display the JSON result on the success page
try {
$checkout_session = \Stripe\Checkout\Session::retrieve($session_id);
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $checkout_session){
// Retrieve the details of a PaymentIntent
try {
$intent = \Stripe\PaymentIntent::retrieve($checkout_session->payment_intent);
} catch (\Stripe\Exception\ApiErrorException $e) {
$api_error = $e->getMessage();
}
// Retrieves the details of customer
try {
// Create the PaymentIntent
$customer = \Stripe\Customer::retrieve($checkout_session->customer);
} catch (\Stripe\Exception\ApiErrorException $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $intent){
// Check whether the charge is successful
if($intent->status == 'succeeded'){
// Customer details
$name = $customer->name;
$email = $customer->email;
// Transaction details
$transactionID = $intent->id;
$paidAmount = $intent->amount;
$paidAmount = ($paidAmount/100);
$paidCurrency = $intent->currency;
$paymentStatus = $intent->status;
$paymentItemNumber = $intent->description;
```
{
"id": "li_1Ja3W2HhaDPa8eVPudjB6QM3",
"object": "item",
"adjustable_quantity": null,
"description": "237",```
I need to get description from line_items
$paymentItemNumber = $intent->description;
this is returning nothing at all
$paidAmount = $intent->amount;
$paidAmount = ($paidAmount/100);
$paidCurrency = $intent->currency;
$paymentStatus = $intent->status;```
these work and return datasets
so, hang on
the line_items are on the session, not the payment intent
are you just trying to get them in the wrong place?
To get those session line items, you'll need to retrieve the session itself and use expansion:
https://stripe.com/docs/api/expanding_objects
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
where you've got $intent->description; would be getting the description form the payment intent, no the session line items
$session = \Stripe\Checkout\Session::retrieve('cs_test_123', [
'expand' => ['line_items']
]);
you could also use this request ot expand the customer and payment intent and the same time, too ๐
let me try that and see what I break next ๐
keep in mind too that the line items are a list/array, so you'll need to traverse the index
item 0, item 1 ...etc
this applies to retrieving metadata as well?
metadata should be included by default for each object
All I want to do is pass an identifier so I know what the product id is after the checkout is complete
'price_data' => [
'product_data' => [
'name' => $productName,
'metadata' => [
'pro_id' => $productId
]
], ```
seems it's a line_item too
Hello! The code snippet above looks like it's from creation of a Checkout Session where you're specifying new Product data (which will create a new Product in Stripe every time). Can you tell me more about your use case/what you're using Checkout to charge people for?
yes
one moment
https://www.codexworld.com/stripe-checkout-payment-gateway-integration-php/ I largely followed this implementation
but the issue is all the price\item is static
basically they can goto the page and decide how much for a particular "item"
So you need dynamic amounts defined by the customer for your integration?
yes i'm able to dynamically set the price the problem is I have no idea what item it is for
without returning a product identifier after checkout
I recommend you add the product identifier to the Checkout Session as metadata.
it already is I see it in the stripe test data
but I need to take further action once the checkout is complete
Can you give me the ID of a Checkout Session with the metadata set so I can see what you're seeing? It's the ID that starts with cs_
In Stripe a Product has one or more Prices, and you use Checkout to charge people for one or more of those Prices. You can create a Checkout Session with Product and Price data to create Products and Prices on the fly (for dynamic amounts), but the Checkout Session's line items will only show the Prices, not the Products they belong to.
Thinking about it more, if you're selling multiple Products/Prices in a single Checkout Session, you would probably want to add the Product ID you need as metadata on the Price, not the Product. That way you can access it directly on the Checkout Session with line_items expanded: https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-price-metadata
Yeah, in that Checkout Session you're setting the metadata on the Product, not the Price. If you change it so you're setting it on the Price I think it will work the way you want.
Wait a second... can you set metadata on the Price when creating a Checkout Session...
Oh, wow, you can't.
Hm, okay.
Going to flag that internally.
Okay, so scratch setting metadata on the Price, you should probably set it on the Checkout Session itself: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-metadata
{
"id": "li_1Ja3W2HhaDPa8eVPudjB6QM3",
"object": "item",
"adjustable_quantity": null,
"description": "237",
"discount_amounts": [
],```
seems I set it on the description successfully
"description": "237", <===
The description is visible to the Customer. If that's okay with you that will work too.
sure it doesn't bother me at all
it's just a product id
long as they can't change it ๐
Nope, they can't change it! ๐
that's one thing I wasn't sure about pre-purchase and wanted to ask
because it's using javascript it seems
What is?
wasn't sure if they could manipulate the request
Your PHP code is creating the Checkout Session and setting the description, right?
yes and handling the price to make sure they don't do anything undesirable
but I wasn't sure if it was just echo'd and handled by stripe js
where they could set any product id paired with that price
No, they can't modify the description on the Checkout Session object in Stripe. They could modify the Checkout page to display something different client-side, but that change wouldn't impact anything server side.
No worries at all, we're here to help!
so after the checkout I want to fetch this description as seen #887792505989496852 message
$paymentItemNumber = $intent->description;
didn't seem to work
which brings me full circle
https://www.codexworld.com/stripe-checkout-payment-gateway-integration-php/ yeah it looks like this abomination see success.php
i'll have to look at it closely who knows how secure it is I just want a test that works
Try this:
$checkout_session = \Stripe\Checkout\Session::retrieve([
'id' => $session_id,
'expand' => [
'line_items'
],
]);
$first_description = $checkout_session->line_items->data[0]->description;
You need to retrieve the Checkout Session with line_items expanded and then traverse the object down to the description.
You would change data[0] to data[1] for the second description, data[2] for the third, etc.
Or you can loop over data to handle them all.
Does that make sense?
yes let me see if that works thanks
are you guy the in the videos on youtube? ๐
your avatar looks familiar hah
hah my mistake
thanks I'll give this a shot ๐
phew that works thank you so much
Awesome!
i know you're doing your job but a working code example geez
best support I've ever experienced hah
That's great to hear, thank you!
Anytime! Have a great night!