OAuthService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Stripe\Service;
  3. class OAuthService extends \Stripe\Service\AbstractService
  4. {
  5. /**
  6. * Sends a request to Stripe's Connect API.
  7. *
  8. * @param string $method the HTTP method
  9. * @param string $path the path of the request
  10. * @param array $params the parameters of the request
  11. * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
  12. *
  13. * @return \Stripe\StripeObject the object returned by Stripe's Connect API
  14. */
  15. protected function requestConnect($method, $path, $params, $opts)
  16. {
  17. $opts = $this->_parseOpts($opts);
  18. $opts->apiBase = $this->_getBase($opts);
  19. return $this->request($method, $path, $params, $opts);
  20. }
  21. /**
  22. * Generates a URL to Stripe's OAuth form.
  23. *
  24. * @param null|array $params
  25. * @param null|array $opts
  26. *
  27. * @return string the URL to Stripe's OAuth form
  28. */
  29. public function authorizeUrl($params = null, $opts = null)
  30. {
  31. $params = $params ?: [];
  32. $opts = $this->_parseOpts($opts);
  33. $base = $this->_getBase($opts);
  34. $params['client_id'] = $this->_getClientId($params);
  35. if (!\array_key_exists('response_type', $params)) {
  36. $params['response_type'] = 'code';
  37. }
  38. $query = \Stripe\Util\Util::encodeParameters($params);
  39. return $base . '/oauth/authorize?' . $query;
  40. }
  41. /**
  42. * Use an authoriztion code to connect an account to your platform and
  43. * fetch the user's credentials.
  44. *
  45. * @param null|array $params
  46. * @param null|array $opts
  47. *
  48. * @throws \Stripe\Exception\OAuth\OAuthErrorException if the request fails
  49. *
  50. * @return \Stripe\StripeObject object containing the response from the API
  51. */
  52. public function token($params = null, $opts = null)
  53. {
  54. $params = $params ?: [];
  55. $params['client_secret'] = $this->_getClientSecret($params);
  56. return $this->requestConnect('post', '/oauth/token', $params, $opts);
  57. }
  58. /**
  59. * Disconnects an account from your platform.
  60. *
  61. * @param null|array $params
  62. * @param null|array $opts
  63. *
  64. * @throws \Stripe\Exception\OAuth\OAuthErrorException if the request fails
  65. *
  66. * @return \Stripe\StripeObject object containing the response from the API
  67. */
  68. public function deauthorize($params = null, $opts = null)
  69. {
  70. $params = $params ?: [];
  71. $params['client_id'] = $this->_getClientId($params);
  72. return $this->requestConnect('post', '/oauth/deauthorize', $params, $opts);
  73. }
  74. private function _getClientId($params = null)
  75. {
  76. $clientId = ($params && \array_key_exists('client_id', $params)) ? $params['client_id'] : null;
  77. if (null === $clientId) {
  78. $clientId = $this->client->getClientId();
  79. }
  80. if (null === $clientId) {
  81. $msg = 'No client_id provided. (HINT: set your client_id using '
  82. . '`new \Stripe\StripeClient([clientId => <CLIENT-ID>
  83. ])`)". You can find your client_ids '
  84. . 'in your Stripe dashboard at '
  85. . 'https://dashboard.stripe.com/account/applications/settings, '
  86. . 'after registering your account as a platform. See '
  87. . 'https://stripe.com/docs/connect/standard-accounts for details, '
  88. . 'or email support@stripe.com if you have any questions.';
  89. throw new \Stripe\Exception\AuthenticationException($msg);
  90. }
  91. return $clientId;
  92. }
  93. private function _getClientSecret($params = null)
  94. {
  95. if (\array_key_exists('client_secret', $params)) {
  96. return $params['client_secret'];
  97. }
  98. return $this->client->getApiKey();
  99. }
  100. /**
  101. * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
  102. *
  103. * @throws \Stripe\Exception\InvalidArgumentException
  104. *
  105. * @return \Stripe\Util\RequestOptions
  106. */
  107. private function _parseOpts($opts)
  108. {
  109. if (\is_array($opts)) {
  110. if (\array_key_exists('connect_base', $opts)) {
  111. // Throw an exception for the convenience of anybody migrating to
  112. // \Stripe\Service\OAuthService from \Stripe\OAuth, where `connect_base`
  113. // was the name of the parameter that behaves as `api_base` does here.
  114. throw new \Stripe\Exception\InvalidArgumentException('Use `api_base`, not `connect_base`');
  115. }
  116. }
  117. return \Stripe\Util\RequestOptions::parse($opts);
  118. }
  119. /**
  120. * @param \Stripe\Util\RequestOptions $opts
  121. *
  122. * @return string
  123. */
  124. private function _getBase($opts)
  125. {
  126. return isset($opts->apiBase) ?
  127. $opts->apiBase :
  128. $this->client->getConnectBase();
  129. }
  130. }