Skip to content

Instantly share code, notes, and snippets.

@StefanoGroenland
Created October 18, 2018 07:53
Show Gist options
  • Save StefanoGroenland/5046012555c9c232aa38a073365bbbb0 to your computer and use it in GitHub Desktop.
Save StefanoGroenland/5046012555c9c232aa38a073365bbbb0 to your computer and use it in GitHub Desktop.
Picqer client classes.
<?php
namespace App\Financials\ExactOnline\Sync\Connection;
/**
* Class ResourceImporter
*
* @package App\Financials\ExactOnline\Sync
* @copyright Copyright (c) 2018, POS4RESTAURANTS BV
*/
class Client
{
private $OAuth;
public function __construct($OAuth = null)
{
$this->OAuth = is_null($OAuth) ? new OAuth : $OAuth;
}
public function requestOAuth()
{
return $this->OAuth->request();
}
/**
* @param null $division
*
* @return \Picqer\Financials\Exact\Connection
* @throws \Exception
*/
public function connect($division = null)
{
return $this->OAuth->connect($division);
}
public function setDivision($division)
{
return $this->OAuth->setDivision($division);
}
public function getDivision()
{
return $this->OAuth->getDivision();
}
}
<?php
namespace App\Financials\ExactOnline\Sync\Connection;
use GuzzleHttp\Exception\RequestException;
use Picqer\Financials\Exact;
/**
* Class OAuth
*
* @package App\Financials\ExactOnline\Sync
* @copyright Copyright (c) 2018, POS4RESTAURANTS BV
*/
class OAuth
{
/** @var Exact\Connection */
protected $connection;
public function __construct()
{
$this->connection = new Exact\Connection();
$this->connection->setExactClientId(env('EXACT_CLIENT_ID'));
$this->connection->setExactClientSecret(env('EXACT_CLIENT_SECRET'));
$this->connection->setRedirectUrl(action('App\Financials\ExactOnline\Sync\AccessController@receive'));
}
public function request()
{
return $this->connection->redirectForAuthorization();
}
/**
* @param null $division
*
* @return Exact\Connection
* @throws \Exception
*/
public function connect($division = null)
{
if (!$division) {
$division = env('EXACT_DIVISION');
}
$account = ExactAccount::firstOrFail();
try {
$this->connection->setAccessToken($account->access_token);
$this->connection->setRefreshToken($account->refresh_token);
$this->connection->setAuthorizationCode($account->authorization_code);
$this->connection->connect();
} catch (RequestException $e) {
$this->connection->setAccessToken(null);
$this->connection->setRefreshToken(null);
$this->connection->connect();
} catch (\Exception $e) {
throw new \Exception('Could not connect to Exact ' . $e->getMessage());
}
$account->update([
'access_token' => $this->connection->getAccessToken(),
'refresh_token' => $this->connection->getRefreshToken()
]);
$this->connection->setDivision($division);
return $this->connection;
}
public function setDivision($division)
{
$this->connection->setDivision($division);
return $this;
}
public function getDivision()
{
return $this->connection->getDivision();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment