session_start();
require_once '../vendor/autoload.php';
$infusionsoft = new \Infusionsoft\Infusionsoft(array('clientId' => 'CLIENT_ID', 'clientSecret' => 'CLIENT_SECRET', 'redirectUri' => 'REDIRECT_URL'));
// By default, the SDK uses the Guzzle HTTP library for requests. To use CURL,
// you can change the HTTP client by using following line:
// $infusionsoft->setHttpClient(new \Infusionsoft\Http\CurlClient());
// If the serialized token is available in the session storage, we tell the SDK
// to use that token for subsequent requests.
if (isset($_SESSION['token'])) {
    $infusionsoft->setToken(unserialize($_SESSION['token']));
}
// If we are returning from Infusionsoft we need to exchange the code for an
// access token.
if (isset($_GET['code']) and !$infusionsoft->getToken()) {
    $infusionsoft->requestAccessToken($_GET['code']);
}
function addWithDupCheck($infusionsoft)
{
    $contact = array('FirstName' => 'John', 'LastName' => 'Doe', 'Email' => '*****@*****.**');
    return $infusionsoft->contacts->addWithDupCheck($contact, 'Email');
}
if ($infusionsoft->getToken()) {
    try {
        $cid = addWithDupCheck($infusionsoft);
    } catch (\Infusionsoft\TokenExpiredException $e) {
        // If the request fails due to an expired access token, we can refresh
        // the token and then do the request again.
        $infusionsoft->refreshAccessToken();
        $cid = addWithDupCheck($infusionsoft);
    }
    $infusionsoft = new \Infusionsoft\Infusionsoft(array('clientId' => getenv('INFUSIONSOFT_CLIENT_ID'), 'clientSecret' => getenv('INFUSIONSOFT_CLIENT_SECRET'), 'redirectUri' => getenv('INFUSIONSOFT_REDIRECT_URL')));
    echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to connect to Infusionsoft</a>';
});
$app->get('/callback', function () use($app) {
    // Setup a new Infusionsoft SDK object
    $infusionsoft = new \Infusionsoft\Infusionsoft(array('clientId' => getenv('INFUSIONSOFT_CLIENT_ID'), 'clientSecret' => getenv('INFUSIONSOFT_CLIENT_SECRET'), 'redirectUri' => getenv('INFUSIONSOFT_REDIRECT_URL')));
    // If the serialized token is already available in the session storage,
    // we tell the SDK to use that token for subsequent requests, rather
    // than try and retrieve another one.
    if (Session::has('token')) {
        $infusionsoft->setToken(unserialize(Session::get('token')));
    }
    // If we are returning from Infusionsoft we need to exchange the code
    // for an access token.
    if (Request::has('code') and !$infusionsoft->getToken()) {
        $infusionsoft->requestAccessToken(Request::get('code'));
    }
    // NOTE: there's some magic in the step above - the Infusionsoft SDK has
    // not only requested an access token, but also set the token in the current
    // Infusionsoft object, so there's no need for you to do it.
    if ($infusionsoft->getToken()) {
        // Save the serialized token to the current session for subsequent requests
        // NOTE: this can be saved in your database - make sure to serialize the
        // entire token for easy future access
        Session::put('token', serialize($infusionsoft->getToken()));
        // Now redirect the user to a page that performs some Infusionsoft actions
        return redirect()->to('/contacts');
    }
    // something didn't work, so let's go back to the beginning
    return redirect()->to('/');
});