#Josef - PHP
1 messages ยท Page 1 of 1 (latest)
sure
evt_1Lb50iIrXZ5Do56HKakVcTnN
switch ($event->type) {
case 'checkout.session.completed':
$c_isLive = $event -> livemode;
$session = $event->data->object;
$c_name = $session -> customer_details -> name;
$c_paymentIntentID = $session -> payment_intent;
Only the isLive key does not work I assume I am selecting the wron JSON depth
Hmmm... when I review the event I see the livemode: false property as belonging to the data object.
Can you try that and let me know if you can successfully return the boolean value?
When I log the $c_isLive variable it is empty or something like " "
I am asking you to log the $event->data->livemode value and see if that contains a boolean
Okay I tried that and it also returns nothing
and throws the error: Undefined property of Stripe\StripeObject instance: livemode
$event -> livemode
does not throw this error
Okay but when you attempt to echo the data->livemode property you don't get a boolean value?
Yes
$event -> livemode; | Returns nothing, no error
$event -> data -> livemode; | Returns nothing, error
That is pretty confusing. You can see the value right there in your screenshot.
// Handle the event
switch ($event->type) {
case 'checkout.session.completed':
$c_isLive = $event -> data -> livemode;
$c_isLiveB = $event -> livemode;
$session = $event->data->object;
$c_name = $session -> customer_details -> name;
$c_paymentIntentID = $session -> payment_intent;
$c_email = $session -> customer_details -> email;
//address
$c_line1 = $session -> customer_details -> address -> line1;
$c_line2 = $session -> customer_details -> address -> line2;
$c_postal_code = $session -> customer_details -> address -> postal_code;
$c_city = $session -> customer_details -> address -> city;
$c_country = $session -> customer_details -> address -> country;
echo "\n Livemode: " . $c_isLive . " LiveModeB: " . $c_isLiveB;
echo "\n The partyyy.app API succeesfully recieved data for customer: " . $c_email;
break;
default:
echo 'Received unknown event type ' . $event->type;
}
You can see the echo result in Stripe (Event ID: evt_1Lb50iIrXZ5Do56HKakVcTnN)
And your first line in the echo statement is returning "Livemode: LivemodeB: "?
yes
And if you try one more level of depth $event->data->object->livemode what do you see?
Returns nothing aswell and throws [26-Aug-2022 18:58:20 Europe/Berlin] Stripe Notice: Undefined property of Stripe\StripeObject instance: livemode
the property is top level, you access it the exact same way you access type or id
You clearly have $event->type here, if that works then $event->livemode also will works
My guess is $event -> data -> livemode; crashes because the API Resource is a Checkout Session and it does not have livemode
Thats what I thought aswell but somehow it does not
Are you sure about that?
My event JSON has
"livemode": false,
ah my bad, I was sure they didn't
Thats what I thought aswell but somehow it does not
I mean I'm sure it does. Just log both right before your switch and see what happens
echo "\n direct livemove: " . $event -> livemode;
returns nothing
Still does not work
what does "return nothing" mean though
it's a webhook endpoint, there's no return, there's nothing to echo
Can you share the beginning of your code where you deserialize the Event?
The line produces exctly this " direct livemove: "
maybe there is a space as result but nothing else
require_once('stripeconnector/stripe-php-9.2.0/init.php');
echo "require working! \n";
// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = '***********************';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
echo "stripe: invalid payload";
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
echo "stripe: invalid signature";
exit();
}
// Handle the event
switch ($event->type) {
case 'checkout.session.completed':
$c_isLive = $event -> livemode;
$session = $event->data->object;
$c_name = $session -> customer_details -> name;
$c_paymentIntentID = $session -> payment_intent;
$c_email = $session -> customer_details -> email;
//address
$c_line1 = $session -> customer_details -> address -> line1;
$c_line2 = $session -> customer_details -> address -> line2;
$c_postal_code = $session -> customer_details -> address -> postal_code;
$c_city = $session -> customer_details -> address -> city;
$c_country = $session -> customer_details -> address -> country;
echo "\n Livemode: " . $c_isLive;
echo "\n direct livemove: " . $event -> livemode;
echo "\n The API succeesfully recieved data for customer: " . $c_email;
break;
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
echo "stripe: working";
$payload = @file_get_contents('php://input');
$event_json = json_decode($payload);
echo "Got Event: '" . $event_json->id . "' with type: '" . $event_json->type . "' and livemode: '". $payload->livemode ."' - that's all";```
what does this get you (to insert where you have the first line)
Got Event: 'evt_1Lb50iIrXZ5Do56HKakVcTnN' with type: 'checkout.session.completed' and livemode: '' - that's all
I have an idea what could be wrong
ack good, thinking too, I've never seen this before. Testing locally to make sure I don't miss anything obvious
I am always resending an old webhook, thought that could be the cause
Now I created a new purchase but the results are still the same.
okay I am seeing the same thing so I'm confused too now. This is becoming personal
๐
$magicString = $event->livemode ? "this is live" : "this is test";
$msg = "Got Event: '" . $event_json->id . "' with type: '" . $event_json->type . "' and livemode: '" . $magicString . "' that's all.\n";```
okay that works for me
so it has to be something with the boolean concept
UGH
Damn I had never realized this happened. I assume when I hit this I just added an if and forgot. I use PHP every day and I never saw this ๐คฆโโ๏ธ
I am considering my programming language choises aswell
lol
every language has its quirks I'll continue to live and die by PHP
I wish I had a way to search my past attempts in my life. Like I'm sure I hit this before and basically went "huh?" and moved on instead of digging
But overall, all is good in the world, and we don't have a problem with events not sending livemode: false which I started to be super worried about ๐