Passed
Push — master ( 4f79b1...15f90b )
by wannanbigpig
05:41 queued 10s
created

Client::getAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 6
rs 9.9332
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\BaseService\Auth;
12
13
use EasyAlipay\Kernel\Support\Support;
14
15
class Client extends Support
16
{
17
    /**
18
     * alipay.system.oauth.token(换取授权访问令牌).
19
     *
20
     * @param string $code
21
     * @param string $grantType
22
     *
23
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
24
     *
25
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
26
     * @throws \GuzzleHttp\Exception\GuzzleException
27
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
28
     */
29
    public function getAccessToken(string $code, string $grantType = 'authorization_code')
30
    {
31
        $method = 'alipay.system.oauth.token';
32
33
        $param = [];
34
35
        if ($grantType !== 'authorization_code') {
36
            $param['refresh_token'] = $code;
37
        } else {
38
            $param['code'] = $code;
39
        }
40
41
        $params = array_merge([
42
            'grant_type' => $grantType,
43
        ], $param);
44
45
        return $this->request($method, $params);
46
    }
47
48
    /**
49
     * get authorization url.
50
     *
51
     * @param string      $redirectUri
52
     * @param string      $scope
53
     * @param string|null $state
54
     *
55
     * @return string
56
     */
57
    public function getAuthorizationUrl(string $redirectUri, string $scope = 'auth_base', string $state = null)
58
    {
59
        $appid = $this->app->config->get('sys_params.app_id');
60
        $params = [
61
            'app_id' => $appid,
62
            'scope' => $scope,
63
            'redirect_uri' => $redirectUri,
64
            'state' => $state ?: $scope,
65
        ];
66
67
        $url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?';
68
69
        return $url.http_build_query($params);
70
    }
71
72
    /**
73
     * alipay.user.info.share(支付宝会员授权信息查询接口).
74
     *
75
     * @param string $authToken
76
     *
77
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
78
     *
79
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
80
     * @throws \GuzzleHttp\Exception\GuzzleException
81
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
82
     */
83
    public function getUserInfo(string $authToken)
84
    {
85
        $method = 'alipay.user.info.share';
86
        $params = [
87
            'auth_token' => $authToken,
88
        ];
89
90
        return $this->request($method, $params);
91
    }
92
93
    /**
94
     * alipay.user.info.auth(用户登陆授权).
95
     *
96
     * @param string      $state
97
     * @param string      $scopes
98
     * @param string|null $returnUrl
99
     *
100
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
101
     *
102
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
103
     * @throws \GuzzleHttp\Exception\GuzzleException
104
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
105
     */
106
    public function loginAuthorization(string $state, string $scopes = 'auth_base', string $returnUrl = null)
107
    {
108
        $method = 'alipay.user.info.auth';
109
        $params = [
110
            'scopes' => $scopes,
111
            'state' => $state,
112
        ];
113
        $this->app->setEndpointConfig($method, [
114
            'return_url' => $returnUrl ?: $this->app['config']->get('return_url'),
115
        ]);
116
117
        return $this->request($method, [
118
            'biz_content' => $params,
119
        ]);
120
    }
121
}
122