Create a gist now

Instantly share code, notes, and snippets.

@amfeng /index.php
Last active Feb 1, 2017

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
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 :)

@achraf52

Thank you for sharing such an useful piece of code.

@ferozpuri

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
orliXdx commented Nov 28, 2014

same problem as ferozpuri

@amfeng
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
Jeff-P commented Feb 14, 2015

Thank you

@habib9861

@amfeng I still get the blank response

@bhmkbelani

@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'];
@bhmkbelani

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".

@nikitabanthiya91

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

@nikitabanthiya91

helpp me !!!!!

@punit-gajjar

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

@phirschybar

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

@plhoangan

Getting blank response!

@mnpenner

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.

@AntonShapoval

Thank you very much!!!

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