diff --git a/src/Extensions/User.php b/src/Extensions/User.php index 886807c..a6efc9a 100644 --- a/src/Extensions/User.php +++ b/src/Extensions/User.php @@ -31,7 +31,14 @@ public function __construct( */ public function getProfilePic() { - return $this->user_info['profile_pic'] ?? null; + if (isset($this->user_info['profile_pic'])) { + return $this->user_info['profile_pic']; + } + + // Workplace (Facebook for companies) uses picture parameter + if (isset($this->user_info['picture'])) { + return $this->user_info['picture']['data']['url']; + } } /** @@ -47,7 +54,7 @@ public function getLocale() */ public function getTimezone() { - return $this->user_info['timezone'] ?? null; + return isset($this->user_info['timezone']) ? $this->user_info['timezone'] : null; } /** @@ -55,7 +62,7 @@ public function getTimezone() */ public function getGender() { - return $this->user_info['gender'] ?? null; + return isset($this->user_info['gender']) ? $this->user_info['gender'] : null; } /** @@ -63,7 +70,7 @@ public function getGender() */ public function getIsPaymentEnabled() { - return $this->user_info['is_payment_enabled'] ?? null; + return isset($this->user_info['is_payment_enabled']) ? $this->user_info['is_payment_enabled'] : null; } /** @@ -71,6 +78,6 @@ public function getIsPaymentEnabled() */ public function getLastAdReferral() { - return $this->user_info['last_ad_referral'] ?? null; + return isset($this->user_info['last_ad_referral']) ? $this->user_info['last_ad_referral'] : null; } } diff --git a/src/FacebookDriver.php b/src/FacebookDriver.php index f97b8bd..2ac32c9 100644 --- a/src/FacebookDriver.php +++ b/src/FacebookDriver.php @@ -383,7 +383,18 @@ public function isConfigured() */ public function getUser(IncomingMessage $matchingMessage) { - $userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields=first_name,last_name,profile_pic,locale,timezone,gender,is_payment_enabled,last_ad_referral&access_token='.$this->config->get('token')); + $messagingDetails = $this->event->get('messaging')[0]; + + // field string available at Facebook + $fields = 'first_name,last_name,profile_pic,locale,timezone,gender,is_payment_enabled,last_ad_referral'; + + // WORKPLACE (Facebook for companies) + // if community isset in sender Object, it is a request done by workplace + if (isset($messagingDetails['sender']['community'])) { + $fields = 'first_name,last_name,email,title,department,employee_number,primary_phone,primary_address,picture,link,locale,name,name_format,updated_time'; + } + + $userInfoData = $this->http->get($this->facebookProfileEndpoint.$matchingMessage->getSender().'?fields='.$fields.'&access_token='.$this->config->get('token')); $this->throwExceptionIfResponseNotOk($userInfoData); $userInfo = json_decode($userInfoData->getContent(), true); diff --git a/tests/FacebookWorkplaceUserTest.php b/tests/FacebookWorkplaceUserTest.php new file mode 100644 index 0000000..ef67040 --- /dev/null +++ b/tests/FacebookWorkplaceUserTest.php @@ -0,0 +1,106 @@ + '1234', + 'first_name' => 'Christine', + 'last_name' => 'Manning', + 'email' => 'christine.manning@example.com', + 'title' => 'Experimenter', + 'picture' => [ + 'data' => [ + 'height' => 50, + 'is_silhouette' => true, + 'url' => 'http://profilepic.com', + 'width' => 50, + ], + ], + 'link' => 'http://workplace-link.facebook.com/app_scoped_user_id/100014245873942/', + 'locale' => 'en_US', + 'name' => 'Christine Manning', + 'name_format' => '{first} {last}', + 'updated_time' => '2016-11-24T06:37:15+0000', + ]; + + $user = new User( + '1234', + 'Christine', + 'Manning', + null, + $userInfo + ); + + return $user; + } + + public function testFirstName() + { + $user = $this->createTestUser(); + + $this->assertEquals('Christine', $user->getFirstName()); + } + + public function testLastName() + { + $user = $this->createTestUser(); + + $this->assertEquals('Manning', $user->getLastName()); + } + + public function testUsername() + { + $user = $this->createTestUser(); + + $this->assertNull($user->getUsername()); + } + + public function testProfilePic() + { + $user = $this->createTestUser(); + + $this->assertEquals('http://profilepic.com', $user->getProfilePic()); + } + + public function testLocale() + { + $user = $this->createTestUser(); + + $this->assertEquals('en_US', $user->getLocale()); + } + + public function testTimezone() + { + $user = $this->createTestUser(); + + $this->assertNull($user->getTimezone()); + } + + public function testGender() + { + $user = $this->createTestUser(); + + $this->assertNull($user->getGender()); + } + + public function testIsPaymentEnabled() + { + $user = $this->createTestUser(); + + $this->assertNull($user->getIsPaymentEnabled()); + } + + public function testLastAdReferral() + { + $user = $this->createTestUser(); + + $this->assertNull($user->getLastAdReferral()); + } +}