PHPackages                             daursu/xero - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. daursu/xero

ActiveLibrary[API Development](/categories/api)

daursu/xero
===========

An elegant Laravel 4 wrapper for the official Xero API.

v0.2(11y ago)23008[3 issues](https://github.com/Daursu/xero/issues)[1 PRs](https://github.com/Daursu/xero/pulls)MITPHPPHP &gt;=5.4.0

Since Nov 6Pushed 10y ago4 watchersCompare

[ Source](https://github.com/Daursu/xero)[ Packagist](https://packagist.org/packages/daursu/xero)[ Docs](http://www.softwareassistance.net)[ RSS](/packages/daursu-xero/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Xero API wrapper for Laravel 4
==============================

[](#xero-api-wrapper-for-laravel-4)

[![Latest Stable Version](https://camo.githubusercontent.com/5e98e2bdc2fcf9dceae083fdaa6879ccd265f60e02888abe4d30e2471486ae15/68747470733a2f2f706f7365722e707567782e6f72672f6461757273752f7865726f2f762f737461626c652e737667)](https://packagist.org/packages/daursu/xero) [![Total Downloads](https://camo.githubusercontent.com/a1d119b8d892e4c77b4e58bf6cfa9c6846c346344a0a4dc59f902c467abb3460/68747470733a2f2f706f7365722e707567782e6f72672f6461757273752f7865726f2f646f776e6c6f6164732e737667)](https://packagist.org/packages/daursu/xero) [![Latest Unstable Version](https://camo.githubusercontent.com/13b17c6180ae3f9ac7fa4fe2ec1c75de5f4bd0de9f649e5cfa7dfc73279a65c9/68747470733a2f2f706f7365722e707567782e6f72672f6461757273752f7865726f2f762f756e737461626c652e737667)](https://packagist.org/packages/daursu/xero) [![License](https://camo.githubusercontent.com/7a85d68103143ffe540dbb35ae2bea6e802a5e2281b6ad1748fc0fbc1dfc1e54/68747470733a2f2f706f7365722e707567782e6f72672f6461757273752f7865726f2f6c6963656e73652e737667)](https://packagist.org/packages/daursu/xero)

This is a wrapper for the official Xero API php library, found at:

Note: I have not implemented the entire API, instead I have created the core logic which can then be extended (see the examples below).

I have tested the library only with a Private app, but it should work for the others.

---

Installation
------------

[](#installation)

Using composer simply add this to your composer.json file:

```
"require": {
  "daursu/xero": "dev-master"
}

```

Use composer to install this package.

```
$ composer update

```

### Registering the Package

[](#registering-the-package)

Register the service provider within the `providers` array found in `app/config/app.php`:

```
'providers' => array(
	// ...

	'Daursu\Xero\XeroServiceProvider',
)
```

### Publish the configuration file

[](#publish-the-configuration-file)

```
php artisan config:publish daursu/xero

```

This should create a new file in `app/config/packages/daursu/xero/config.php`. Update this file with your own settings and API key. There is also a folder called `certs`, where I recommend you to put your certificates.

Here is a guide how to generate your public/private key

Usage
-----

[](#usage)

The syntax is very simillar to the Laravel Eloquent one.

```
use \Daursu\Xero\Models\Invoice;
use \Daursu\Xero\Models\Contact;

// Retrieve all the invoices
$invoices = Invoice::get();

foreach ($invoices as $invoice) {
    print_r($invoice->toArray());
    print_r($invoice->getId());
    print_r($invoice->InvoiceID);
}

// Retrive a single invoice
$invoice = Invoice::find("0263f2bd-5825-476b-b6cf-6b76896a8cff");
var_dump($invoice);

// The get method also takes additional parameters
$contact = Contact::get(array('where' => 'Name.Contains("Dan")'));
```

#### Create or update a record

[](#create-or-update-a-record)

This is pretty straight forward as well.

```
use \Daursu\Xero\Models\Invoice;
use \Daursu\Xero\Models\Contact;

// Initialize from an array
$invoice = new Invoice(array(
    'Type' => 'ACCREC',
    'Status' => 'DRAFT',
    'Date' => date('Y-m-d'),
    ...
));

// Now you will need to attach a contact to the invoice
// Note that this time I am not passing an array to the constructor,
// this is just another way you can initialize objects
$contact = new Contact();
$contact->Name = "John Smith";

// Now you can assign it like this
$invoice->Contact = $contact;

// or
$invoice->setRelationship($contact);

// Save the invoice
$invoice->save(); // returns true or false

// Other methods
$invoice->update();
$invoice->create();

print_r($invoice->toArray()); // should have all the properties populated once it comes back from Xero
```

#### Collections

[](#collections)

Collections are used when you need to specify multiple relationships (ie. A contact might have multiple addresses.

```
use \Daursu\Xero\Models\Contact;
use \Daursu\Xero\Models\Address;

$contact = new Contact;
$contact->name = "John";

// IMPORTANT: A collection can only contain a single type of model
// in this case it can only hold addresses.
$collection = Address::newCollection(array(
			array('AddressType' => 'NEW', 'AddressLine1' => 'Cambridge', 'AddressLine2' => 'England'),
			array('AddressType' => 'OTHER', 'AddressLine1' => 'London', 'AddressLine2' => 'England'),
		));

// Push an new item
$collection->push(array('AddressType' => 'STREET', 'AddressLine1' => 'Street', 'AddressLine2' => 'England'));

// Push an existing object
$address = new Address(array('AddressType' => 'OBJECT', 'AddressLine1' => 'Oxford', 'AddressLine2' => 'England'));
$collection->push($address);

// Now set the relationship
$contact->setRelationship($collection);

// Or like this
$contact->Addresses = $collection;

// Save the contact
$contact->save();
```

#### Output methods

[](#output-methods)

```
// You can output an object using different methods
$address->toArray();
$address->toJson();
$address->toXML();
```

#### Extend the library

[](#extend-the-library)

I have not implemented all the models that Xero provides, however it is very easy to implement. Here is an example of adding a new model called `CreditNote`.

```
// File CreditNote.php
