#Josef - PHP

1 messages ยท Page 1 of 1 (latest)

open cliff
#

Hi ๐Ÿ‘‹

#

Can you share a webhook event ID?

stark slate
#

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

open cliff
#

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?

stark slate
#

When I log the $c_isLive variable it is empty or something like " "

open cliff
#

I am asking you to log the $event->data->livemode value and see if that contains a boolean

stark slate
#

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

open cliff
#

Okay but when you attempt to echo the data->livemode property you don't get a boolean value?

stark slate
open cliff
#

That is pretty confusing. You can see the value right there in your screenshot.

stark slate
#
// 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)

open cliff
#

And your first line in the echo statement is returning "Livemode: LivemodeB: "?

stark slate
#

yes

open cliff
#

And if you try one more level of depth $event->data->object->livemode what do you see?

stark slate
next magnet
#

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

stark slate
next magnet
#

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

stark slate
#

echo "\n direct livemove: " . $event -> livemode;

returns nothing
Still does not work

next magnet
#

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?

stark slate
#

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";
next magnet
#
$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)

stark slate
#

Got Event: 'evt_1Lb50iIrXZ5Do56HKakVcTnN' with type: 'checkout.session.completed' and livemode: '' - that's all

#

I have an idea what could be wrong

next magnet
#

ack good, thinking too, I've never seen this before. Testing locally to make sure I don't miss anything obvious

stark slate
next magnet
#

okay I am seeing the same thing so I'm confused too now. This is becoming personal

stark slate
#

๐Ÿ˜‚

next magnet
#
$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 ๐Ÿคฆโ€โ™‚๏ธ

stark slate
#

I am considering my programming language choises aswell

next magnet
#

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 ๐Ÿ˜…

stark slate
#

Somehow it is still returning nothing for me.
Well its already late in timezone, I will try to implement it again tomorrow with more sleep and get back to you

#

It was nice talking to you, thanks for the help!