Paypal::parseAccessTokenResponse()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
c 0
b 0
f 0
rs 9.1111
cc 6
nc 5
nop 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * PayPal service.
15
 *
16
 * @author Flávio Heleno <[email protected]>
17
 *
18
 * @see https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/
19
 */
20
class Paypal extends AbstractService
21
{
22
    /**
23
     * Defined scopes.
24
     *
25
     * @see https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/
26
     * @see  #attributes
27
     */
28
    const SCOPE_OPENID = 'openid';
29
    const SCOPE_PROFILE = 'profile';
30
    const SCOPE_PAYPALATTRIBUTES = 'https://uri.paypal.com/services/paypalattributes';
31
    const SCOPE_EMAIL = 'email';
32
    const SCOPE_ADDRESS = 'address';
33
    const SCOPE_PHONE = 'phone';
34
    const SCOPE_EXPRESSCHECKOUT = 'https://uri.paypal.com/services/expresscheckout';
35
36
    public function __construct(
37
        CredentialsInterface $credentials,
38
        ClientInterface $httpClient,
39
        TokenStorageInterface $storage,
40
        $scopes = [],
41
        ?UriInterface $baseApiUri = null
42
    ) {
43
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
44
45
        if (null === $baseApiUri) {
46
            $this->baseApiUri = new Uri('https://api.paypal.com/v1/');
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAuthorizationEndpoint()
54
    {
55
        return new Uri('https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getAccessTokenEndpoint()
62
    {
63
        return new Uri('https://api.paypal.com/v1/identity/openidconnect/tokenservice');
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getAuthorizationMethod()
70
    {
71
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function parseAccessTokenResponse($responseBody)
78
    {
79
        $data = json_decode($responseBody, true);
80
81
        if (null === $data || !is_array($data)) {
82
            throw new TokenResponseException('Unable to parse response.');
83
        } elseif (isset($data['message'])) {
84
            throw new TokenResponseException('Error in retrieving token: "' . $data['message'] . '"');
85
        } elseif (isset($data['name'])) {
86
            throw new TokenResponseException('Error in retrieving token: "' . $data['name'] . '"');
87
        }
88
89
        $token = new StdOAuth2Token();
90
        $token->setAccessToken($data['access_token']);
91
        $token->setLifeTime($data['expires_in']);
92
93
        if (isset($data['refresh_token'])) {
94
            $token->setRefreshToken($data['refresh_token']);
95
            unset($data['refresh_token']);
96
        }
97
98
        unset($data['access_token'], $data['expires_in']);
99
100
        $token->setExtraParams($data);
101
102
        return $token;
103
    }
104
}
105