#danilo_dlr

1 messages · Page 1 of 1 (latest)

bronze ravineBOT
south rose
#

hi! that's the object of a webhook endpoint, which is a different concept from the event that is sent to the webhook endpoint(and it's the event that has the name/amount etc you want), so I think you're a but confused there

abstract wharf
#

thanks i will check it !

abstract wharf
#

ok with this guide now i get on my error log the information about the payment, like the id and amount.
If have 2 more questions now 😅

  1. if im ussing checkout.session.completed i only get the info if the payment if succesfull?
  2. and to send to the custome that succesfully paid and email, i guess i need to store the id, so when i send a post from my form to the stripe checkout, i get also the id to store on my db?
    for example, to the checkout php i get amount with the post i send from my form ( unit_amount = $_POST['amount']; ), on the checkout i have also the id? its generated automatically so i can store it with the information i get from the post? i hope this question is clear enough
south rose
#
  1. yes
abstract wharf
#

let me explain better the 2nd question:
i get the information trough post, and send it to the stripe checkout (like the unit_amount or currency) but i need to know from who is this information to store this data on a database so when i get the checkout.session.completed i can search on my db the email i stored and send to him an email, so for that i need the id, where do i get this information, on the checkout.php is already this id created?

south rose
#
  1. sure, you can save the ID of the Session cs_test_xxxx into your database at the time you create the Session, and then look it up when handling the webhook. You can also pass metadata when creating the CheckoutSession and use that in the same way
abstract wharf
#

if i save the amount on the stripe checkout php with unit_amount = $_POST['amount'];
how can i get the id? how can i asign the value and from where?

south rose
#

for example $id= $session->id; after you called something like $session = $stripe->checkout->sessions->create([....]);.

abstract wharf
#

the id I get with the session id is:

cs_test_a1EuAfrziPQsqVSE80TwKPhfa5H5RaSyFoGBxowI2p52ZGf7XlHw5zyOOD

and with the webhook the id i get is:

li_1NBb8HLAOCvZNGOdQWB7LJBC

stripe checkout code:
$checkout_session = \Stripe\Checkout\Session::create([ ***** ]);
$checkout_session->id;

webhook code:
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],

im checking 2 different values?

south rose
#

seems like you are checking two different values yes.

#

li_xxx is the ID of a line item. You didn't show the code that you're using that gives you that value so I can't say more about it.

The ID of the CheckoutSession is simply $session->id and will be cs_test_xxxx
it's the same value as $event->data->object->id, if your webhook has received an event of type checkout.session.completed, and it looks like cs_test_xxxx.

abstract wharf
#

im using the values you said: $session->id for the checkout and $event->data->object->id for the webhook, but im not getting the same string...

i send you more code in case you can find what is causing this:

--------webhook:
// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],
]);

$line_items = $session->line_items;
// Fulfill the purchase...
fulfill_order($line_items);
}

------------stripe checkout:
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price_data' => [
'currency' => 'clp',
'unit_amount' => $importe,
'product_data' => [
'name' => 'Donativo UNRWA',
'images' => ["sample.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'sample.php',
'cancel_url' => 'sample.html',
]);

then i connect to my db to send the data

$decodedData = json_decode($postData, true);
$foreign_id = $checkout_session->id;
$email_user = $_POST["email"];
$nombre = $_POST["nombre"];
$apellidos = $_POST["apellidos"]; ...

so im using $checkout_session->id; to store the id on my db (getting cs_test_a1EuAfrziPQsqVSE80TwKPhfa5H5RaSyFoGBxowI2p52ZGf7XlHw5zyOOD)
and i use 'id' => $event->data->object->id, on my webhook (its a copy paste of the code i get from https://stripe.com/docs/payments/checkout/fulfill-orders to show the id on my error_log)

south rose
#

and what's the issue exactly?

#

$event->data->object->id is definitely a value like "cs_test_xxx"

#

note you are only passing the line_items to your fulfill_order() function, so that function doesn't know the ID, you have lost that context. You might want to refactor the code

#

but like you have multiple ways to access the ID and do things with it:

  // Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
  print_r($event->data->object->id); // cs_test_xxx
  $session = \Stripe\Checkout\Session::retrieve([...]);
  print_r($session->id); // cs_test_xxx 
...
abstract wharf
#

as you can see on my log (created by the webhook) i get li_xxx not cs_test

Got error 'PHP message: Fulfilling order...PHP message: Stripe\Collection JSON: {\n "object": "list",\n "data": [\n {\n "id": "li_1NBc5aLAOCvZNGOdkzFg33XI",\n "object": "item",\n "amount_discount": 0,\n "amount_subtotal": 10000,\n "amount_tax": 0,\n "amount_total": 10000,\n "currency": "clp",\n "description": "Donativo UNRWA",\n "price": {\n "id": "price_1NBc5aLAOCvZNGOdoFBegcUI",\n "object": "price",\n "active": false,\n "billing_scheme": "per_unit",\n "created": 1685013134,\n "currency": "clp",\n "custom_unit_amount": null,\n "livemode": false,\n "lookup_key": null,\n "metadata": [],\n "nickname": null,\n "product": "prod_NmKw7iJyFYGJXE",\n "recurring": null,\n "tax_behavior": "unspecified",\n "tiers_mode": null,\n "transform_q...'

#

webhook:

try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}

function fulfill_order($line_items) {
// TODO: fill me in
error_log("Fulfilling order...");
error_log($line_items);
}

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],
]);

$line_items = $session->line_items;
// Fulfill the purchase...
fulfill_order($line_items);
}

http_response_code(200);

south rose
#

yeah

#

that's because you are logging the value of line_items

south rose
abstract wharf
#

so i have to change that for something else?

south rose
#

if you want the ID I showed you above how your code can access it

#

and then you can do anything with it, you can change the fulfill_order function to a call like fulfil_order($id,$line_items) and pass it the ID, or whatever works for you

bronze ravineBOT
strong stream
#

Hi! I'm taking over from my colleague. Please, let me know if you have any other questions.

abstract wharf
#

hi, i dont know if you read the code above, but i need to send to my error log the id of the checkout.session.completed that starts with cs_test_xxx i change a little the code of my webhook and now it just doesnt work 😪

function fulfill_order($line_items) {
// TODO: fill me in
error_log("Fulfilling order...");
error_log($line_items);
}
// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],
]);

your coworker said to change the fulfill_order function to fulfill_order($id, $line_items)
but i guess im doing it the wrong way, im stuck.

#

i changed the function to:

fulfill_order($id, $line_items)
and the called like

fulfill_order($id, $line_items);

function fulfill_order($id, $line_items) {
// TODO: fill me in
error_log("Fulfilling order...");
error_log($id, $line_items);
}

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],
]);
error_log($session->id);
$line_items = $session->line_items;
// Fulfill the purchase...
fulfill_order($id, $line_items);
}

and i get on my log
message: Fulfilling order...PHP message: PHP Warning: error_log() expects parameter 2 to be int, object given in

strong stream
#

What are you trying to do exactly?

abstract wharf
#

send to my error log trough my webhook the id that starts with cs_test_xxx when i get a succesfull purchase

strong stream
#

This looks like some PHP error, and not Stripe-related error: "PHP message: PHP Warning: error_log() expects parameter 2 to be int, object given in"

#

I'm not that familiar with PHP, is error_log a built-in function?

abstract wharf
#

yes,, its like console_log but for php and the message gets displayed on the server error_log file

#

i just wanna know whats the simpliest way to get the cs_test_xxx value from the webhook

strong stream
#

Can you do instead:

error_log($id);
error_log($line_items);
#

But I think you're looking for $session->id

abstract wharf
#

Sorry, it's already solved, it was easier than I thought

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
// Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
$session = \Stripe\Checkout\Session::retrieve([
'id' => $event->data->object->id,
'expand' => ['line_items'],
]);
error_log($session->id);

}

with that i get the cs_test_xxx i was looking for

Thanks for your hard work guys, this discord is awesome for getting help, you can close this ticket 💪 ♥️

strong stream
#

Glad to hear. Please, let me know if you have any other questions.