Last active
July 13, 2020 15:23
-
-
Save manshu/7ddbb58427982d254f834cea96e7f28c to your computer and use it in GitHub Desktop.
Pipedrive CRMServiceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PipedriveService implements CRMServiceInterface | |
{ | |
protected $pipedrive; | |
public function __construct(Pipedrive $pipedrive) | |
{ | |
$this->pipedrive = $pipedrive; | |
} | |
public function authorize($code) | |
{ | |
return $this->pipedrive->authorize($code); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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