oauth.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require('../init.php');
  3. \Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
  4. \Stripe\Stripe::setClientId(getenv('STRIPE_CLIENT_ID'));
  5. if (isset($_GET['code'])) {
  6. // The user was redirected back from the OAuth form with an authorization code.
  7. $code = $_GET['code'];
  8. try {
  9. $resp = \Stripe\OAuth::token([
  10. 'grant_type' => 'authorization_code',
  11. 'code' => $code,
  12. ]);
  13. } catch (\Stripe\Error\OAuth\OAuthBase $e) {
  14. exit("Error: " . $e->getMessage());
  15. }
  16. $accountId = $resp->stripe_user_id;
  17. echo "<p>Success! Account <code>$accountId</code> is connected.</p>\n";
  18. echo "<p>Click <a href=\"?deauth=$accountId\">here</a> to disconnect the account.</p>\n";
  19. } elseif (isset($_GET['error'])) {
  20. // The user was redirect back from the OAuth form with an error.
  21. $error = $_GET['error'];
  22. $error_description = $_GET['error_description'];
  23. echo "<p>Error: code=" . htmlspecialchars($error, ENT_QUOTES) . ", description=" . htmlspecialchars($error_description, ENT_QUOTES) . "</p>\n";
  24. echo "<p>Click <a href=\"?\">here</a> to restart the OAuth flow.</p>\n";
  25. } elseif (isset($_GET['deauth'])) {
  26. // Deauthorization request
  27. $accountId = $_GET['deauth'];
  28. try {
  29. \Stripe\OAuth::deauthorize([
  30. 'stripe_user_id' => $accountId,
  31. ]);
  32. } catch (\Stripe\Error\OAuth\OAuthBase $e) {
  33. exit("Error: " . $e->getMessage());
  34. }
  35. echo "<p>Success! Account <code>" . htmlspecialchars($accountId, ENT_QUOTES) . "</code> is disconnected.</p>\n";
  36. echo "<p>Click <a href=\"?\">here</a> to restart the OAuth flow.</p>\n";
  37. } else {
  38. $url = \Stripe\OAuth::authorizeUrl([
  39. 'scope' => 'read_only',
  40. ]);
  41. echo "<a href=\"$url\">Connect with Stripe</a>\n";
  42. }