laravel-xero icon indicating copy to clipboard operation
laravel-xero copied to clipboard

Contact Filter on AccountNumber doesn't filter contacts but returns all records.

Open helloadmin opened this issue 1 year ago • 1 comments

I'm using the following code Xero::contacts()->get(1, 'AccountNumber="10307"') and returns a collection of all contacts and not just the single contact where AccountNumber = 10307

When I use the more raw method of using the verbs, I get the intended result. See below. $endpoint = 'Contacts'; $query = ['where' => 'AccountNumber="' . $accountNumber . '"',]; $result = Xero::get($endpoint, $query);

Is this a bug with Xero::contacts()->get(1, 'AccountNumber="10307"')? The format looks simple and aligns with the examples provided.

helloadmin avatar Nov 05 '23 18:11 helloadmin

@helloadmin

I was able to replicate this issue

filtering by email address returns all contact

Xero::contacts()->get(null, 'EmailAddress="email_address_here"')

I was able to find the cause:

In the guzzle method

your calling get this way ->$type(self::$baseUrl . $request, $data), which means data is empty and the query parameters for the "where" filter is concatenated with the baseUrl like here:

https://api.xero.com/api.xro/2.0/contacts?where=EmailAddress%3D%sample%40gmail.com%22

which does not work because upon inspection the query parameter does not get included in the request

The query is supposed to be something like this to work

$get('https://api.xero.com/api.xro/2.0/contacts', ['where' => 'EmailAddress="[email protected]"']);

The workaround for this is something like this:

$response = Http::withToken(Xero::getAccessToken())
  ->withHeaders(["Xero-tenant-id" => Xero::getTenantId()])
  ->get("https://api.xero.com/api.xro/2.0/Contacts", [
    "where" => 'EmailAddress="[email protected]"'
  ]);

For inspecting the request object you can do something like this before calling Xero and Http

Http::globalRequestMiddleware(function ($request) {
  dump($requeest)
  return $request;
});

Notice that the query attribute of the request object when using Xero::get returns empty, but if you separate the query parameter for get just like suggested above, the request object shows query value.

I can create a PR if you like, but it will take some time since I have my own integration that I need to finish. I guess this is not just happening on contact but on invoices as well if you try to use the where filter.

@dcblogdev

By the way, Thanks a lot for the package helps me a lot to speed up integrations especially how you handle token renewal, it was helpful.

twocngdagz avatar Feb 07 '24 04:02 twocngdagz

thanks, @twocngdagz really helpful reply!

Working on improving the filtering and managed to get this working without altering the public API

Xero::contacts()->get(1, ['where' => 'ContactID==Guid("74ea95ea-6e1e-435d-9c30-0dff8ae1bd80")']);

Still testing this out.

dcblogdev avatar May 02 '24 20:05 dcblogdev

addressed this using a new filter option https://github.com/dcblogdev/laravel-xero/pull/61

dcblogdev avatar May 03 '24 16:05 dcblogdev