1. Go to this page and download the library: Download hei/accounting-connector 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/ */
// 1. Send the customer to the provider. $state is a CSRF nonce you generate,
// store (session, cache), and check on the way back.
return redirect($connector->authorizationUrl($state));
// 2. In the callback, after checking $state, trade the code for tokens.
// Intuit puts the company (realm) id in the callback query; pass it through.
$result = $connector->exchangeCode(
$request->query('code'),
$request->query(), // carries realmId for QuickBooks; harmless for Xero
);
// 3. A Xero user signed in to several organisations may have authorised more
// than one. Ask which to use before assuming the first.
if ($result->needsTenantSelection()) {
// show $result->tenants, then call toConnection($chosenTenantId, ...)
}
// 4. Turn the result into a Connection and store it. reference is YOUR tenant id;
// it is what the shipped repository stores the row against.
$connection = $result->toConnection(settings: [], reference: (string) $organization->id);
app(ConnectionRepository::class)->save($connection);
// 5. Confirm to the customer which company they connected. A bookkeeper who
// picked the wrong one of six finds out now, not when the first transaction
// lands in a stranger's ledger.
$info = $connector->tenantInfo($connection);
$result = $connector->attach(
EntityType::Bill,
$invoiceId,
AttachmentSet::of($pdf, $png), // first one that fits wins
$connection,
);
if (! $result->uploaded) {
logger()->warning('Receipt not attached', ['reason' => $result->reason]);
}
$accounts = $connector->chartOfAccounts($connection);
$expense = Account::only($accounts, AccountClass::Expense); // Xero EXPENSE/DIRECTCOSTS/OVERHEADS
$income = Account::only($accounts, AccountClass::Revenue); // QBO Income/Other Income
$banks = $connector->bankAccounts($connection); // no extra request