Skip to content

Commit 908d73f

Browse files
committed
First setup of Moneybird V2 client
0 parents  commit 908d73f

40 files changed

+2561
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
composer.phar
2+
composer.lock
3+
vendor/
4+
.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Picqer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# moneybird-php-client
2+
3+
PHP Client for Moneybird V2
4+
5+
**Work in progress!**
6+
7+
## Installation
8+
Will be on Packagist/Composer soon.
9+
10+
## Usage
11+
You need to have to following credentials and information ready. You can get this from your Moneybird account.
12+
- Client ID
13+
- Client Secret
14+
- Callback URL
15+
16+
You need to be able to store some data locally:
17+
- The three credentials mentioned above
18+
- Authorizationcode
19+
- Accesstoken
20+
21+
### Authorization code
22+
If you have no authorization code yet, you will need this first. The client supports fetching the authorization code as follows.
23+
24+
```php
25+
<?php
26+
27+
$connection = new \Picqer\Financials\Moneybird\Connection();
28+
$connection->setRedirectUrl(REDIRECTURL);
29+
$connection->setClientId(CLIENTID);
30+
$connection->setClientSecret(CLIENTSECRET);
31+
$connection->redirectForAuthorization();
32+
```
33+
34+
This will perform a redirect to Moneybird at which you can login and authorize the app for a specific Moneybird administration.
35+
After login, Moneybird will redirect you to the callback URL with request param "code" which you should save as the authorization code.
36+
37+
### Normal actions
38+
After you have the authorization code as described above, you can perform normal requests. The client will take care of the accesstoken
39+
automatically.
40+
41+
```php
42+
<?php
43+
44+
$connection = new \Picqer\Financials\Moneybird\Connection();
45+
$connection->setRedirectUrl(REDIRECTURL);
46+
$connection->setClientId(CLIENTID);
47+
$connection->setClientSecret(CLIENTSECRET);
48+
49+
// Get authorization code as described in readme (always set this when available)
50+
$connection->setAuthorizationCode(AUTHORIZATIONCODE);
51+
52+
// Set this in case you got the access token, otherwise client will fetch it (always set this when available)
53+
$connection->setAccessToken(ACCESSTOKEN);
54+
55+
try {
56+
$connection->connect();
57+
} catch (\Exception $e) {
58+
throw new Exception('Could not connect to Moneybird: ' . $e->getMessage());
59+
}
60+
61+
// After connection save the last access token for reuse
62+
$connection->getAccessToken(); // will return the access token you need to save
63+
64+
// Set up a new Moneybird instance and inject the connection
65+
$moneybird = new \Picqer\Financials\Moneybird\Moneybird($connection);
66+
67+
// Example: Fetch list of salesinvoices
68+
$salesInvoices = $moneybird->salesInvoice->get();
69+
var_dump($salesInvoices); // Array with SalesInvoice objects
70+
71+
// Example: Fetch a sales invoice
72+
$salesInvoice = $moneybird->salesInvoice->find(3498576378625);
73+
var_dump($salesInvoice); // SalesInvoice object
74+
75+
// Example: Create a new contact
76+
$contact = $moneybird->contact();
77+
78+
$contact->company_name = 'Picqer';
79+
$contact->firstname = 'Stephan';
80+
$contact->lastname = 'Groen';
81+
$contact->save();
82+
83+
// Example: Update existing contact, change email address
84+
$contact = $moneybird->contact()->find(89672345789233);
85+
$contact->email = '[email protected]';
86+
$contact->save();
87+
88+
// Example: Use the Moneybird synchronisation API
89+
$contactVersions = $moneybird->contact()->listVersions();
90+
var_dump($contactVersions); // Array with ids and versions to compare to your own
91+
92+
// Example: Use the Moneybird synchronisation API to get new versions of specific ids
93+
$contacts = $moneybird->contact()->getVersions([
94+
2389475623478568,
95+
2384563478959922
96+
]);
97+
var_dump($contacts); // Array with two Contact objects
98+
```
99+
100+
## Code example
101+
See for example: [example/example.php](example/example.php)
102+
103+
## TODO
104+
- Filtering
105+
- Receiving webhooks support (would be nice)
106+
- Some linked/nested entities (notes, attachments etcetera)
107+
- Dedicated Exception for RateLimit reached and return of Retry-After value

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "picqer/moneybird-php-client",
3+
"type": "library",
4+
"description": "A PHP Client for the Moneybird V2 API",
5+
"keywords": [
6+
"api",
7+
"php",
8+
"moneybird"
9+
],
10+
"homepage": "http://github.com/picqer/moneybird-php-client",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Stephan Groen",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.5.0",
20+
"guzzlehttp/guzzle": "~6.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Picqer\\Financials\\Moneybird\\": "src/Picqer/Financials/Moneybird"
28+
}
29+
}
30+
}

example/example.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
// Autoload composer installed libraries
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
// Set these params
7+
define('REDIRECTURL', 'http://example.org/callback.php');
8+
define('CLIENTID', '');
9+
define('CLIENTSECRET', '');
10+
11+
// Set the administrationid to work with
12+
$administrationId = '';
13+
14+
/**
15+
* Function to retrieve persisted data for the example
16+
* @param string $key
17+
* @return null|string
18+
*/
19+
function getValue($key)
20+
{
21+
$storage = json_decode(file_get_contents('storage.json'), true);
22+
if (array_key_exists($key, $storage)) {
23+
return $storage[$key];
24+
}
25+
return null;
26+
}
27+
28+
/**
29+
* Function to persist some data for the example
30+
* @param string $key
31+
* @param string $value
32+
*/
33+
function setValue($key, $value)
34+
{
35+
$storage = json_decode(file_get_contents('storage.json'), true);
36+
$storage[$key] = $value;
37+
file_put_contents('storage.json', json_encode($storage));
38+
}
39+
40+
/**
41+
* Function to authorize with Moneybird, this redirects to Moneybird login promt and retrieves authorization code
42+
* to set up requests for oAuth tokens
43+
*/
44+
function authorize()
45+
{
46+
$connection = new \Picqer\Financials\Moneybird\Connection();
47+
$connection->setRedirectUrl(REDIRECTURL);
48+
$connection->setClientId(CLIENTID);
49+
$connection->setClientSecret(CLIENTSECRET);
50+
$connection->redirectForAuthorization();
51+
}
52+
53+
/**
54+
* Function to connect to Moneybird, this creates the client and automatically retrieves oAuth tokens if needed
55+
*
56+
* @return \Picqer\Financials\Moneybird\Connection
57+
* @throws Exception
58+
*/
59+
function connect()
60+
{
61+
$connection = new \Picqer\Financials\Moneybird\Connection();
62+
$connection->setRedirectUrl(REDIRECTURL);
63+
$connection->setClientId(CLIENTID);
64+
$connection->setClientSecret(CLIENTSECRET);
65+
66+
// Retrieves authorizationcode from database
67+
if (getValue('authorizationcode')) {
68+
$connection->setAuthorizationCode(getValue('authorizationcode'));
69+
}
70+
71+
// Retrieves accesstoken from database
72+
if (getValue('accesstoken')) {
73+
$connection->setAccessToken(getValue('accesstoken'));
74+
}
75+
76+
// Make the client connect and exchange tokens
77+
try {
78+
$connection->connect();
79+
} catch (\Exception $e) {
80+
throw new Exception('Could not connect to Moneybird: ' . $e->getMessage());
81+
}
82+
83+
// Save the new tokens for next connections
84+
setValue('accesstoken', $connection->getAccessToken());
85+
86+
return $connection;
87+
}
88+
89+
// If authorization code is returned from Moneybird, save this to use for token request
90+
if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) {
91+
setValue('authorizationcode', $_GET['code']);
92+
}
93+
94+
// If we do not have a authorization code, authorize first to setup tokens
95+
if (getValue('authorizationcode') === null) {
96+
authorize();
97+
}
98+
99+
// Create the Moneybird client
100+
$connection = connect();
101+
$connection->setAdministrationId($administrationId);
102+
$moneybird = new \Picqer\Financials\Moneybird\Moneybird($connection);
103+
104+
// Get the contacts from our administration
105+
try {
106+
$contacts = $moneybird->salesInvoice()->get();
107+
108+
foreach ($contacts as $contact) {
109+
var_dump($contact);
110+
}
111+
} catch (\Exception $e) {
112+
echo get_class($e) . ' : ' . $e->getMessage();
113+
}

example/storage.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./vendor/autoload.php"
3+
colors="true">
4+
<testsuites>
5+
<testsuite>
6+
<directory>tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<filter>
10+
<whitelist>
11+
<directory suffix=".php">src</directory>
12+
</whitelist>
13+
</filter>
14+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Picqer\Financials\Moneybird\Actions;
2+
3+
/**
4+
* Class FindAll
5+
* @package Picqer\Financials\Moneybird\Actions
6+
*/
7+
trait FindAll
8+
{
9+
10+
/**
11+
* @return mixed
12+
*/
13+
public function get()
14+
{
15+
$result = $this->connection()->get($this->url);
16+
17+
return $this->collectionFromResult($result);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Picqer\Financials\Moneybird\Actions;
2+
3+
/**
4+
* Class FindOne
5+
* @package Picqer\Financials\Moneybird\Actions
6+
*/
7+
trait FindOne {
8+
9+
/**
10+
* @param $id
11+
* @return mixed
12+
*/
13+
public function find($id)
14+
{
15+
$result = $this->connection()->get($this->url . '/' . urlencode($id));
16+
17+
return $this->makeFromResponse($result);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace Picqer\Financials\Moneybird\Actions;
2+
3+
/**
4+
* Class Removable
5+
* @package Picqer\Financials\Moneybird\Actions
6+
*/
7+
trait Removable {
8+
9+
/**
10+
* @return mixed
11+
*/
12+
public function delete()
13+
{
14+
return $this->connection()->delete($this->url . '/' . urlencode($this->id));
15+
}
16+
17+
}

0 commit comments

Comments
 (0)