|
1
|
|
|
<?php |
|
2
|
|
|
namespace Overtrue\Socialite\Providers; |
|
3
|
|
|
|
|
4
|
|
|
use Overtrue\Socialite\AccessTokenInterface; |
|
5
|
|
|
use Overtrue\Socialite\ProviderInterface; |
|
6
|
|
|
use Overtrue\Socialite\User; |
|
7
|
|
|
|
|
8
|
|
|
use Payment\Utils\ArrayUtil; |
|
9
|
|
|
use Payment\Utils\Rsa2Encrypt; |
|
10
|
|
|
use Payment\Utils\StrUtil; |
|
11
|
|
|
|
|
12
|
|
|
class AlipayProvider extends AbstractProvider implements ProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected $baseUrl = 'https://openapi.alipay.com/gateway.do'; |
|
16
|
|
|
|
|
17
|
|
|
public function getTokenUrl() |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->baseUrl; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getAuthUrl($state) |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->baseUrl; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function getTokenFields($code) |
|
29
|
|
|
{ |
|
30
|
|
|
$params = []; |
|
31
|
|
|
$params['app_id'] = $this->clientId; |
|
32
|
|
|
$params['method'] = 'alipay.system.oauth.token'; |
|
33
|
|
|
$params['format'] = 'JSON'; |
|
34
|
|
|
$params['charset'] = 'utf-8'; |
|
35
|
|
|
$params['sign_type'] = 'RSA2'; |
|
36
|
|
|
$params['timestamp'] = date('Y-m-d H:i:s'); |
|
37
|
|
|
$params['version'] = '1.0'; |
|
38
|
|
|
$params['grant_type'] = 'authorization_code'; |
|
39
|
|
|
$params['code'] = $code; |
|
40
|
|
|
$params = ArrayUtil::arraySort($params); |
|
41
|
|
|
$str = ArrayUtil::createLinkstring($params); |
|
42
|
|
|
$rsa = new Rsa2Encrypt(StrUtil::getRsaKeyValue($this->clientSecret, 'private')); |
|
43
|
|
|
$params['sign'] = $rsa->encrypt($str); |
|
44
|
|
|
|
|
45
|
|
|
return $params; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getAccessToken($code) |
|
49
|
|
|
{ |
|
50
|
|
|
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ |
|
51
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
52
|
|
|
'query' => $this->getTokenFields($code), |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$result = json_decode($response->getBody(), true); |
|
56
|
|
|
if (isset($result['alipay_system_oauth_token_response'])) { |
|
57
|
|
|
$result = $result['alipay_system_oauth_token_response']; |
|
58
|
|
|
if (isset($result['access_token'])) { |
|
59
|
|
|
return $this->parseAccessToken($result); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new \Exception('alipay.system.oauth.token:'.$response->getBody()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getUserByToken(AccessTokenInterface $token) |
|
67
|
|
|
{ |
|
68
|
|
|
$params = []; |
|
69
|
|
|
$params['app_id'] = $this->clientId; |
|
70
|
|
|
$params['method'] = 'alipay.user.info.share'; |
|
71
|
|
|
$params['format'] = 'JSON'; |
|
72
|
|
|
$params['charset'] = 'utf-8'; |
|
73
|
|
|
$params['sign_type'] = 'RSA2'; |
|
74
|
|
|
$params['timestamp'] = date('Y-m-d H:i:s'); |
|
75
|
|
|
$params['version'] = '1.0'; |
|
76
|
|
|
$params['auth_token'] = $token->getToken(); |
|
77
|
|
|
|
|
78
|
|
|
$params = ArrayUtil::arraySort($params); |
|
79
|
|
|
$str = ArrayUtil::createLinkstring($params); |
|
80
|
|
|
$rsa = new Rsa2Encrypt(StrUtil::getRsaKeyValue($this->clientSecret, 'private')); |
|
81
|
|
|
$params['sign'] = $rsa->encrypt($str); |
|
82
|
|
|
|
|
83
|
|
|
$response = $this->getHttpClient()->get($this->baseUrl, [ |
|
84
|
|
|
'query' => array_filter($params), |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
$result = json_decode($response->getBody(), true); |
|
88
|
|
|
if (isset($result['alipay_user_info_share_response'])) { |
|
89
|
|
|
$result = $result['alipay_user_info_share_response']; |
|
90
|
|
|
if (isset($result['code']) && $result['code'] == '10000') { |
|
91
|
|
|
unset($result['code'], $result['msg']); |
|
92
|
|
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw new \Exception('alipay.user.info.share:'.$response->getBody()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function mapUserToObject(array $user) |
|
100
|
|
|
{ |
|
101
|
|
|
return new User([ |
|
102
|
|
|
'id' => $this->arrayItem($user, 'user_id'), |
|
103
|
|
|
'name' => $this->arrayItem($user, 'nick_name'), |
|
104
|
|
|
'nickname' => $this->arrayItem($user, 'nick_name'), |
|
105
|
|
|
'avatar' => $this->arrayItem($user, 'avatar'), |
|
106
|
|
|
'email' => null |
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |