private function getConnectionForTesting()
 {
     $this->container = [];
     $history = Middleware::history($this->container);
     $connection = new \Picqer\Financials\Moneybird\Connection();
     $connection->insertMiddleWare($history);
     $connection->setClientId('testClientId');
     $connection->setClientSecret('testClientSecret');
     $connection->setAccessToken('testAccessToken');
     $connection->setAuthorizationCode('testAuthorizationCode');
     $connection->setRedirectUrl('testRedirectUrl');
     $connection->setTesting(true);
     return $connection;
 }
Example #2
0
/**
 * Function to connect to Moneybird, this creates the client and automatically retrieves oAuth tokens if needed
 *
 * @return \Picqer\Financials\Moneybird\Connection
 * @throws Exception
 */
function connect($redirectUrl, $clientId, $clientSecret)
{
    $connection = new \Picqer\Financials\Moneybird\Connection();
    $connection->setRedirectUrl($redirectUrl);
    $connection->setClientId($clientId);
    $connection->setClientSecret($clientSecret);
    // Retrieves authorizationcode from database
    if (getValue('authorizationcode')) {
        $connection->setAuthorizationCode(getValue('authorizationcode'));
    }
    // Retrieves accesstoken from database
    if (getValue('accesstoken')) {
        $connection->setAccessToken(getValue('accesstoken'));
    }
    // Make the client connect and exchange tokens
    try {
        $connection->connect();
    } catch (\Exception $e) {
        throw new Exception('Could not connect to Moneybird: ' . $e->getMessage());
    }
    // Save the new tokens for next connections
    setValue('accesstoken', $connection->getAccessToken());
    return $connection;
}
 private function getMoneybirdApi()
 {
     $connection = new \Picqer\Financials\Moneybird\Connection();
     $connection->setAccessToken($this->_config['moneybird_access_token']);
     $connection->setAdministrationId($this->_config['moneybird_administration_id']);
     $connection->setAuthorizationCode('not_required');
     try {
         $connection->connect();
     } catch (Exception $e) {
         die('Could not initialize Moneybird connection: ' . $e->getMessage());
     }
     $this->apiConnection = $connection;
     return new \Picqer\Financials\Moneybird\Moneybird($this->apiConnection);
 }