-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 248741d
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\Clover; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class CloverExtendSocialite | ||
{ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite(Provider::IDENTIFIER, Provider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\Clover; | ||
|
||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
|
||
class Provider extends AbstractProvider | ||
{ | ||
/** | ||
* Unique Provider Identifier. | ||
*/ | ||
const IDENTIFIER = 'clover'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = []; | ||
|
||
/** | ||
* Indicates if the session state should be utilized. | ||
* | ||
* @var bool | ||
*/ | ||
protected $stateless = true; | ||
|
||
public static function additionalConfigKeys() | ||
{ | ||
return [ | ||
'environment', | ||
]; | ||
} | ||
|
||
protected function getApiDomain(): string | ||
{ | ||
return match ($this->getConfig('environment')) { | ||
'sandbox' => 'sandbox.dev.clover.com', | ||
default => 'www.clover.com', | ||
}; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase( | ||
sprintf('https://%s/oauth/v2/authorize', $this->getApiDomain()), | ||
$state | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
$domain = match ($this->getConfig('environment')) { | ||
'sandbox' => 'apisandbox.dev.clover.com', | ||
default => 'api.clover.com', | ||
}; | ||
|
||
return sprintf('https://%s/oauth/token', $domain); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$requestParams = str(request()->fullUrl()) | ||
->after('?') | ||
->explode('&') | ||
->mapWithKeys(fn (string $keyAndValue) => [str($keyAndValue)->before('=')->toString() => str($keyAndValue)->after('=')->toString()]); | ||
|
||
$response = $this->getHttpClient()->get(sprintf( | ||
'https://%s/v3/merchants/%s/employees/%s', | ||
$this->getApiDomain(), | ||
$requestParams['merchant_id'], | ||
$requestParams['employee_id'], | ||
), [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer '.$token, | ||
], | ||
]); | ||
|
||
return json_decode((string) $response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['id'], | ||
'nickname' => $user['name'], | ||
'name' => $user['name'], | ||
'email' => $user['email'], | ||
'avatar' => null, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Clover | ||
|
||
```bash | ||
composer require socialiteproviders/clover | ||
``` | ||
|
||
## Installation & Basic Usage | ||
|
||
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. | ||
|
||
Ensure the app has permission to read employees. | ||
|
||
### Add configuration to `config/services.php` | ||
|
||
```php | ||
'clover' => [ | ||
'client_id' => env('CLOVER_CLIENT_ID'), | ||
'client_secret' => env('CLOVER_CLIENT_SECRET'), | ||
'redirect' => env('CLOVER_REDIRECT_URI') | ||
'environment' => env('CLOVER_ENVIRONMENT', 'production'), | ||
], | ||
``` | ||
|
||
### Add provider event listener | ||
|
||
Configure the package's listener to listen for `SocialiteWasCalled` events. | ||
|
||
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. | ||
|
||
```php | ||
protected $listen = [ | ||
\SocialiteProviders\Manager\SocialiteWasCalled::class => [ | ||
// ... other providers | ||
'SocialiteProviders\\Clover\\CloverExtendSocialite@handle', | ||
], | ||
]; | ||
``` | ||
|
||
### Usage | ||
|
||
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): | ||
|
||
```php | ||
return Socialite::driver('clover')->redirect(); | ||
``` | ||
|
||
Presumably you are using this OAuth provider in order to retrieve an API token for calling other API endpoints. | ||
|
||
The user includes a `token` property that you can use to retrieve the API token like this: | ||
|
||
```php | ||
Route::get('clover/auth/callback', function () { | ||
$user = Socialite::driver('clover')->user(); | ||
|
||
// Save this token somewhere for other use. | ||
$token = $user->token; | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "socialiteproviders/clover", | ||
"description": "Clover OAuth2 Provider for Laravel Socialite", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "macbookandrew", | ||
"homepage": "https://andrewrminion.com" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.1", | ||
"ext-json": "*", | ||
"socialiteproviders/manager": "^4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\Clover\\": "" | ||
} | ||
} | ||
} |