OAuth.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Stripe;
  3. abstract class OAuth
  4. {
  5. /**
  6. * Generates a URL to Stripe's OAuth form.
  7. *
  8. * @param array|null $params
  9. * @param array|null $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::urlEncode($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 array|null $params
  29. * @param array|null $opts
  30. *
  31. * @return StripeObject Object containing the response from the API.
  32. */
  33. public static function token($params = null, $opts = null)
  34. {
  35. $base = ($opts && array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
  36. $requestor = new ApiRequestor(null, $base);
  37. list($response, $apiKey) = $requestor->request(
  38. 'post',
  39. '/oauth/token',
  40. $params,
  41. null
  42. );
  43. return Util\Util::convertToStripeObject($response->json, $opts);
  44. }
  45. /**
  46. * Disconnects an account from your platform.
  47. *
  48. * @param array|null $params
  49. * @param array|null $opts
  50. *
  51. * @return StripeObject Object containing the response from the API.
  52. */
  53. public static function deauthorize($params = null, $opts = null)
  54. {
  55. $params = $params ?: [];
  56. $base = ($opts && array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
  57. $requestor = new ApiRequestor(null, $base);
  58. $params['client_id'] = self::_getClientId($params);
  59. list($response, $apiKey) = $requestor->request(
  60. 'post',
  61. '/oauth/deauthorize',
  62. $params,
  63. null
  64. );
  65. return Util\Util::convertToStripeObject($response->json, $opts);
  66. }
  67. private static function _getClientId($params = null)
  68. {
  69. $clientId = ($params && array_key_exists('client_id', $params)) ? $params['client_id'] : null;
  70. if ($clientId === null) {
  71. $clientId = Stripe::getClientId();
  72. }
  73. if ($clientId === null) {
  74. $msg = 'No client_id provided. (HINT: set your client_id using '
  75. . '"Stripe::setClientId(<CLIENT-ID>)". You can find your client_ids '
  76. . 'in your Stripe dashboard at '
  77. . 'https://dashboard.stripe.com/account/applications/settings, '
  78. . 'after registering your account as a platform. See '
  79. . 'https://stripe.com/docs/connect/standard-accounts for details, '
  80. . 'or email support@stripe.com if you have any questions.';
  81. throw new Error\Authentication($msg);
  82. }
  83. return $clientId;
  84. }
  85. }