<?php

require_once '../../vendor/autoload.php';
use Chargely\Sso\Jwt\Client;
/**
 * The easiest way to configure an SSO client is by storing your configuration in a file.
 *
 * See example configuration files in the ../config folder.
 */
// PHP config file (Recommended)
$client = Client::factory('../config/chargely.php');
// 'default_profile' will be used
$client = Client::factory('../config/chargely.php', 'dev');
// Load the 'dev' profile
// .ini config file (See warning at top of file)
$client = Client::factory('../config/chargely.ini');
// 'default_profile' will be used
$client = Client::factory('../config/chargely.ini', 'dev');
// Load the 'dev' profile
/**
 * You can also provide configuration explicitly in your code.
 *
 * This method is useful for rapid development or integrating with your existing configuration systems. However, be
 * careful to not hard-code your credentials inside of your applications. Hard-coding your credentials can be dangerous,
 * because it is easy to accidentally commit your credentials into an SCM repository, potentially exposing your
 * credentials to more people than intended. It can also make it difficult to rotate credentials in the future.
 */
$client = Client::factory(array('subdomain' => 'mycompany', 'public_key' => '{{PUBLIC_KEY}}', 'private_key' => '{{PRIVATE_KEY}}'));
<?php

require_once 'vendor/autoload.php';
use Chargely\Sso\Jwt\Client;
$client = Client::factory('../config/chargely.php');
/**
 * Presumably your application has a User object that gives you access to certain
 * properties such as logged-in status and e-mail address.
 *
 * Here we will just use "stdClass" to represent the User object in your system.
 */
$user = new stdClass();
// NEVER give a valid refresh token to users who are not authenticated with your system.
if ($user->isLoggedIn) {
    // (Good) Set user e-mail
    $client->setEmail($user->email);
    // (Better) Set Chargify customer ID if you have it stored in your system.
    $client->setCustId($user->chargify_customer_id);
    // (Best) Set Chargify customer reference if you have it stored in Chargify.
    $client->setCustRef($user->id);
    // Send refresh token to browser and exit.
    $client->sendRefreshToken(200);
    exit;
} else {
    // the user is not authorized.
    $client->sendRefreshToken(401);
    exit;
}