|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SocialiteProviders\Paymenter; |
| 4 | + |
| 5 | +use GuzzleHttp\RequestOptions; |
| 6 | +use InvalidArgumentException; |
| 7 | +use SocialiteProviders\Manager\OAuth2\AbstractProvider; |
| 8 | +use SocialiteProviders\Manager\OAuth2\User; |
| 9 | + |
| 10 | +class Provider extends AbstractProvider |
| 11 | +{ |
| 12 | + public const IDENTIFIER = 'PAYMENTER'; |
| 13 | + |
| 14 | + protected $scopes = ['profile']; |
| 15 | + |
| 16 | + public static function additionalConfigKeys(): array |
| 17 | + { |
| 18 | + return ['base_url']; |
| 19 | + } |
| 20 | + |
| 21 | + protected function getBaseUrl() |
| 22 | + { |
| 23 | + $baseurl = $this->getConfig('base_url'); |
| 24 | + if ($baseurl === null) { |
| 25 | + throw new InvalidArgumentException('Missing base_url'); |
| 26 | + } |
| 27 | + |
| 28 | + return rtrim($baseurl, '/'); |
| 29 | + } |
| 30 | + |
| 31 | + protected function getAuthUrl($state): string |
| 32 | + { |
| 33 | + return $this->buildAuthUrlFromBase($this->getBaseUrl().'/oauth/authorize', $state); |
| 34 | + } |
| 35 | + |
| 36 | + protected function getTokenUrl(): string |
| 37 | + { |
| 38 | + return $this->getBaseUrl().'/api/oauth/token'; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected function getUserByToken($token) |
| 45 | + { |
| 46 | + $response = $this->getHttpClient()->get($this->getBaseUrl().'/api/oauth/me', [ |
| 47 | + RequestOptions::HEADERS => [ |
| 48 | + 'Authorization' => 'Bearer '.$token, |
| 49 | + ], |
| 50 | + ]); |
| 51 | + |
| 52 | + return json_decode((string) $response->getBody(), true); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function refreshToken($refreshToken) |
| 59 | + { |
| 60 | + $response = $this->getHttpClient()->post($this->getTokenUrl(), [ |
| 61 | + RequestOptions::FORM_PARAMS => [ |
| 62 | + 'client_id' => $this->clientId, |
| 63 | + 'client_secret' => $this->clientSecret, |
| 64 | + 'grant_type' => 'refresh_token', |
| 65 | + 'refresh_token' => $refreshToken, |
| 66 | + ], |
| 67 | + ]); |
| 68 | + |
| 69 | + return json_decode((string) $response->getBody(), true); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + protected function mapUserToObject(array $user) |
| 76 | + { |
| 77 | + return (new User)->setRaw($user)->map([ |
| 78 | + 'email' => $user['email'], |
| 79 | + 'email_verified' => !empty($user['email_verified_at']), |
| 80 | + 'name' => $user['first_name'], |
| 81 | + 'family_name' => $user['last_name'], |
| 82 | + 'groups' => $user['role_id'], |
| 83 | + 'id' => $user['id'], |
| 84 | + ]); |
| 85 | + } |
| 86 | +} |
0 commit comments