Completed
Pull Request — master (#479)
by Andrey
02:39
created

Paypal::getAccessTokenEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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