|
| 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 | +} |
0 commit comments