-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProvider.php
113 lines (98 loc) · 3.02 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?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
{
public const IDENTIFIER = 'clover';
/**
* 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((string) $response->getBody(), true);
}
protected function getApiDomain(): string
{
return match ($this->getConfig('environment')) {
'sandbox' => 'sandbox.dev.clover.com',
'europe' => 'api.eu.clover.com',
'latin_america' => 'api.la.clover.com',
default => 'api.clover.com',
};
}
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase(sprintf('https://%s/oauth/v2/authorize', $this->getApiDomain()), $state);
}
protected function getTokenUrl(): string
{
$domain = match ($this->getConfig('environment')) {
'sandbox' => 'apisandbox.dev.clover.com',
'europe' => 'eu.clover.com',
'latin_america' => 'la.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'),
), [
RequestOptions::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());
}
}