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 #1169

Merged
merged 6 commits into from
Mar 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Clover/CloverExtendSocialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CloverExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled)
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite(Provider::IDENTIFIER, Provider::class);
}
Expand Down
57 changes: 40 additions & 17 deletions src/Clover/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SocialiteProviders\Clover;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

Expand All @@ -24,25 +26,40 @@ class Provider extends AbstractProvider
*/
protected $stateless = true;

public static function additionalConfigKeys()
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',
default => 'www.clover.com',
};
}

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase(
sprintf('https://%s/oauth/v2/authorize', $this->getApiDomain()),
Expand All @@ -53,31 +70,26 @@ protected function getAuthUrl($state)
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
protected function getTokenUrl(): string
{
$domain = match ($this->getConfig('environment')) {
'sandbox' => 'apisandbox.dev.clover.com',
default => 'api.clover.com',
default => 'api.clover.com',
};

return sprintf('https://%s/oauth/token', $domain);
return sprintf('https://%s/oauth/v2/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'],
$this->request->query('merchant_id'),
$this->request->query('employee_id'),
), [
'headers' => [
'Authorization' => 'Bearer '.$token,
Expand All @@ -93,11 +105,22 @@ protected function getUserByToken($token)
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'id' => $user['id'],
'nickname' => $user['name'],
'name' => $user['name'],
'email' => $user['email'],
'avatar' => null,
'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());
}
}
20 changes: 17 additions & 3 deletions src/Clover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`.
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
'SocialiteProviders\\Clover\\CloverExtendSocialite@handle',
\SocialiteProviders\Clover\CloverExtendSocialite::class.'@handle',
],
];
```
Expand All @@ -52,7 +52,21 @@ The user includes a `token` property that you can use to retrieve the API token
Route::get('clover/auth/callback', function () {
$user = Socialite::driver('clover')->user();

// Save this token somewhere for other use.
$token = $user->token;
// 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'
});
```
14 changes: 13 additions & 1 deletion src/Clover/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
"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.0"
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
Expand Down
Loading