Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish clover #1168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions monorepo-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ parameters:
src/Cheddar: '[email protected]:SocialiteProviders/Cheddar.git'
src/ClaveUnica: '[email protected]:SocialiteProviders/ClaveUnica.git'
src/Clerk: '[email protected]:SocialiteProviders/Clerk.git'
src/Clover: '[email protected]:SocialiteProviders/Clover.git'
src/Cognito: '[email protected]:SocialiteProviders/Cognito.git'
src/Coinbase: '[email protected]:SocialiteProviders/Coinbase.git'
src/ConstantContact: '[email protected]:SocialiteProviders/ConstantContact.git'
Expand Down
13 changes: 13 additions & 0 deletions src/Clover/CloverExtendSocialite.php
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): void
{
$socialiteWasCalled->extendSocialite(Provider::IDENTIFIER, Provider::class);
}
}
126 changes: 126 additions & 0 deletions src/Clover/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace SocialiteProviders\Clover;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
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(): array
{
return [
'environment',
];
}

/**
* Get the access token response for the given code.
*
* @param string $code
*/
public function getAccessTokenResponse($code): array
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::JSON => $this->getTokenFields($code),
]);

return json_decode($response->getBody(), true);
}

protected function getApiDomain(): string
{
return match ($this->getConfig('environment')) {
'sandbox' => 'sandbox.dev.clover.com',
default => 'www.clover.com',
};
}

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase(
sprintf('https://%s/oauth/v2/authorize', $this->getApiDomain()),
$state
);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl(): string
{
$domain = match ($this->getConfig('environment')) {
'sandbox' => 'apisandbox.dev.clover.com',
default => 'api.clover.com',
};

return sprintf('https://%s/oauth/v2/token', $domain);
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(sprintf(
'https://%s/v3/merchants/%s/employees/%s',
$this->getApiDomain(),
$this->request->query('merchant_id'),
$this->request->query('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,
]);
}

/**
* Get the expires in from the token response body.
*
* @param array $body
* @return string
*/
protected function parseExpiresIn($body)
{
return (string) (Arr::get($body, 'access_token_expiration') - time());
}
}
72 changes: 72 additions & 0 deletions src/Clover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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::class.'@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 these tokens somewhere for use with the API.
$token = $user->accessTokenResponseBody;

// Here’s what it looks like:
// [
// 'access_token' => 'JWT',
// 'access_token_expiration' => 1709563149,
// 'refresh_token' => 'clvroar-6e49ffe9b5122f137aa39d8f7f930558',
// 'refresh_token_expiration' => 1741097349,
// ]

// You may also want to store the merchant ID somewhere.
$merchantId = request()->input('merchant_id');

// Here’s what it looks like:
// 'ABC123DEF4567'
});
```
33 changes: 33 additions & 0 deletions src/Clover/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "socialiteproviders/clover",
"description": "Clover OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"keywords": [
"clover",
"laravel",
"oauth",
"provider",
"socialite"
],
"authors": [
{
"name": "macbookandrew",
"homepage": "https://andrewrminion.com"
}
],
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/clover"
},
"require": {
"php": "^8.1",
"ext-json": "*",
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Clover\\": ""
}
}
}
Loading