PHP code example of picqer / moneybird-php-client

1. Go to this page and download the library: Download picqer/moneybird-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

picqer / moneybird-php-client example snippets




ection = new \Picqer\Financials\Moneybird\Connection();
$connection->setRedirectUrl('REDIRECTURL');
$connection->setClientId('CLIENTID');
$connection->setClientSecret('CLIENTSECRET');
$connection->redirectForAuthorization();

$administrations = $moneybird->administration()->getAll();



ection = new \Picqer\Financials\Moneybird\Connection();
$connection->setRedirectUrl('REDIRECTURL');
$connection->setClientId('CLIENTID');
$connection->setClientSecret('CLIENTSECRET');

// Get authorization code as described in readme (always set this when available)
$connection->setAuthorizationCode('AUTHORIZATIONCODE');

// Set this in case you got the access token, otherwise client will fetch it (always set this when available)
$connection->setAccessToken('ACCESSTOKEN');

try {
    $connection->connect();
} catch (\Exception $e) {
    throw new Exception('Could not connect to Moneybird: ' . $e->getMessage());
}

// After connection save the last access token for reuse 
$connection->getAccessToken(); // will return the access token you need to save

// Set up a new Moneybird instance and inject the connection
$moneybird = new \Picqer\Financials\Moneybird\Moneybird($connection);

// Example: Get administrations and set the first result as active administration
$administrations = $moneybird->administration()->getAll();
$connection->setAdministrationId($administrations[0]->id);

// Example: Fetch list of salesinvoices 
$salesInvoices = $moneybird->salesInvoice()->get();
var_dump($salesInvoices); // Array with SalesInvoice objects

// Example: Fetch a sales invoice
$salesInvoice = $moneybird->salesInvoice()->find(3498576378625);
var_dump($salesInvoice); // SalesInvoice object

// Example: Get sales invoice PDF contents
$pdfContents = $salesInvoice->download();

// Example: Create credit invoice based on existing invoice
$creditInvoice = $salesInvoice->duplicateToCreditInvoice();
var_dump($creditInvoice); // SalesInvoice object

// Example: Create a new contact
$contact = $moneybird->contact();

$contact->company_name = 'Picqer';
$contact->firstname = 'Stephan';
$contact->lastname = 'Groen';
$contact->save();
var_dump($contact); // Contact object (as saved in Moneybird)

// Example: Update existing contact, change email address
$contact = $moneybird->contact()->find(89672345789233);
$contact->email = '[email protected]';
$contact->save();
var_dump($contact); // Contact object (as saved in Moneybird)

// Example: Use the Moneybird synchronisation API
$contactVersions = $moneybird->contact()->listVersions();
var_dump($contactVersions); // Array with ids and versions to compare to your own

// Example: Use the Moneybird synchronisation API to get new versions of specific ids
$contacts = $moneybird->contact()->getVersions([
  2389475623478568,
  2384563478959922
]);
var_dump($contacts); // Array with two Contact objects

// Example: List sales invoices that are in draft (max 100)
$salesInvoices = $moneybird->salesInvoice()->filter([
  'state' => 'draft'
]);
var_dump($salesInvoices); // Array with filtered SalesInvoice objects

// Example: Get import mappings for contacts
$mappings = $moneybird->importMapping()->setType('contact')->get();
var_dump($mappings); // Array with ImportMapping objects

// Example: Register a payment for a sales invoice
$salesInvoicePayment = $moneybird->salesInvoicePayment();
$salesInvoicePayment->price = 153.75;
$salesInvoicePayment->payment_date = '2015-12-03';

$salesInvoice = $moneybird->salesInvoice()->find(3498576378625);
$salesInvoice->registerPayment($salesInvoicePayment);

// How to add SalesInvoiceDetails (invoice lines) to a SalesInvoice
$salesInvoiceDetailsArray = [];

foreach ($invoiceLines as $invoiceLine) { // Your invoice lines
   $salesInvoiceDetail = $moneybird->salesInvoiceDetail();
   $salesInvoiceDetail->price = 34.33;
   ...

   $salesInvoiceDetailsArray[] = $salesInvoiceDetail;
}

$salesInvoice = $moneybird->salesInvoice();
$salesInvoice->details = $salesInvoiceDetailsArray;


composer