forked from SocialiteProviders/Clover
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProvider.php
More file actions
163 lines (142 loc) · 4.18 KB
/
Provider.php
File metadata and controls
163 lines (142 loc) · 4.18 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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',
'europe' => 'api.eu.clover.com',
'latin_america' => 'api.la.clover.com',
default => 'api.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',
'europe' => 'eu.clover.com',
'latin_america' => 'la.clover.com',
default => 'api.clover.com',
};
return sprintf('https://%s/oauth/v2/token', $domain);
}
protected function getRefreshUrl(): 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/refresh', $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());
}
/**
* Get the refresh token response for the given refresh token.
*
* @param string $refreshToken
* @return array
*/
protected function getRefreshTokenResponse($refreshToken)
{
$response = json_decode($this->getHttpClient()->post($this->getRefreshUrl(), [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::JSON => [
'refresh_token' => $refreshToken,
'client_id' => $this->clientId,
],
])->getBody(), true);
$response['expires_in'] = $this->parseExpiresIn($response);
return $response;
}
}