Home

Awesome

Billy PHP SDK

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

PHP SDK for Billy API version 2 only from the Danish accounting program Billy.

Getting started

Before doing anything you should register yourself with Billy and get access credentials.

Installation

Composer

Simply add a dependency on lsolesen/billy-php-sdk to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a dependency on Billy PHP SDK 2.1:

{
    "require": {
        "lsolesen/billy-php-sdk": "2.1.*"
    }
}

After running composer install, you can take advantage of Composer's autoloader in vendor/autoload.php.

Usage

Create a new client

First you should create a client instance that is authorized with api_key or provided by Billy.

<?php
use Billy\Client\Client as Billy_Client;
use Billy\Client\Request as Billy_Request;

try {
    $request = new Billy_Request($api_key);
    $client = new Billy_Client($request);
} catch (Exception $e) {
    //...
}
?>

Create and update contact

<?php
use Billy\Contacts\ContactRepository;

try {
    // @todo: This will probably end up becoming an object of its own.
    $persons = array(
        array(
            'name' => $name,
            'email' => $email,
        )
    );
    $contact = new Contact();
    $contact
        ->setName($name)
        ->set('phone', $phone)
        ->setCountryID($address['country'])
        ->set('street', $address['thoroughfare'])
        ->set('cityText', $address['locality'])
        ->set('stateText', $address['administrative_area'])
        ->set('zipcodeText', $address['postal_code'])
        ->set('contactNo', $profile_id)
        ->set('contactPersons', $persons);

    $repository = new ContactRepository($request);
    $created_contact = $repository->create($contact);

    $contact = $repository->getSingle($created_contact->getID());
    $contact
        ->setName($new_name);
    $repository->update($contact);
} catch (Exception $e) {
    //...
}
?>

Create and update product

<?php
use Billy\Products\ProductsRepository;

try {
    $prices = array();
    $prices[] = array(
        'currencyId' => 'DKK',
        'unitPrice' => '20.25',
    );
    $product = new Product();
    $product
        ->setAccount($billy_state_account_id)
        ->setProductNo($product_id)
        ->setSalesTaxRuleset($billy_vat_model_id)
        ->set('prices', $prices);

    $repository = new ProductRepository($request);
    $created_product = $repository->create($product);

    $product = $repository->getSingle($created_product->getID());
    $product
        ->setName($new_name);
    $repository->update($product);
} catch (Exception $e) {
    //...
}
?>

Create an invoice

<?php
use Billy\Invoices\InvoicesRepository;

try {
    $invoice_line_items = array();

    $invoice_line = new InvoiceLine();
    $invoice_line->setProductID($product->getID())
        ->setQuantity(4)
        ->set('priority', $priority)
        ->setDescription('My description')
        ->setUnitPrice(20.25);

    $invoice_line_items[] = $invoice_line->toArray();

    $new_invoice = new Billy_Invoice();
    $new_invoice->setType('invoice')
        ->setOrderNumber($order_number)
        ->setContactID($contact->getID())
        ->setContactMessage($contact_message)
        ->setEntryDate($entry_date)
        ->setPaymentTermsDays(8)
        ->setCurrencyID('DKK')
        ->set('lines', $invoice_line_items);

    $created_invoice = $repository->create($new_invoice);
    $billy_invoice_id = $created_invoice->getID();
} catch (Exception $e) {
    //...
}
?>