Implemented more customer functions
This commit is contained in:
parent
bbc747621e
commit
1567806b87
51
src/API.php
51
src/API.php
@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Cache;
|
|||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
use Intuit\Models\ProviderToken;
|
use Intuit\Models\ProviderToken;
|
||||||
use Intuit\Response\ListList;
|
use Intuit\Response\{Customer,ListList};
|
||||||
|
|
||||||
final class API
|
final class API
|
||||||
{
|
{
|
||||||
@ -77,19 +77,19 @@ final class API
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*
|
|
||||||
case 'POST':
|
case 'POST':
|
||||||
$request = $this->prepareRequestPost(
|
$request = $this->prepareRequestPost(
|
||||||
$url,
|
$url,
|
||||||
$parameters,
|
$parameters,
|
||||||
[
|
[
|
||||||
'accept: application/json',
|
'Accept' => 'application/json',
|
||||||
'Api-Request-Id: '.$request_id,
|
'Authorization' => sprintf('Bearer %s',$this->token->access_token),
|
||||||
'Api-Signature: '.$signature,
|
'Content-Type' => 'application/json',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
/*
|
||||||
case 'PUT':
|
case 'PUT':
|
||||||
$request = $this->prepareRequestPut(
|
$request = $this->prepareRequestPut(
|
||||||
$url,
|
$url,
|
||||||
@ -139,6 +139,21 @@ final class API
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific customer record
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param array $parameters
|
||||||
|
* @return Customer
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function getCustomer(int $id,array $parameters=[]): Customer
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('%s:Get a specific customer [%d]',static::LOGKEY,$id));
|
||||||
|
|
||||||
|
return new Customer($this->execute('customer/'.$id,$parameters));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of our clients
|
* Get a list of our clients
|
||||||
*
|
*
|
||||||
@ -165,7 +180,7 @@ final class API
|
|||||||
* @param array $headers
|
* @param array $headers
|
||||||
* @return \CurlHandle
|
* @return \CurlHandle
|
||||||
*/
|
*/
|
||||||
private function prepareRequest($url,array $parameters=[],array $headers=[]): \CurlHandle
|
private function prepareRequest(string $url,array $parameters=[],array $headers=[]): \CurlHandle
|
||||||
{
|
{
|
||||||
$request = curl_init();
|
$request = curl_init();
|
||||||
|
|
||||||
@ -184,4 +199,28 @@ final class API
|
|||||||
|
|
||||||
return $request;
|
return $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function prepareRequestPost(string $url,array $parameters=[],array $headers=[]): \CurlHandle
|
||||||
|
{
|
||||||
|
$request = $this->prepareRequest($url,$parameters,$headers);
|
||||||
|
|
||||||
|
curl_setopt($request,CURLOPT_CUSTOMREQUEST,'POST');
|
||||||
|
curl_setopt($request,CURLOPT_POSTFIELDS,json_encode($parameters));
|
||||||
|
|
||||||
|
return $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a customer
|
||||||
|
*
|
||||||
|
* @param array $parameters
|
||||||
|
* @return Customer
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function updateCustomer(array $parameters=[]): Customer
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('%s:Update customer',static::LOGKEY),['params'=>$parameters]);
|
||||||
|
|
||||||
|
return new Customer($this->execute('customer',array_merge($parameters,['method'=>'POST'])));
|
||||||
|
}
|
||||||
}
|
}
|
77
src/Jobs/AccountingCustomerUpdate.php
Normal file
77
src/Jobs/AccountingCustomerUpdate.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Jobs;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
use Intuit\Models\{Customer,ProviderToken};
|
||||||
|
|
||||||
|
class AccountingCustomerUpdate implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
private const LOGKEY = 'JAS';
|
||||||
|
|
||||||
|
private Customer $customer;
|
||||||
|
private ProviderToken $to;
|
||||||
|
private bool $forceprod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(ProviderToken $to,Customer $customer,bool $forcprod=FALSE)
|
||||||
|
{
|
||||||
|
$this->onQueue('intuit');
|
||||||
|
|
||||||
|
$this->customer = $customer;
|
||||||
|
$this->to = $to->withoutRelations();
|
||||||
|
$this->forceprod = $forcprod;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
Config::set('site',$this->to->site);
|
||||||
|
|
||||||
|
$api = $this->to->provider->API($this->to,$this->forceprod);
|
||||||
|
$updated = $api->updateCustomer(array_merge(
|
||||||
|
$this->customer->getDirty(),
|
||||||
|
[
|
||||||
|
'Id'=>$this->customer->id,
|
||||||
|
'SyncToken'=>$this->customer->synctoken,
|
||||||
|
]));
|
||||||
|
|
||||||
|
if (($x=$this->to->provider->accounts->where('pivot.ref',$updated->id))->count() === 1) {
|
||||||
|
$ao = $x->pop();
|
||||||
|
|
||||||
|
$ao->providers()->syncWithoutDetaching([
|
||||||
|
$this->to->provider->id => [
|
||||||
|
'ref' => $updated->id,
|
||||||
|
'synctoken' => $updated->synctoken,
|
||||||
|
'created_at'=>Carbon::create($updated->created_at),
|
||||||
|
'updated_at'=>Carbon::create($updated->updated_at),
|
||||||
|
'site_id'=>$ao->site_id, // @todo See if we can have this handled automatically
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Log::info(sprintf('%s:Updated account [%s] (%s:%s), synctoken now [%s]',self::LOGKEY,$ao->sid,$updated->id,$updated->DisplayName,$updated->synctoken));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Log::error(sprintf('%s:Unable to update account refer for [%s:%s]',self::LOGKEY,$updated->id,$updated->DisplayName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ namespace Intuit\Response;
|
|||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Jenssegers\Model\Model;
|
||||||
|
|
||||||
use Intuit\Models\Customer;
|
use Intuit\Models\Customer;
|
||||||
|
|
||||||
@ -13,37 +14,20 @@ use Intuit\Models\Customer;
|
|||||||
*
|
*
|
||||||
* @note: This class is used for events not specifically created.
|
* @note: This class is used for events not specifically created.
|
||||||
*/
|
*/
|
||||||
abstract class Base implements \JsonSerializable, \Countable, \ArrayAccess, \Iterator
|
abstract class Base implements \JsonSerializable
|
||||||
{
|
{
|
||||||
protected const LOGKEY = 'RB-';
|
protected const LOGKEY = 'RB-';
|
||||||
|
|
||||||
protected Collection $_data;
|
protected Model $_model;
|
||||||
protected ?string $_type;
|
|
||||||
protected int $startPosition;
|
|
||||||
protected int $maxResults;
|
|
||||||
|
|
||||||
private ?int $counter = NULL;
|
|
||||||
protected const TYPES = [
|
|
||||||
'customers',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default Constructor Setup
|
* Default Constructor Setup
|
||||||
*
|
*
|
||||||
* @param object $response
|
* @param object $response
|
||||||
* @param string $type
|
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(object $response,string $type)
|
public function __construct(object $response)
|
||||||
{
|
{
|
||||||
if (! in_array($type,self::TYPES))
|
|
||||||
throw new \Exception('Unknown data type: '.$type);
|
|
||||||
|
|
||||||
if (object_get($response,'time'))
|
|
||||||
unset($response->time);
|
|
||||||
|
|
||||||
$this->_data = $this->data($response,$type);
|
|
||||||
|
|
||||||
// This is only for child classes
|
// This is only for child classes
|
||||||
if (get_class($this) == Base::class) {
|
if (get_class($this) == Base::class) {
|
||||||
Log::debug(sprintf('%s:Intuit RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]);
|
Log::debug(sprintf('%s:Intuit RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]);
|
||||||
@ -63,88 +47,6 @@ abstract class Base implements \JsonSerializable, \Countable, \ArrayAccess, \Ite
|
|||||||
return $this->_data ?: new \stdClass;
|
return $this->_data ?: new \stdClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function current(): mixed
|
|
||||||
{
|
|
||||||
return $this->_data[$this->counter];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function next(): void
|
|
||||||
{
|
|
||||||
$this->counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function key(): mixed
|
|
||||||
{
|
|
||||||
return $this->counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function valid(): bool
|
|
||||||
{
|
|
||||||
return isset($this->_data[$this->counter]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function rewind(): void
|
|
||||||
{
|
|
||||||
$this->counter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetExists(mixed $offset): bool
|
|
||||||
{
|
|
||||||
return $this->_data->has($offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetGet(mixed $offset): mixed
|
|
||||||
{
|
|
||||||
return $this->_data->get($offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetSet(mixed $offset, mixed $value): void
|
|
||||||
{
|
|
||||||
throw new \Exception('Method not implemented: '.__METHOD__);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetUnset(mixed $offset): void
|
|
||||||
{
|
|
||||||
$this->_data->forget($offset);
|
|
||||||
|
|
||||||
// Rekey the collection
|
|
||||||
$this->_data = $this->_data->values();
|
|
||||||
|
|
||||||
// Reset the counter if we have deleted a value before it
|
|
||||||
if ($offset < $this->counter)
|
|
||||||
$this->counter--;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function count(): int
|
|
||||||
{
|
|
||||||
return $this->_data->count();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* METHODS */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert our response into a collection of the appropriate model
|
|
||||||
*
|
|
||||||
* @param object $response
|
|
||||||
* @param string $type
|
|
||||||
* @return Collection
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
private function data(object $response,string $type): Collection
|
|
||||||
{
|
|
||||||
switch ($type) {
|
|
||||||
case 'customers':
|
|
||||||
$data = collect(Customer::hydrate($response->QueryResponse->Customer));
|
|
||||||
$this->startPosition = $response->QueryResponse->startPosition;
|
|
||||||
$this->maxResults = $response->QueryResponse->maxResults;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: throw new \Exception('Unknown object type: '.$this->_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for an item in the result
|
* Search for an item in the result
|
||||||
*
|
*
|
||||||
|
25
src/Response/Customer.php
Normal file
25
src/Response/Customer.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a Customer Intuit Response to API calls
|
||||||
|
*/
|
||||||
|
class Customer extends Base
|
||||||
|
{
|
||||||
|
protected const LOGKEY = 'RCI';
|
||||||
|
|
||||||
|
public function __construct(object $response)
|
||||||
|
{
|
||||||
|
parent::__construct($response);
|
||||||
|
|
||||||
|
if (object_get($response,'time'))
|
||||||
|
unset($response->time);
|
||||||
|
|
||||||
|
$this->_model = new \Intuit\Models\Customer((array)$response->Customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get($key) {
|
||||||
|
return $this->_model->__get($key);
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,135 @@
|
|||||||
|
|
||||||
namespace Intuit\Response;
|
namespace Intuit\Response;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Intuit\Models\Customer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a Generic Intuit Response to API calls
|
* This is a Generic Intuit Response to API calls that produces a list of objects
|
||||||
*/
|
*/
|
||||||
class ListList extends Base
|
class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
|
||||||
{
|
{
|
||||||
protected const LOGKEY = 'RLI';
|
protected const LOGKEY = 'RLI';
|
||||||
|
|
||||||
|
protected Collection $_data;
|
||||||
|
protected ?string $_type;
|
||||||
|
protected int $startPosition;
|
||||||
|
protected int $maxResults;
|
||||||
|
|
||||||
|
private ?int $counter = NULL;
|
||||||
|
protected const TYPES = [
|
||||||
|
'customers',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor Setup
|
||||||
|
*
|
||||||
|
* @param object $response
|
||||||
|
* @param string $type
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __construct(object $response,string $type)
|
||||||
|
{
|
||||||
|
parent::__construct($response);
|
||||||
|
|
||||||
|
if (! in_array($type,self::TYPES))
|
||||||
|
throw new \Exception('Unknown data type: '.$type);
|
||||||
|
|
||||||
|
if (object_get($response,'time'))
|
||||||
|
unset($response->time);
|
||||||
|
|
||||||
|
$this->_data = $this->data($response,$type);
|
||||||
|
|
||||||
|
// This is only for child classes
|
||||||
|
if (get_class($this) == Base::class) {
|
||||||
|
Log::debug(sprintf('%s:Intuit RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]);
|
||||||
|
|
||||||
|
if (App::environment() == 'dev')
|
||||||
|
file_put_contents('/tmp/response',print_r($this,TRUE),FILE_APPEND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function current(): mixed
|
||||||
|
{
|
||||||
|
return $this->_data[$this->counter];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function next(): void
|
||||||
|
{
|
||||||
|
$this->counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key(): mixed
|
||||||
|
{
|
||||||
|
return $this->counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function valid(): bool
|
||||||
|
{
|
||||||
|
return isset($this->_data[$this->counter]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rewind(): void
|
||||||
|
{
|
||||||
|
$this->counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists(mixed $offset): bool
|
||||||
|
{
|
||||||
|
return $this->_data->has($offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet(mixed $offset): mixed
|
||||||
|
{
|
||||||
|
return $this->_data->get($offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetSet(mixed $offset, mixed $value): void
|
||||||
|
{
|
||||||
|
throw new \Exception('Method not implemented: '.__METHOD__);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset(mixed $offset): void
|
||||||
|
{
|
||||||
|
$this->_data->forget($offset);
|
||||||
|
|
||||||
|
// Rekey the collection
|
||||||
|
$this->_data = $this->_data->values();
|
||||||
|
|
||||||
|
// Reset the counter if we have deleted a value before it
|
||||||
|
if ($offset < $this->counter)
|
||||||
|
$this->counter--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return $this->_data->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* METHODS */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert our response into a collection of the appropriate model
|
||||||
|
*
|
||||||
|
* @param object $response
|
||||||
|
* @param string $type
|
||||||
|
* @return Collection
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
private function data(object $response,string $type): Collection
|
||||||
|
{
|
||||||
|
switch ($type) {
|
||||||
|
case 'customers':
|
||||||
|
$data = collect(Customer::hydrate($response->QueryResponse->Customer));
|
||||||
|
$this->startPosition = $response->QueryResponse->startPosition;
|
||||||
|
$this->maxResults = $response->QueryResponse->maxResults;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: throw new \Exception('Unknown object type: '.$this->_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user