Create a gist now

Instantly share code, notes, and snippets.

@amfeng /index.php
Last active Oct 9, 2017

What would you like to do?
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>";
}
?>

onassar commented Oct 16, 2012

Built an OAuth2 library in PHP to help with this flow.
Named PHP-StripeOAuth, the goal was to provide a clean wrapper for the redirects and and data-access.
Let me know if you have any feedback :)

Thank you for sharing such an useful piece of code.

hello there, i am using this code and change client id and api key with my account, but i am receiving null response.

here is my code..

define('CLIENT_ID', 'ca_4auFMvHC6upU1wymeMUXsMHIpuKREeP3');
define('API_KEY', 'sk_test_fGPhznwVgzGmiPqrIPoG1WwP');

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' => $_GET['scope'],
  '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
$respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);

$resp = json_decode(curl_exec($req), true);
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 "<a href='$url'>Connect with Stripe</a>";

}

orliXdx commented Nov 28, 2014

same problem as ferozpuri

Owner

amfeng commented Dec 11, 2014

Hi all, for those of you who are trying to use this code locally and getting blank responses, this might help:

http://stackoverflow.com/questions/18971983/curl-requires-curlopt-ssl-verifypeer-false

Jeff-P commented Feb 14, 2015

Thank you

@amfeng I still get the blank response

@amfeng I also get the black response.....i have included the certificate as well. i got the access_code but how to get the users credentials?
this is my code. please help me ASAP...

        $code = $_GET['code'];

        $token_request_body = array(
            'grant_type' => 'authorization_code',
            'client_id' => '*****************************',
            'code' => $code,
            'client_secret' => '*********************************'
        );


        $path = asset_url()."cacert.pem"; //path of my certificate file

        $req = curl_init(TOKEN_URI);
        curl_setopt($req, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($req, CURLOPT_CAINFO, $path);
        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'];

okey...problem solved. Have to put certificate file. and though i was running it on offline, i have to give value as "false" in "CURLOPT_SSL_VERIFYPEER".

getting blank response !!!!!!!!!!!!!!!!!!!!

helpp me !!!!!

getting blank response !!!!!!!!!!!!!!!!!!!!

is this available in Stripe's native API library? Seems silly to do raw CURL if not necessary.

Getting blank response!

You have to call curl_exec before you can call curl_getinfo($req, CURLINFO_HTTP_CODE), otherwise it will return 0.

Other than that, this code worked fine for me.

Thank you very much!!!

still got blank response

robvls commented Oct 9, 2017

@mnpenner wins with the comment about curl_exec before curl_getinfo -- thank you!

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