#Reitrac

1 messages ยท Page 1 of 1 (latest)

edgy nightBOT
quiet shale
#

Hi there, you want to see the line_items in the response of checkout session creation request?

cyan heron
#

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

quiet shale
#

Sure, you can expand the ['line_items'] when creating a checkout session

cyan heron
#

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

quiet shale
#

So that the line_items will be included in the response.

cyan heron
#

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

late tapir
#

Hi! I'm taking over this thread.

cyan heron
#

hello Soma

late tapir
cyan heron
#

In the checkout session object, there is no product information

#

how to expand product ?

#

nothing in your doc

late tapir
#

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

cyan heron
#

How expand if i have no ch_ id ?

late tapir
cyan heron
#

i tried this :

`$session = \Stripe\Checkout\Session::retrieve([
'id' => 'cs_test_a1qm4EOtdLccSTccBDUOnKZb1EWZOTHXrCCf6Wyllr6WGGeV2RuZLc6Y0O',
'expand' => ['customer', 'product'],
]);
$product = $session->product;

echo $product;`

#

But return nothing

late tapir
#

It should be 'expand' => ['customer', 'line_item'] and then $product = $session->line_items->data[0]->price->product;

cyan heron
#

ok

#

i'll try

cyan heron
#

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;`
late tapir
#

Please edit your message above to remove your sk_test_***

cyan heron
#

SInce i added expand my sql queries not writing in my data base

late tapir
cyan heron
#

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;

late tapir
#

I meant the request Id when you retrieve the Checkout Session with expand.

cyan heron
#

this one ? req_pmupak9YMSADWQ

#

I have 3 req for each payment

late tapir
#

So I guess this is the request that gives you an error? req_Vo8l2Snx15I1jN

steep bear
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

cyan heron
#

Hello

#

I want to get product name after payment

steep bear
#

yes let me take a look

cyan heron
#

hummmmm

#

there is no product name in the response ?

steep bear
#

would you mind sharing the response you got?

cyan heron
#

how ?

#

in logs ?

steep bear
#

just console.log the whole response and share it here

cyan heron
#

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

steep bear
#

ok in that case what you need is to expand on data.price.product

#

in the retrieve line_items call

cyan heron
#

how ?

#

Using? $product = $line_items->data->price->product;

steep bear
#
$line_items = $stripe->checkout->sessions->allLineItems('', ['limit' => 5, 'expand'=> ['data.price.product']]);
cyan heron
#

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

steep bear
#

yes that's also true

#

but if you had multiple items on the Checkout then that becomes invalid

cyan heron
#

i'll never add multiple items

steep bear
#

ok fair enough

cyan heron
#

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 ?

steep bear
#

you can get the session id yes

cyan heron
#

how ?

#

using $_POST ?

steep bear
cyan heron
#

tahnks i'll try id ๐Ÿ™‚

cyan heron
#

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;

steep bear
#

which stripe-php version are you using?

cyan heron
#

i answered my own question, forgot to load init.php ๐Ÿ™‚

steep bear
#

yeah that's useful ๐Ÿ˜…

#

let me know if you need any more help