Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend all testing #5

Merged
merged 52 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ac63f2e
Update readme.md
iranl May 13, 2019
ba254d2
Upload
iranl May 14, 2019
8ff1016
Update readme.md
iranl May 14, 2019
f25dade
Update RenewAuthorization.php
iranl May 14, 2019
c910487
Upload
iranl May 15, 2019
ab8485a
Upload
iranl May 15, 2019
f636072
Update readme.md
iranl May 15, 2019
e1ae0ef
Upload
iranl May 15, 2019
8fb6a18
Upload
iranl May 20, 2019
23c3588
Upload
iranl May 20, 2019
a9a47c1
Update LinesField.php
iranl May 20, 2019
511901e
Upload
iranl May 20, 2019
1b895af
Upload
iranl May 20, 2019
d473a43
Upload
iranl May 20, 2019
715cfae
Upload
iranl May 20, 2019
84bd966
Update BaseTransactionLine.php
iranl May 20, 2019
ea1110c
Upload
iranl May 20, 2019
fd87d27
Upload
iranl May 20, 2019
8b93d38
Upload
iranl May 20, 2019
9d80a55
Upload
iranl May 20, 2019
4f1cf71
Update TransactionsDocument.php
iranl May 20, 2019
900e206
Upload
iranl May 20, 2019
352c0bb
Upload
iranl May 20, 2019
501813c
Upload
iranl May 20, 2019
cf55a0f
Upload
iranl May 20, 2019
39fe6da
Upload
iranl May 20, 2019
dd99194
Upload
iranl May 21, 2019
036b0e0
Update CloseAndStartValueFields.php
iranl May 21, 2019
7f2c6db
Update CloseAndStartValueFields.php
iranl May 21, 2019
4a58c61
Update CloseAndStartValueFields.php
iranl May 21, 2019
dc31e4b
Update CloseAndStartValueFields.php
iranl May 21, 2019
2dd2e3b
Upload
iranl May 21, 2019
f31caf2
Upload
iranl May 21, 2019
3ac3c74
Upload
iranl May 21, 2019
a4d8709
Upload
iranl May 21, 2019
0f19a0a
Upload
iranl May 21, 2019
75b6bde
Upload
iranl May 21, 2019
0a9dc3b
Upload
iranl May 21, 2019
31dcd9b
Upload
iranl May 21, 2019
5361b7c
Update TransactionMapper.php
iranl May 21, 2019
007cbd9
Upload
iranl May 21, 2019
7d08f62
Upload
iranl May 21, 2019
a1d8d65
Upload
iranl May 21, 2019
564e413
Upload
iranl May 21, 2019
220c4d2
Upload
iranl May 21, 2019
c9a42b9
Upload
iranl May 21, 2019
361a7a1
Upload
iranl May 21, 2019
9968021
Upload
iranl May 21, 2019
02dd604
Update TransactionLineInterface.php
iranl May 21, 2019
a683cbb
Upload
iranl May 21, 2019
e0f5377
Update TransactionsDocument.php
iranl May 21, 2019
4243673
Upload
iranl May 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions examples/Authorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/* To use OAuth 2 for logging in it is necessary that a user with access rights to Twinfield grants authorization.

* On loading a page containing the following code the user will be redirected to the Twinfield Login page.
* After successful login and optionally consent the user will be redirected back to the page at which point the Access Token and Refresh Token can be retrieved.

* For more information, please refer to: https://github.com/thephpleague/oauth2-client#usage
*/

function SaveRefreshTokenToStore(array $refreshTokenStorage) {
/* Save refresh token and expiry time to some kind of storage (e.g. SQL database).
* $refreshTokenStorage['refresh_token']
* $refreshTokenStorage['refresh_expiry']
*/
}

function SaveAccessTokenToStore(array $accessTokenStorage) {
/* Save access token, expiry time and cluster to some kind of storage (e.g. SQL database).
* $accessTokenStorage['access_token']
* $accessTokenStorage['access_expiry']
* $accessTokenStorage['access_cluster']
*/
}

require_once('vendor/autoload.php');

// The client ID assigned to you by Twinfield.
$twin_client_id = 'SomeClientId';

// The client secret assigned to you by Twinfield.
$twin_client_secret = 'SomeClientSecret';

/* The FQDN URI where this script can be called from when Twinfield redirects back to after the user logs in.
* Must be filled out on the form (Redirect URL) when requesting the client ID/Secret from Twinfield.
*/
$twin_redirect_uri = 'https://example.org/twinfield/Authorization.php';

$provider = new \PhpTwinfield\Secure\Provider\OAuthProvider([
'clientId' => $twin_client_id,
'clientSecret' => $twin_client_secret,
'redirectUri' => $twin_redirect_uri,
]);

// If we don't have an authorization code then get one.
if (!isset($_GET['code'])) {
//Optionally limit your scope if you don't require all.
$options = [
'scope' => ['twf.user','twf.organisation','twf.organisationUser','offline_access','openid']
];

$authorizationUrl = $provider->getAuthorizationUrl($options);

// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();

// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack.
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
if (isset($_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
}

exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

/* Twinfield's refresh token is valid for 550 days.
* Renewing the refresh token can be done by requesting the user to reload this page and logging into Twinfield before the refresh token is invalidated after 550 days.
* NOTE: Remember to put in place functionality to request the user to renew their authorization, see RenewAuthorization.php for an example.
*/

$refresh_expiry = strtotime(date('Ymd') . " +550 days");

// Save refresh token and refresh token expiry time to storage.
$refreshTokenStorage = array();
$refreshTokenStorage['refresh_token'] = $accessToken->getRefreshToken();
$refreshTokenStorage['refresh_expiry'] = $refresh_expiry;

SaveRefreshTokenToStore($refreshTokenStorage);

/* Optionally save access token, access token expiry time and access cluster to storage.
* If you choose to use an always valid access token to login to speed up your requests you need to put in place functionality to automatically renew the access token using the save refresh token.
* As the access token is valid for 60 minutes you need to create a task scheduler/cron that runs at least once every hour, see RenewAccessToken.php for an example.
*/

$validationUrl = "https://login.twinfield.com/auth/authentication/connect/accesstokenvalidation?token=";
$validationResult = @file_get_contents($validationUrl . urlencode($accessToken->getToken()));

if ($validationResult !== false) {
$resultDecoded = \json_decode($validationResult, true);
$accessTokenStorage = array();
$accessTokenStorage['access_token'] = $accessToken->getToken();
$accessTokenStorage['access_expiry'] = $accessToken->getExpires();
$accessTokenStorage['access_cluster'] = $resultDecoded["twf.clusterUrl"];

SaveAccessTokenToStore($accessTokenStorage);
}
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}
263 changes: 263 additions & 0 deletions examples/BrowseData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
<?php

/* BrowseData
* API Documentation: https://c3.twinfield.com/webservices/documentation/#/ApiReference/Request/BrowseData
*/

//Optionally declare the namespace PhpTwinfield so u can call classes without prepending \PhpTwinfield\
namespace PhpTwinfield;

// Use the ResponseException class to handle errors when listing, getting and sending objects to/from Twinfield
use PhpTwinfield\Response\ResponseException;

require_once('vendor/autoload.php');

// Retrieve an OAuth 2 connection
require_once('Connection.php');

/* BrowseData API Connector
* \PhpTwinfield\ApiConnectors\BrowseDataApiConnector
* Available methods: getBrowseData, getBrowseDefinition, getBrowseFields
*/

// Run all or only some of the following examples
$executeListBrowseDefinition = false;
$executeListBrowseFields = false;
$executeGetBrowseData = true;

$browseDataApiConnector = new \PhpTwinfield\ApiConnectors\BrowseDataApiConnector($connection);

/* Codes of all available Browse Definitions
Browse code Description
000 General ledger transactions
010 Transactions still to be matched
020 Transaction list
100 Customer transactions
200 Supplier transactions
300 Project transactions
301 Asset transactions
400 Cash transactions
410 Bank transactions
900 Cost centers
030_1 General Ledger (details)
030_2 General Ledger (details) (v2)
031 General Ledger (intercompany)
040_1 Annual Report (totals)
050_1 Annual Report (YTD)
060 Annual Report (totals multicurrency)
130_1 Customers
130_2 Customers (v2)
164 Credit Management
230_1 Suppliers
230_2 Suppliers (v2)
302_1 Fixed Assets
610_1 Time & Expenses (Totals)
620 Time & Expenses (Multicurrency)
650_1 Time & Expenses (Details)
651_1 Time & Expenses (Totals per week)
652_1 Time & Expenses (Totals per period)
660_1 Time & Expenses (Billing details)
661_1 Time & Expenses (Billing per week)
662_1 Time & Expenses (Billing per period)
670 Transaction summary
680 Bank link details
690 Vat Return status
700 Hierarchy access
*/

/* BrowseDefinition
* \PhpTwinfield\BrowseDefinition
* Available getters: getCode, getColumns, getName, getOffice, getShortName, isVisible
* Available setters: setCode, setName, setOffice, setShortName, setVisible, addColumn, removeColumn
*/

/* BrowseColumn
* \PhpTwinfield\BrowseColumn
* Available getters: getField, getFrom, getId, getLabel, getOperator, getTo, isAsk, isVisible
* Available setters: setAsk, setField, setFrom, setId, setLabel, setOperator, setTo, setVisible
*/

if ($executeListBrowseDefinition) {
try {
$browseDefinition = $browseDataApiConnector->getBrowseDefinition('000');
} catch (ResponseException $e) {
$browseDefinition = $e->getReturnedObject();
}

echo "<pre>";
print_r($browseDefinition);
echo "</pre>";
}

/* BrowseField
* \PhpTwinfield\BrowseField
* Available getters: getCode, getDataType, getFinder, getOptions, isCanOrder
* Available setters: setCanOrder, setCode, setDataType, setFinder, addOption
*/

/* BrowseFieldOption
* \PhpTwinfield\BrowseFieldOption
* Available getters: getCode, getName
* Available setters: setCode, setName
*/

if ($executeListBrowseFields) {
try {
$browseFields = $browseDataApiConnector->getBrowseFields();
} catch (ResponseException $e) {
$browseFields = $e->getReturnedObject();
}

echo "<pre>";
print_r($browseFields);
echo "</pre>";
}

/* BrowseSortField
* \PhpTwinfield\BrowseSortField
* Available getters: getCode, getOrder
* Available setters: setCode, setOrder
*/

/* BrowseData
* \PhpTwinfield\BrowseData
* Available getters: getFirst, getHeaders, getLast, getRows, getTotal
* Available setters: setFirst, setLast, setTotal, addHeader, addRow
*/

/* BrowseDataHeader
* \PhpTwinfield\BrowseDataHeader
* Available getters: getCode, getLabel, getType, isHideForUser
* Available setters: setCode, setHideForUser, setLabel, setType
*/

/* BrowseDataRow
* \PhpTwinfield\BrowseDataRow
* Available getters: getCells, getCode, getLine, getNumber, getOffice
* Available setters: setCode, setLine, setNumber, setOffice, addCell
*/

/* BrowseDataCell
* \PhpTwinfield\BrowseDataCell
* Available getters: getField, getType, getValue, isHideForUser
* Available setters: setField, setHideForUser, setType, setValue
*/

if ($executeGetBrowseData) {
// First, create the columns that you want to retrieve (see the browse definition for which columns are available)
$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.head.yearperiod')
->setLabel('Period')
->setVisible(true)
->setAsk(true)
->setOperator(\PhpTwinfield\Enums\BrowseColumnOperator::BETWEEN())
->setFrom('2018/01')
->setTo('2050/12');

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.head.code')
->setLabel('Transaction type')
->setVisible(true);

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.head.shortname')
->setLabel('Name')
->setVisible(true);

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.head.number')
->setLabel('Trans. no.')
->setVisible(true);

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.line.dim1')
->setLabel('General ledger')
->setVisible(true)
->setAsk(true)
->setOperator(\PhpTwinfield\Enums\BrowseColumnOperator::BETWEEN())
->setFrom('1300')
->setTo('1300');

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.head.curcode')
->setLabel('Currency')
->setVisible(true);

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.line.valuesigned')
->setLabel('Value')
->setVisible(true);

$columns[] = (new \PhpTwinfield\BrowseColumn())
->setField('fin.trs.line.description')
->setLabel('Description')
->setVisible(true);

// Second, create sort fields
$sortFields[] = new \PhpTwinfield\BrowseSortField('fin.trs.head.code');

try {
// Get the browse data
$browseData = $browseDataApiConnector->getBrowseData('000', $columns, $sortFields);
} catch (ResponseException $e) {
$browseData = $e->getReturnedObject();
}

echo "<pre>";
print_r($browseData);
echo "</pre>";

echo "Browse Data<br />";
echo "First: {$browseData->getFirst()}<br />";
echo "Last: {$browseData->getLast()}<br />";
echo "Total: {$browseData->getTotal()}<br /><br />";

$browseDataHeaders = $browseData->getHeaders();
$tableHeader = array("Result #", "Office", "Code", "Number", "Line");

foreach ($browseDataHeaders as $browseDataHeader) {
$tableHeader[] = $browseDataHeader->getLabel();
}

$browseDataRows = $browseData->getRows();

foreach ($browseDataRows as $key => $browseDataRow) {
$tableRows[$key][] = $key;
$tableRows[$key][] = $browseDataRow->getOffice();
$tableRows[$key][] = $browseDataRow->getCode();
$tableRows[$key][] = $browseDataRow->getNumber();
$tableRows[$key][] = $browseDataRow->getLine();

$browseDataCells = $browseDataRow->getCells();

foreach ($browseDataCells as $browseDataCell) {
$tableRows[$key][] = $browseDataCell->getValue();
}
}

?>
<table>
<tr>
<?php foreach ($tableHeader as $tableHeaderColumn) { echo "<th>{$tableHeaderColumn}</th>"; } ?>
</tr>
<?php foreach ($tableRows as $tableRow) { echo "<tr>"; foreach ($tableRow as $tableColumn) { echo "<td>{$tableColumn}</td>"; } echo "</tr>"; } ?>
</table>
<style type="text/css">
table {
width:100%;
border-collapse:collapse;
}
table td {
padding:7px; border:#4e95f4 1px solid;
}

table tr:nth-child(odd) {
background: #b8d1f3;
}

table tr:nth-child(even) {
background: #dae5f4;
}
</style>
<?php
}
Loading