calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
360 stars 259 forks source link

Trying to get all Xero Contacts #838

Closed CarlAtIssl closed 2 years ago

CarlAtIssl commented 2 years ago

Hi

I've a Laravel system and we've already installed the xero-php plug in.

In my system, I have a Login button, which works and I store the information provded by Xero when authenticated in my session.

I now want to create a button called "Get All Contacts" (am providing that I can talk with Xero).

In my controller, I've added at the start:-

use Calcinai\OAuth2\Client\Provider\Xero;
use XeroPHP\Application;

My routine to get the contacts is as follows:

$clientId = $request->get('clientId');
$clientSecret = $request->get('clientSecret');
$redirectUri = 'https://dev.mydomain.co.uk/xeroCallback';

$provider = new Xero([
    'clientId'                => $clientId,
    'clientSecret'            => $clientSecret,
    'redirectUri'             => $redirectUri,
    'urlAuthorize'            => 'https://login.xero.com/identity/connect/authorize',
    'urlAccessToken'          => 'https://identity.xero.com/connect/token',
    'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation',
]);

$token = $provider->getAccessToken('authorization_code', [
    'code' => session('xero_code'),
]);

$identity = $provider->getResourceOwner($token);
$tenants = $provider->getTenants($token);

$xero = new \XeroPHP\Application($token->getToken(), $tenants[0]->tenantId);
$contacts = $xero->load(Contact::class)->execute();

The last line now gives the error:

Class does not exist [\XeroPHP\Models\XeroPHP\Models\Accoumting\Contact]

I've obviously done something wrong, can anyone help me fix this issue.

Thanks in advance for any help in fixing this. Regards Carl.

rodjsta commented 2 years ago

Your error shows at least one problem... the spelling of 'Accounting'.

CarlAtIssl commented 2 years ago

Your error shows at least one problem... the spelling of 'Accounting'.

Fixed that (oops - need better glasses)

Now the error reads

Class does not exist [\XeroPHP\Models\App\Http\Controllers\Contact]

I've obviously not set this up correct so apologise in advance for my code Regards Carl.

CarlAtIssl commented 2 years ago

Fixed it, by changing what I wanted to do, instead of all contacts, I decided to try to find an existing one.

I did the following:-

$clientId = $request->get('clientId');
$clientSecret = $request->get('clientSecret');
$redirectUri = 'https://dev.mydomain.co.uk/xeroCallback';

$provider = new Xero([
    'clientId'                => $clientId,
    'clientSecret'            => $clientSecret,
    'redirectUri'             => $redirectUri,
    'urlAuthorize'            => 'https://login.xero.com/identity/connect/authorize',
    'urlAccessToken'          => 'https://identity.xero.com/connect/token',
    'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation',
]);

$token = $provider->getAccessToken('authorization_code', [
    'code' => session('xero_code'),
]);

$identity = $provider->getResourceOwner($token);
$tenants = $provider->getTenants($token);

$xero = new \XeroPHP\Application($token->getToken(), $tenants[0]->tenantId);

$search = 'guid goes in here';

$contacts =  $xero->load('Accounting\Contact')
    ->where('ContactID',$search)
    ->execute();

$contact = $contacts->first();

Now I am working on extracting the returned data so that I can display it!

Cheers Carl.

calcinai commented 2 years ago

Just a quick note for anyone else that might come across this. The issue looks like it was caused by a missing use statement for the model. When the direct class syntax reference is used i.e. $xero->load(Contact::class), there needs to be a corresponding use XeroPHP\Accounting\Contact statement.

It looks like the error was resolved in this case because of the string model name: $xero->load('Accounting\Contact'), where XeroPHP will automatically resolve the model class.

CarlAtIssl commented 2 years ago

Hi Michael

Thanks for that, I've got round it now, but I think I will hit an issue with the 60 api calls per second limit, especially when creating new contacts in bulk.

Cheers Carl

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Michael Calcinai @.> Sent: Sunday, January 2, 2022 9:06:03 PM To: calcinai/xero-php @.> Cc: Carl Ogden @.>; State change @.> Subject: Re: [calcinai/xero-php] Trying to get all Xero Contacts (Issue #838)

Just a quick note for anyone else that might come across this. The issue looks like it was caused by a missing use statement for the model. When the direct class syntax reference is used i.e. $xero->load(Contact::class), there needs to be a corresponding use XeroPHP\Accounting\Contact statement.

It looks like the error was resolved in this case because of the string model name: $xero->load('Accounting\Contact'), where XeroPHP will automatically resolve the model class.

— Reply to this email directly, view it on GitHubhttps://github.com/calcinai/xero-php/issues/838#issuecomment-1003775162, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANWGWI3HFUFGGSXKZDE5HGLUUC43XANCNFSM5K5XDUXQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you modified the open/close state.Message ID: @.***>

CarlAtIssl commented 2 years ago

Hi Michael

I’m trying to check that a specific account code has been created in Xero, my code reads:-

    $branch->NomCodeTenancyInvoices = 1001;
    $branch->NomCodeTenantInvoices = 1002;
    $branch->NomCodeLandlordMgmtFees = 1003;
    $branch->NomCodeLandlordInvoices = 1004;
    $branch->invNumberPrefix = 'SO-';
    //
    // Check Nominal codes exist prior
    //
    $code_check = new LineItem($xero);
    $code_exists = $code_check->getAccountCode($branch->NomCodeTenancyInvoices);

But this fails with:-

array:1 [▼ "e" => ErrorException {#1333 ▼

message: "Undefined index: AccountCode"

#code: 0
#file: "/mnt/storage/standout/public_html/dev/vendor/calcinai/xero-php/src/XeroPHP/Models/Accounting/LineItem.php"
#line: 276
#severity: E_NOTICE
trace: {▼
  /mnt/storage/standout/public_html/dev/vendor/calcinai/xero-php/src/XeroPHP/Models/Accounting/LineItem.php:276 {▼
    XeroPHP\Models\Accounting\LineItem->getAccountCode() …
    › {
    ›     return $this->_data['AccountCode'];
    › }
    arguments: {▼
      $level: 8
      $message: "Undefined index: AccountCode"
      $file: "/mnt/storage/standout/public_html/dev/vendor/calcinai/xero-php/src/XeroPHP/Models/Accounting/LineItem.php"
      $line: 276
      $context: []
    }
  }

Is there a bug in the getAccountCode section where you’ve not put $value in the brackets and then used it within the routine?

If you prefer for me to open an issue, I can do!

Cheers Carl.

Kind Regards [Description: logo] Carl Ogden Support @ ISSLhttp://www.webdesign-issl.co.uk/ 0161 660 1982 http://www.issl.co.ukhttp://www.issl.co.uk/ Internet Solutions Services Limited, 85 Great Portland Street London W1W 7LT ISSL is registered in England and Wales, No 06029679. Registered Office: 167-169 Great Portland Street, 5th Floor, London, W1W 5PF

On 02/01/2022, 21:06, "Michael Calcinai" @.***> wrote:

Just a quick note for anyone else that might come across this. The issue looks like it was caused by a missing use statement for the model. When the direct class syntax reference is used i.e. $xero->load(Contact::class), there needs to be a corresponding use XeroPHP\Accounting\Contact statement.

It looks like the error was resolved in this case because of the string model name: $xero->load('Accounting\Contact'), where XeroPHP will automatically resolve the model class.

— Reply to this email directly, view it on GitHubhttps://github.com/calcinai/xero-php/issues/838#issuecomment-1003775162, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANWGWI3HFUFGGSXKZDE5HGLUUC43XANCNFSM5K5XDUXQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you modified the open/close state.Message ID: @.***>

calcinai commented 2 years ago

@CarlAtIssl Not 100% sure what you're trying to do there, but if you're trying to check if an account exists, you'll need to use the Account model and the ->load() method a as the one you're using is just the getter for the value of a LineItem.

As an aside, PHP doesn't show the parameters passed/function arguments. So regarding your question above, no. That error is because it's trying to fetch a value that does not exist. i.e. the AccountCode of a blank LineItem that you just created.

CarlAtIssl commented 2 years ago

Hi

Thanks for that, perfect! I was totally wrong and now have it working!

$xero_code = $xero->load('Accounting\Account')
    ->where('Code="'.$branch->NomCodeTenancyInvoices.'"')
    ->execute();
$code = $xero_code->first();

Kind regards Carl.