Skip to content

Instantly share code, notes, and snippets.

@manshu
Last active July 13, 2020 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manshu/7ddbb58427982d254f834cea96e7f28c to your computer and use it in GitHub Desktop.
Save manshu/7ddbb58427982d254f834cea96e7f28c to your computer and use it in GitHub Desktop.
Pipedrive CRMServiceProvider
class CRMServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->app->bind(CRMServiceInterface::class, function ($app) {
$pipedrive = Pipedrive::OAuth([
'clientId' => env('PIPEDRIVE_CLIENT_ID'),
'clientSecret' => env('PIPEDRIVE_CLIENT_SECRET'),
'redirectUrl' => env('PIPEDRIVE_REDIRECT_URI'),
'storage' => new PipedriveTokenIO(),
]);
return new PipedriveService($pipedrive);
});
}
}
class PipedriveService implements CRMServiceInterface
{
protected $pipedrive;
public function __construct(Pipedrive $pipedrive)
{
$this->pipedrive = $pipedrive;
}
public function authorize($code)
{
return $this->pipedrive->authorize($code);
}
}
class PipedriveTokenIO implements PipedriveTokenStorage
{
public function getToken()
{
//return isset($_SESSION['token']) ? unserialize($_SESSION['token']) : null;
if (Session::has('token')) {
session(['token']);
} else {
$response = auth()->user()->token->only('access_token', 'expires_in', 'refresh_token');
$token = new PipedriveToken([
'accessToken' => $response['access_token'], // read it individually from the db
'refreshToken' => $response['expires_in'], // read it individually from the db
'expiresAt' => $response['refresh_token'], // read it individually from the db
]);
session(['token' => $token]);
}
}
public function setToken(PipedriveToken $token)
{
if (!!\Auth::User()->token) {
$response = \Auth::User()->token->only('access_token', 'expires_in', 'refresh_token');
$token = new PipedriveToken([
'accessToken' => $response['access_token'], // read it individually from the db
'refreshToken' => $response['expires_in'], // read it individually from the db
'expiresAt' => $response['refresh_token'], // read it individually from the db
]);
session(['token' => $token]);
} else {
$client = Client::create([
'user_id' => auth()->user()->id,
'provider_name' => 'pipedrive',
'access_token' => $token->getAccessToken(),
'expires_in' => $token->expiresAt(),
'refresh_token' => $token->getRefreshToken(),
]);
session(['token' => $token]);
return $client;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment