OAuth.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Stripe;
  3. abstract class OAuth
  4. {
  5. /**
  6. * Generates a URL to Stripe's OAuth form.
  7. *
  8. * @param null|array $params
  9. * @param null|array $opts
  10. *
  11. * @return string the URL to Stripe's OAuth form
  12. */
  13. public static function authorizeUrl($params = null, $opts = null)
  14. {
  15. $params = $params ?: [];
  16. $base = ($opts && \array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
  17. $params['client_id'] = self::_getClientId($params);
  18. if (!\array_key_exists('response_type', $params)) {
  19. $params['response_type'] = 'code';
  20. }
  21. $query = Util\Util::encodeParameters($params);
  22. return $base . '/oauth/authorize?' . $query;
  23. }
  24. /**
  25. * Use an authoriztion code to connect an account to your platform and
  26. * fetch the user's credentials.
  27. *
  28. * @param null|array $params
  29. * @param null|array $opts
  30. *
  31. * @throws \Stripe\Exception\OAuth\OAuthErrorException if the request fails
  32. *
  33. * @return StripeObject object containing the response from the API
  34. */
  35. public static function token($params = null, $opts = null)
  36. {
  37. $base = ($opts && \array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
  38. $requestor = new ApiRequestor(null, $base);
  39. list($response, $apiKey) = $requestor->request(
  40. 'post',
  41. '/oauth/token',
  42. $params,
  43. null
  44. );
  45. return Util\Util::convertToStripeObject($response->json, $opts);
  46. }
  47. /**
  48. * Disconnects an account from your platform.
  49. *
  50. * @param null|array $params
  51. * @param null|array $opts
  52. *
  53. * @throws \Stripe\Exception\OAuth\OAuthErrorException if the request fails
  54. *
  55. * @return StripeObject object containing the response from the API
  56. */
  57. public static function deauthorize($params = null, $opts = null)
  58. {
  59. $params = $params ?: [];
  60. $base = ($opts && \array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
  61. $requestor = new ApiRequestor(null, $base);
  62. $params['client_id'] = self::_getClientId($params);
  63. list($response, $apiKey) = $requestor->request(
  64. 'post',
  65. '/oauth/deauthorize',
  66. $params,
  67. null
  68. );
  69. return Util\Util::convertToStripeObject($response->json, $opts);
  70. }
  71. private static function _getClientId($params = null)
  72. {
  73. $clientId = ($params && \array_key_exists('client_id', $params)) ? $params['client_id'] : null;
  74. if (null === $clientId) {
  75. $clientId = Stripe::getClientId();
  76. }
  77. if (null === $clientId) {
  78. $msg = 'No client_id provided. (HINT: set your client_id using '
  79. . '"Stripe::setClientId(<CLIENT-ID>)". You can find your client_ids '
  80. . 'in your Stripe dashboard at '
  81. . 'https://dashboard.stripe.com/account/applications/settings, '
  82. . 'after registering your account as a platform. See '
  83. . 'https://stripe.com/docs/connect/standard-accounts for details, '
  84. . 'or email support@stripe.com if you have any questions.';
  85. throw new Exception\AuthenticationException($msg);
  86. }
  87. return $clientId;
  88. }
  89. }