#Reitrac
1 messages ยท Page 1 of 1 (latest)
Hi there, you want to see the line_items in the response of checkout session creation request?
Here is the result of my $session :
`StripeCheckoutSession JSON: {
"id": "cs_test_a1F3jaU8TqWep2uLdkkkCGS09qamceBGDtIbfnKJB69M4tQkcUdftLvFy9",
"object": "checkout.session",
"after_expiration": null,
"allow_promotion_codes": null,
"amount_subtotal": 3500,
"amount_total": 3500,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_address_collection": null,
"cancel_url": "https://whisky-district.monagraphic-dev.com/stripe-php/public/cancel.html",
"client_reference_id": null,
"consent": null,
"consent_collection": null,
"created": 1668586633,
"currency": "eur",
"customer": null,
"customer_creation": "if_required",
"customer_details": {
"address": {
"city": null,
"country": "FR",
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": "nicolas@monagraphic.com",
"name": "Cartier",
"phone": null,
"tax_exempt": "none",
"tax_ids": []
},
"customer_email": null,
"expires_at": 1668673033,
"livemode": false,
"locale": null,
"metadata": [],
"mode": "payment",
"payment_intent": "pi_3M4goXC1ozQJGJcc1pvHvkZk",
"payment_link": null,
"payment_method_collection": "always",
"payment_method_options": [],
"payment_method_types": [
"card"
],
"payment_status": "paid",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"setup_intent": null,
"shipping_address_collection": null,
"shipping_cost": null,
"shipping_details": null,
"shipping_options": [],
"status": "complete",
"submit_type": null,
"subscription": null,
"success_url": "https://whisky-district.monagraphic-dev.com/stripe-php/public/test.php",
....
`
to put it simply, after a payment, I need to synchronize the stripe data in my back-office
Sure, you can expand the ['line_items'] when creating a checkout session
And here is my line items :
$checkout_session = \Stripe\Checkout\Session::create([ 'line_items' => [ [ 'quantity' => 1, 'price_data' => [ 'currency' => 'eur', 'unit_amount' => $prixTotal, 'tax_behavior' => 'inclusive', 'product_data' => [ 'name' => $session, ], ], ]], 'mode' => 'payment', 'payment_method_types' => ['card'], 'success_url' => $YOUR_DOMAIN . '/test.php', 'cancel_url' => $YOUR_DOMAIN . '/cancel.html', ]);
So that the line_items will be included in the response.
https://stripe.com/docs/api/expanding_objects?lang=ruby#expanding_objects this is how you expand response
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
i tried to create new line :
'product' => $id in 'price_data' but it doesnt works
i need to get product_id from checkout_session_id
I read the doc but nothing tell me how to do that :/
or from pi_ID
Hi! I'm taking over this thread.
hello Soma
I think you want to get the line_item in the Checkout Session object (that contains the price and producst)? https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items
If so, you need to retrieve the Checkout Session with expand: ["line_item"] you can learn more about expand here: https://stripe.com/docs/expand
In the checkout session object, there is no product information
how to expand product ?
nothing in your doc
I just explained: retrieve the Checkout Session with expand: ["line_item"]
Then the Checkout Session object will contain line_item with information about the price and product.
For example the product ID is in line_items.data.price.product
How expand if i have no ch_ id ?
The Charge (ch_xxx) is stored in the PaymentIntent. So you have to expand the payment_intent property: https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-payment_intent
i tried this :
`$session = \Stripe\Checkout\Session::retrieve([
'id' => 'cs_test_a1qm4EOtdLccSTccBDUOnKZb1EWZOTHXrCCf6Wyllr6WGGeV2RuZLc6Y0O',
'expand' => ['customer', 'product'],
]);
$product = $session->product;
echo $product;`
But return nothing
It should be 'expand' => ['customer', 'line_item'] and then $product = $session->line_items->data[0]->price->product;
its dosnt works :/
here is my webhook :
`case 'checkout.session.completed':
$session = $event->data->object;
$checkout_id = $event->data->object->id;
$prix = $session = $event->data->object->amount_total;
$email = $session = $event->data->object->customer_details->email;
$nom = $session = $event->data->object->customer_details->name;
$idproduit = $event->data->object->payment_intent;
\Stripe\Stripe::setApiKey('');
$session = \Stripe\Checkout\Session::retrieve([
'id' => $checkout_id,
'expand' => ['customer', 'line_item'],
]);
$product = $session->line_items->data[0]->price->product;
$sql = "INSERT INTO stripe (transaction, prix, statut, email, nom, id_produit, checkout_session, test) VALUES ('123456789', '".$prix."', 'Complรฉtรฉ', '".$email."', '".$nom."', '".$idproduit."', '".$checkout_id."', '".$product."')";
$conn->exec($sql);
break;`
Please edit your message above to remove your sk_test_***
SInce i added expand my sql queries not writing in my data base
its dosnt works :/
Can you share the request ID (req_xxx)? Here's how you can find it: https://support.stripe.com/questions/finding-the-id-for-an-api-request
req_JMfevt7xpsZiuK
this is the last entry
i just need to get my product name after payment, why its so hard ? ๐ฆ
and the req from session :
req_5DI24ENLEc1eME
and the content of POST :
{ "mode": "payment", "line_items": { "0": { "price_data": { "currency": "eur", "unit_amount": "3500", "tax_behavior": "inclusive", "product_data": { "name": "20221116095353d71a4b" } }, "quantity": "1" } },
So i'll try : $product = $session->line_items->data[0]->price_data->product_data->name;
I meant the request Id when you retrieve the Checkout Session with expand.
So I guess this is the request that gives you an error? req_Vo8l2Snx15I1jN
๐ taking over for my colleague. Let me catch up.
yes let me take a look
our API changed recently where you can no longer expand on line_items but instead you can retrieve them separately https://stripe.com/docs/api/checkout/sessions/line_items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
would you mind sharing the response you got?
just console.log the whole response and share it here
my code is in webhook.php i cant access it
this page is running by your API
i wrote in my database :
StripeCollection JSON: { "object": "list", "data": [ { "id": "li_1M4j5VC1ozQJGJccv19u5Bnw", "object": "item", "amount_discount": 0, "amount_subtotal": 3500, "amount_tax": 0, "amount_total": 3500, "currency": "eur", "description": "20221116104319033777", "price": { "id": "price_1M4j5VC1ozQJGJccpbZ6Bl28", "object": "price", "active": false, "billing_scheme": "per_unit", "created": 1668595405, "currency": "eur", "custom_unit_amount": null, "livemode": false, "lookup_key": null, "metadata": [], "nickname": null, "product": "prod_MoLnqW27mryOsK", "recurring": null, "tax_behavior": "inclusive", "tiers_mode": null, "transform_quantity": null, "type": "one_time", "unit_amount": 3500, "unit_amount_decimal": "3500" }, "quantity": 1 } ], "has_more": false, "url": "/v1/checkout/sessions/cs_test_a1gJ1U5nKhvjaVCLBNyyI68jFYRPCm9Kvo3EbLKF1tv82uADJSesmozyAK/line_items" }
so name is 'descirption' ?
ok in that case what you need is to expand on data.price.product
in the retrieve line_items call
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
$line_items = $stripe->checkout->sessions->allLineItems('', ['limit' => 5, 'expand'=> ['data.price.product']]);
i think i found the solution :
$line_items = $stripe->checkout->sessions->allLineItems($checkout_id, ['limit' => 5]); $product = $line_items->data[0]->description;
This return me my product name
yes that's also true
but if you had multiple items on the Checkout then that becomes invalid
i'll never add multiple items
ok fair enough
In my front office, i calcul total = unit_price * quantity and return this total in line_items with quantity 1 forced ๐
Last question, can i get $session in success_url to create an order summary ?
you can get the session id yes
tahnks i'll try id ๐
Why, when i try to use API, this return me :
Fatal error: Uncaught Error: Class 'Stripe\StripeClient' not found
Here is my code in my success.php file :
$stripe = new \Stripe\StripeClient("***"); $line_items = $stripe->checkout->sessions->allLineItems($session, ['limit' => 5]); $transaction = $line_items->data[0]->description;
which stripe-php version are you using?
i answered my own question, forgot to load init.php ๐