1<?php
2
3namespace OAuth\OAuth2\Service;
4
5use OAuth\OAuth2\Token\StdOAuth2Token;
6use OAuth\Common\Http\Exception\TokenResponseException;
7use OAuth\Common\Http\Uri\Uri;
8use OAuth\Common\Consumer\CredentialsInterface;
9use OAuth\Common\Http\Client\ClientInterface;
10use OAuth\Common\Storage\TokenStorageInterface;
11use OAuth\Common\Http\Uri\UriInterface;
12
13/**
14 * PayPal service.
15 *
16 * @author Flávio Heleno <flaviohbatista@gmail.com>
17 * @link https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/
18 */
19class 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