Skip to content

Commit

Permalink
Merge pull request #26 from picqer/settable-urls
Browse files Browse the repository at this point in the history
Make url's settable #25
  • Loading branch information
stephangroen committed Dec 14, 2015
2 parents 667bee9 + 05d5550 commit c28ad67
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 87 deletions.
178 changes: 98 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ Note: For Guzzle 6 use v2, for Guzzle 3 use v1.
## Composer install
Installing this Exact client for PHP can be done through Composer.

"require": {
"picqer/exact-php-client": "~2.0"
}

```json
"require": {
"picqer/exact-php-client": "~2.0"
}
```

## Usage

1. Set up app at Exact App Center to retrieve credentials
Expand All @@ -29,11 +31,13 @@ You will also need to set the correct `Callback URL` for the oAuth dance to work

The code below is an example `authorize()` function.

$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL'); // Same as entered online in the App Center
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
$connection->redirectForAuthorization();
```php
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL'); // Same as entered online in the App Center
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
$connection->redirectForAuthorization();
```

This will redirect the user to Exact to login and authorize your integration with their account.

Expand All @@ -47,85 +51,99 @@ The `refreshtoken` is a token which is used to get a new `accesstoken` which als
The library will settle all of this for you. The code below could be an general `connect()` function, which returns
the api connection.

$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL');
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
```php
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL');
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');

if (getValue('authorizationcode')) // Retrieves authorizationcode from database
$connection->setAuthorizationCode(getValue('authorizationcode'));
if (getValue('authorizationcode')) // Retrieves authorizationcode from database
$connection->setAuthorizationCode(getValue('authorizationcode'));

if (getValue('accesstoken')) // Retrieves accesstoken from database
$connection->setAccessToken(unserialize(getValue('accesstoken')));
if (getValue('accesstoken')) // Retrieves accesstoken from database
$connection->setAccessToken(unserialize(getValue('accesstoken')));

if (getValue('refreshtoken')) // Retrieves refreshtoken from database
$connection->setRefreshToken(getValue('refreshtoken'));
if (getValue('refreshtoken')) // Retrieves refreshtoken from database
$connection->setRefreshToken(getValue('refreshtoken'));

// Make the client connect and exchange tokens
try {
$connection->client();
} catch (\Exception $e)
{
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}
// Make the client connect and exchange tokens
try {
$connection->client();
} catch (\Exception $e)
{
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}

// Save the new tokens for next connections
setValue('accesstoken', serialize($connection->getAccessToken()));
setValue('refreshtoken', $connection->getRefreshToken());
// Save the new tokens for next connections
setValue('accesstoken', serialize($connection->getAccessToken()));
setValue('refreshtoken', $connection->getRefreshToken());
```

### Use the library to do stuff (examples)

// Create a new account
$account = new Account($connection);
$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Status = 'C';
$account->save();


// Add a product in Exact
$item = new Item($connection);
$item->Code = $productcode;
$item->CostPriceStandard = $costprice;
$item->Description = $name;
$item->IsSalesItem = true;
$item->SalesVatCode = 'VH';
$item->save();


// Retrieve an item
$item = new Item($connection);
$item->find(ID);

// List items
$item = new Item($connection);
$item->get();

// List items with filter (using a filter always returns a collection)
$item = new Item($connection);
$items = $item->filter("Code eq '$productcode'"); // Uses filters as described in Exact API docs (odata filters)
// Create new invoice with invoice lines
$items[] = [
'Item' => $itemId,
'Quantity' => $orderproduct['amount'],
'UnitPrice' => $orderproduct['price']
];
$salesInvoice = new SalesInvoice($this->connection());
$salesInvoice->InvoiceTo = $customer_code;
$salesInvoice->OrderedBy = $customer_code;
$salesInvoice->YourRef = $orderId;
$salesInvoice->SalesInvoiceLines = $items;


```php
// Create a new account
$account = new Account($connection);
$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Status = 'C';
$account->save();


// Add a product in Exact
$item = new Item($connection);
$item->Code = $productcode;
$item->CostPriceStandard = $costprice;
$item->Description = $name;
$item->IsSalesItem = true;
$item->SalesVatCode = 'VH';
$item->save();


// Retrieve an item
$item = new Item($connection);
$item->find(ID);

// List items
$item = new Item($connection);
$item->get();

// List items with filter (using a filter always returns a collection)
$item = new Item($connection);
$items = $item->filter("Code eq '$productcode'"); // Uses filters as described in Exact API docs (odata filters)

// Create new invoice with invoice lines
$items[] = [
'Item' => $itemId,
'Quantity' => $orderproduct['amount'],
'UnitPrice' => $orderproduct['price']
];

$salesInvoice = new SalesInvoice($this->connection());
$salesInvoice->InvoiceTo = $customer_code;
$salesInvoice->OrderedBy = $customer_code;
$salesInvoice->YourRef = $orderId;
$salesInvoice->SalesInvoiceLines = $items;
```

## Connect to other Exact country then NL
Choose the right base URL according to [Exact developers guide](https://developers.exactonline.com/#Exact%20Online%20sites.html)

```php
<?php
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL');
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
$connection->setBaseUrl('https://start.exactonline.de');
```

Check [src/Picqer/Financials/Exact](src/Picqer/Financials/Exact) for all available entities.

## Code example
Expand Down
77 changes: 70 additions & 7 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ class Connection
/**
* @var string
*/
private $apiUrl = 'https://start.exactonline.nl/api/v1';
private $baseUrl = 'https://start.exactonline.nl';

/**
* @var string
*/
private $authUrl = 'https://start.exactonline.nl/api/oauth2/auth';
private $apiUrl = '/api/v1';

/**
* @var string
*/
private $tokenUrl = 'https://start.exactonline.nl/api/oauth2/token';
private $authUrl = '/api/oauth2/auth';

/**
* @var string
*/
private $tokenUrl = '/api/oauth2/token';

/**
* @var
Expand Down Expand Up @@ -244,7 +249,7 @@ public function delete($url)
*/
private function getAuthUrl()
{
return $this->authUrl . '?' . http_build_query(array(
return $this->baseUrl . $this->authUrl . '?' . http_build_query(array(
'client_id' => $this->exactClientId,
'redirect_uri' => $this->redirectUrl,
'response_type' => 'code'
Expand Down Expand Up @@ -404,7 +409,7 @@ private function acquireAccessToken()
];
}

$response = $this->client()->post($this->tokenUrl, $body);
$response = $this->client()->post($this->getTokenUrl(), $body);

if ($response->getStatusCode() == 200) {
Psr7\rewind_body($response);
Expand Down Expand Up @@ -461,14 +466,14 @@ private function formatUrl($endPoint, $includeDivision = true)
{
if ($includeDivision) {
return implode('/', [
$this->apiUrl,
$this->getApiUrl(),
$this->getCurrentDivisionNumber(),
$endPoint
]);
}

return implode('/', [
$this->apiUrl,
$this->getApiUrl(),
$endPoint
]);
}
Expand Down Expand Up @@ -517,6 +522,64 @@ private function parseExceptionForErrorMessages(Exception $e)
throw new ApiException('Error ' . $response->getStatusCode() .': ' . $errorMessage);
}

/**
* @return string
*/
protected function getBaseUrl()
{
return $this->baseUrl;
}

/**
* @return string
*/
private function getApiUrl()
{
return $this->baseUrl . $this->apiUrl;
}

/**
* @return string
*/
private function getTokenUrl()
{
return $this->baseUrl . $this->tokenUrl;
}

/**
* Set base URL for different countries according to
* https://developers.exactonline.com/#Exact%20Online%20sites.html
*
* @param string $baseUrl
*/
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}

/**
* @param string $apiUrl
*/
public function setApiUrl($apiUrl)
{
$this->apiUrl = $apiUrl;
}

/**
* @param string $authUrl
*/
public function setAuthUrl($authUrl)
{
$this->authUrl = $authUrl;
}

/**
* @param string $tokenUrl
*/
public function setTokenUrl($tokenUrl)
{
$this->tokenUrl = $tokenUrl;
}
}


0 comments on commit c28ad67

Please sign in to comment.