<?php

require_once './config/config.php';
// initialize a new generic provider
$provider = new \League\OAuth2\Client\Provider\GenericProvider(['clientId' => CLIENT_ID, 'clientSecret' => SECRET_KEY, 'redirectUri' => REDIRECT_URL, 'urlAuthorize' => 'https://connect.stripe.com/oauth/authorize', 'urlAccessToken' => 'https://connect.stripe.com/oauth/token', 'urlResourceOwnerDetails' => 'https://api.stripe.com/v1/account']);
// Check for an authorization code
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    // Try to retrieve the access token
    try {
        $accessToken = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);
        // You could retrieve the API key with `$accessToken->getToken()`, but it's better to authenticate using the Stripe-account header (below)
        // Retrieve the account ID to be used for authentication: https://stripe.com/docs/connect/authentication
        // TODO: Save this account ID to your database for later use.
        $account_id = $provider->getResourceOwner($accessToken)->getId();
        // Retrieve the account from Stripe: https://stripe.com/docs/api/php#retrieve_account
        $account = \Stripe\Account::Retrieve($account_id);
        $success = "Your Stripe account has been connected!";
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
} elseif (isset($_GET['error'])) {
    $error = $_GET['error_description'];
} else {
    $error = "No authorization code received";
}
Exemple #2
0
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$provider = new \League\OAuth2\Client\Provider\GenericProvider(['clientId' => getenv('CLIENT_ID'), 'clientSecret' => getenv('CLIENT_SECRET'), 'redirectUri' => getenv('REDIRECT_URI'), 'urlAuthorize' => getenv('AUTH_URL'), 'urlAccessToken' => getenv('TOKEN_URL'), 'urlResourceOwnerDetails' => getenv('ATTRIBUTES_URL')]);
session_start();
if (!isset($_GET['code'])) {
    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();
    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();
    // Redirect the user to the authorization URL.
    header('Location: ' . filter_var($authorizationUrl, FILTER_SANITIZE_URL));
    exit;
} else {
    try {
        // Try to get an access token using the authorization code grant.
        // Pass in the affiliation in the scope
        $accessToken = $provider->getAccessToken('authorization_code', ['code' => $_GET['code'], 'scope' => 'military']);
        // // Using the access token, we may look up details about the user
        $resourceOwner = $provider->getResourceOwner($accessToken);
        // // The JSON payload of the response
        $_SESSION['payload'] = json_encode($resourceOwner->toArray());
        header('Location: ' . '/');
        exit;
    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
        // Failed to get the access token or user details.
        exit($e->getMessage());
    }
}