Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.

Examples

pluijm edited this page Sep 13, 2010 · 48 revisions

Create a contact

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$contact = new MoneybirdContact;
$contact->name = 'Testcontact';
$contact->contact_name = 'Contactpersoon';
$contact->address1 = 'Adresregel1';
$contact->address2 = 'Adresregel2';
$contact->zipcode = '1234AA';
$contact->city = 'Amsterdam';
$contact->country = 'Nederland';
$contact->email = 'moneybirdtest@mydomain.tld';
$contact->phone = '0612345678';
//
$contact = $mbapi->saveContact($contact);
echo 'Contact ID: '.$contact->id;
?>

Edit a contact

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$contact = $mbapi->getContact(4741);
$contact->name = 'New name;
$contact = $mbapi->saveContact($contact);
// Or:
$contact->save();
?>

Delete a contact

Note: this is only possible if the contact has no invoices

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$contact = $mbapi->getContact(4741);
$mbapi->deleteContact($contact);
// Or:
$contact->delete();
?>

Create an invoice

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$contact = $mbapi->getContact(4741);
//
$invoice = new MoneybirdInvoice;
$invoice->setContact($contact);
//
$lines = array();
$invoiceLine = new MoneybirdInvoiceLine($invoice);
$invoiceLine->description = 'Invoice line descr';
$invoiceLine->amount = 2;
$invoiceLine->price = 8.4;
$invoiceLine->tax = 0.19;
$lines[] = $invoiceLine;
// Repeat for more lines
//
$invoice->details = $lines;
//
$invoice = $mbapi->saveInvoice($invoice);
echo 'Invoice ID: '.$invoice->id;
?>

Create an invoice

Alternative

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$contact = $mbapi->getContact(4741);
//
$invoice = new MoneybirdInvoice;
//
$lines = array();
$invoiceLine = new MoneybirdInvoiceLine($invoice);
$invoiceLine->description = 'Invoice line descr';
$invoiceLine->amount = 2;
$invoiceLine->price = 8.4;
$invoiceLine->tax = 0.19;
$lines[] = $invoiceLine;
// Repeat for more lines
//
$invoice->details = $lines;
//
$invoice = $mbapi->saveInvoice($invoice);
echo 'Invoice ID: '.$invoice->id;
?>

Edit an invoice

Note: only possible if the invoice has not been sent

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$invoice = $mbapi->getInvoice(7577);
//
$invoiceLines = $invoice->details;
$invoiceLine = new MoneybirdInvoiceLine();
$invoiceLine->description = 'New invoice line descr';
$invoiceLine->amount = 5;
$invoiceLine->price = 8.4;
$invoiceLine->tax = 0.19;
$invoiceLines[] = $invoiceLine;
$invoice->details = $invoiceLines;
//
$mbapi->saveInvoice($invoice);
// Or:
$invoice->save();
?>

Delete an invoice

Note: only possible if the invoice has not been sent

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$invoice = $mbapi->getInvoice(7577);
$mbapi->deleteInvoice($invoice);
// Or:
$invoice->delete();
?>

Send an invoice

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$invoice = $mbapi->getInvoice(7577);
$sendInfo = new MoneybirdInvoiceSendInformation(
    'email',
    'email@mydomain.tld', // Optional
    'Message in e-mail'   // Optional
);
//
$mbapi->sendInvoice($sendInfo);
// Or:
$invoice->send($sendInfo);
?>

Send a reminder

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$invoice = $mbapi->getInvoice(7577);
$sendInfo = new MoneybirdInvoiceSendInformation(
    'email',
    'email@mydomain.tld', // Optional
    'Message in e-mail'   // Optional
);
//
$mbapi->sendInvoiceReminder($sendInfo);
// Or:
$invoice->remind($sendInfo);
?>

Register a payment

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
$invoice = $mbapi->getInvoice(7577);
$payment = new MoneybirdInvoicePayment();
$payment->price = 10.94;
$payment->payment_method = 'bank_transfer';
$payment->payment_date = new DateTime();
//
$mbapi->registerInvoicePayment($payment);
// Or:
$invoice->registerPayment($payment);
?>

Getting invoices using an advanced filter

<?php
$mbapi = new MoneybirdApi('client', 'user', 'pass');
//
$filter = new MoneybirdAdvancedInvoiceFilter(
    new DateTime('2009-06-23'),
    new DateTime('2009-06-23'),
    'open'
);
//
$invoices = $mbapi->getInvoices($filter);
foreach ($invoices as $invoice)
{
    echo $invoice->invoice_id.PHP_EOL;
}
?>

Error handling

In case of an error, an exception will be thrown. All exceptions thrown by this library extend the MoneybirdException. For a list of exceptions, see Exceptions.php.

<?php
try
{
    $mbapi = new MoneybirdApi('client', 'user', 'pass');
}
catch (MoneybirdAuthorizationRequiredException $e)
{
    // Authorization failure!
}
//
try
{
    $invoice = $mbapi->getInvoice(1); // Unexisting invoice
}
catch (MoneybirdItemNotFoundException $e)
{
    // Invoice not found
}
?>

When the MoneybirdUnprocessableEntityException exception is thrown, an array of errors is available:
bc. <?php
print_r($mbapi→getErrorMessages());
?>

Clone this wiki locally