QuickBooks.php 3.3 KB

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