Projects that integrate with Xero and Laravel have a lot of the same boilerplate. Here is what we do.
You can install the package via composer:
composer require lukecurtis/laravel-xero-boilerplate
Under the hood this package uses xero-php. Add the following to your .env
file
XERO_CONSUMER_KEY=KEY-HERE
XERO_CONSUMER_SECRET=SECRET-HERE
XERO_RSA_PRIVATE_KEY='file:///home/vagrant/code/web-app/storage/privatekey.pem'
XERO_RSA_PUBLIC_KEY='file:///home/vagrant/code/web-app/storage/publickey.cer'
Once complete you can publish and configure the config file that has been published and also migrate
php artisan vendor:publish --provider="Lukecurtis\LaravelXeroBoilerplate\LaravelXeroBoilerplateServiceProvider"
php artisan migrate
Finally, once all authenticated, you'll need to sync your Contacts, Accounts & Items
php artisan sync:xero --dispatch_now
You'll notice the xero tables are now populated.
Next up is to tell the package which model is having invoices - we acheive this by adding the HasInvoice
trait. So for example if you have jobs in your system you might do the following:
namespace App\Models\Job;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Lukecurtis\LaravelXeroBoilerplate\HasInvoice\HasInvoices;
use Lukecurtis\LaravelXeroBoilerplate\HasInvoice\HasInvoicesTrait;
class Job extends Model implements HasInvoices
{
//...
use SoftDeletes,
HasInvoicesTrait;
//...
}
Now you can go ahead and $job->xeroInvoices
to get all invoices. Lets go ahead and create one:
use Lukecurtis\LaravelXeroBoilerplate\Models\Xero\XeroInvoice;
use Lukecurtis\LaravelXeroBoilerplate\Models\Xero\XeroLineItem;
$invoice = XeroInvoice::create([
'xero_contact_id' => $data['xero_contact_id'],
'type' => $data['type'],
'email' => $data['email'],
'due_date' => $data['due_date'],
'name' => $data['name'],
'status' => $data['status'],
]);
if ($invoice) {
foreach ($data['xero_line_items'] as $item) {
$item['xero_invoice_id'] = $invoice->id;
$xeroLineItem = XeroLineItem::create([
'xero_invoice_id' => $data['xero_invoice_id'],
'xero_item_id' => $data['xero_item_id'],
'description' => $data['description'],
'quantity' => $data['quantity'],
'xero_account_id' => $data['xero_account_id'],
'tax_type' => $data['tax_type'],
'unit_price' => $data['unit_price'],
'total' => number_format((($data['unit_price'] * $data['quantity']) / 100), 2, '.', '') * 100,
]);
}
}
Finally once your invoice is persisted you can link it like so
$job->addXeroInvoice($invoice);
This will send the invoice to xero, you can load the line items like so Invoice::with('xeroLineItems')
for example, like any other Laravel Model.
You don't need to link a xero invoice to a model if need be.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email lukesimoncurtis@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.