Skip to content

Instantly share code, notes, and snippets.

@amfeng
Last active October 16, 2024 21:27
Show Gist options
  • Save amfeng/3507366 to your computer and use it in GitHub Desktop.
Save amfeng/3507366 to your computer and use it in GitHub Desktop.
Stripe OAuth Example -- PHP
<?php
define('CLIENT_ID', 'YOUR_CLIENT_ID');
define('API_KEY', 'YOUR_API_KEY');
define('TOKEN_URI', 'https://connect.stripe.com/oauth/token');
define('AUTHORIZE_URI', 'https://connect.stripe.com/oauth/authorize');
if (isset($_GET['code'])) { // Redirect w/ code
$code = $_GET['code'];
$token_request_body = array(
'client_secret' => API_KEY,
'grant_type' => 'authorization_code',
'client_id' => CLIENT_ID,
'code' => $code,
);
$req = curl_init(TOKEN_URI);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, true );
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
// TODO: Additional error handling
$respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
$resp = json_decode(curl_exec($req), true);
curl_close($req);
echo $resp['access_token'];
} else if (isset($_GET['error'])) { // Error
echo $_GET['error_description'];
} else { // Show OAuth link
$authorize_request_body = array(
'response_type' => 'code',
'scope' => 'read_write',
'client_id' => CLIENT_ID
);
$url = AUTHORIZE_URI . '?' . http_build_query($authorize_request_body);
echo "<a href='$url'>Connect with Stripe</a>";
}
?>
@ryderx
Copy link

ryderx commented May 20, 2020

Hi i am getting the following error
array(4) { ["client_secret"]=> string(42) "sk_test_xZZKjDRsPZb0z3KWvOCK0uXf00amK9apPF" ["grant_type"]=> NULL ["client_id"]=> string(35) "ca_HFVybE89S1Jse3hapX45PqAMaCFfI3Mr" ["code"]=> string(35) "ac_HJKaA5JoCzOwofYAxd0OYkYA4qodf78y" } array(2) { ["error"]=> string(15) "invalid_request" ["error_description"]=> string(23) "No grant type specified" }

this is my code:

API_KEY, 'grant_type' => $_GET['authorization_code'], 'client_id' => CLIENT_ID, 'code' => $code, ); var_dump($token_request_body); $req = curl_init(TOKEN_URI); curl_setopt($req, CURLOPT_RETURNTRANSFER, true); curl_setopt($req, CURLOPT_POST, true ); curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body)); // TODO: Additional error handling $resp = json_decode(curl_exec($req), true); $respCode = curl_getinfo($req, CURLINFO_HTTP_CODE); curl_close($req); echo $resp['access_token']; var_dump($resp); } else if (isset($_GET['error'])) { // Error echo $_GET['error_description']; } else { // Show OAuth link $authorize_request_body = array( 'response_type' => 'code', 'scope' => 'read_write', 'client_id' => CLIENT_ID ); $url = AUTHORIZE_URI . '?' . http_build_query($authorize_request_body); echo "Connect with Stripe"; } any help is welcome thank you in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment