1<?php
2
3namespace OAuth\OAuth1\Service;
4
5use OAuth\OAuth1\Token\StdOAuth1Token;
6use OAuth\Common\Http\Exception\TokenResponseException;
7use OAuth\Common\Http\Uri\Uri;
8use OAuth\Common\Consumer\CredentialsInterface;
9use OAuth\Common\Storage\TokenStorageInterface;
10use OAuth\Common\Http\Client\ClientInterface;
11use OAuth\Common\Http\Uri\UriInterface;
12use OAuth\OAuth1\Signature\SignatureInterface;
13
14class QuickBooks extends AbstractService
15{
16    /**
17     * {@inheritdoc}
18     */
19    public function __construct(
20        CredentialsInterface $credentials,
21        ClientInterface $httpClient,
22        TokenStorageInterface $storage,
23        SignatureInterface $signature,
24        UriInterface $baseApiUri = null
25    ) {
26        parent::__construct(
27            $credentials,
28            $httpClient,
29            $storage,
30            $signature,
31            $baseApiUri
32        );
33
34        if (null === $baseApiUri) {
35            $this->baseApiUri = new Uri('https://quickbooks.api.intuit.com/');
36        }
37    }
38
39    /**
40     * {@inheritdoc}
41     */
42    public function getRequestTokenEndpoint()
43    {
44        return new Uri('https://oauth.intuit.com/oauth/v1/get_request_token');
45    }
46
47    /**
48     * {@inheritdoc}
49     */
50    public function getAuthorizationEndpoint()
51    {
52        return new Uri('https://appcenter.intuit.com/Connect/Begin');
53    }
54
55    /**
56     * {@inheritdoc}
57     */
58    public function getAccessTokenEndpoint()
59    {
60        return new Uri('https://oauth.intuit.com/oauth/v1/get_access_token');
61    }
62
63    /**
64     * {@inheritdoc}
65     */
66    protected function parseRequestTokenResponse($responseBody)
67    {
68        parse_str($responseBody, $data);
69
70        if (null === $data || !is_array($data)) {
71            throw new TokenResponseException('Unable to parse response.');
72        } elseif (!isset($data['oauth_callback_confirmed'])
73            || $data['oauth_callback_confirmed'] !== 'true') {
74            throw new TokenResponseException('Error in retrieving token.');
75        }
76
77        return $this->parseAccessTokenResponse($responseBody);
78    }
79
80    /**
81     * {@inheritdoc}
82     */
83    protected function parseAccessTokenResponse($responseBody)
84    {
85        parse_str($responseBody, $data);
86
87        if (null === $data || !is_array($data)) {
88            throw new TokenResponseException('Unable to parse response.');
89        } elseif (isset($data['error'])) {
90            $message = 'Error in retrieving token: "' . $data['error'] . '"';
91            throw new TokenResponseException($message);
92        }
93
94        $token = new StdOAuth1Token();
95
96        $token->setRequestToken($data['oauth_token']);
97        $token->setRequestTokenSecret($data['oauth_token_secret']);
98        $token->setAccessToken($data['oauth_token']);
99        $token->setAccessTokenSecret($data['oauth_token_secret']);
100
101        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
102        unset($data['oauth_token'], $data['oauth_token_secret']);
103        $token->setExtraParams($data);
104
105        return $token;
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    public function request(
112        $path,
113        $method = 'GET',
114        $body = null,
115        array $extraHeaders = array()
116    ) {
117        $extraHeaders['Accept'] = 'application/json';
118        return parent::request($path, $method, $body, $extraHeaders);
119    }
120}
121