diff --git a/CloverExtendSocialite.php b/CloverExtendSocialite.php new file mode 100644 index 0000000..3247622 --- /dev/null +++ b/CloverExtendSocialite.php @@ -0,0 +1,13 @@ +extendSocialite(Provider::IDENTIFIER, Provider::class); + } +} diff --git a/Provider.php b/Provider.php new file mode 100644 index 0000000..548d5b4 --- /dev/null +++ b/Provider.php @@ -0,0 +1,103 @@ +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, + ]); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..299514d --- /dev/null +++ b/README.md @@ -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; +}); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b64340f --- /dev/null +++ b/composer.json @@ -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\\": "" + } + } +}