Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
e8b049a2e8 | |||
6741d95073 | |||
9d81824b52 | |||
0cc4a217bd | |||
c9523f595b | |||
947e1358b5 | |||
403d2168c1 | |||
b87550fae7 | |||
c35a439566 | |||
0c5ffdad2f | |||
600dfec536 | |||
dd82509008 |
@ -1,23 +1,23 @@
|
|||||||
{
|
{
|
||||||
"name": "leenooks/intuit",
|
"name": "leenooks/intuit",
|
||||||
"description": "Intuit API",
|
"description": "Intuit API",
|
||||||
"keywords": ["laravel","leenooks","intuit"],
|
"keywords": ["laravel","leenooks","intuit"],
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Deon George",
|
"name": "Deon George",
|
||||||
"email": "deon@leenooks.net"
|
"email": "deon@dege.au"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"jenssegers/model": "^1.5"
|
"jenssegers/model": "^1.5"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Intuit\\": "src"
|
"Intuit\\": "src"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
@ -25,5 +25,5 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"minimum-stability": "dev"
|
"minimum-stability": "dev"
|
||||||
}
|
}
|
146
src/API.php
146
src/API.php
@ -7,9 +7,9 @@ use Illuminate\Support\Arr;
|
|||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
use Intuit\Exceptions\{ConnectionIssueException,InvalidQueryResultException};
|
use Intuit\Exceptions\{ConnectionIssueException,InvalidQueryResultException,NotFoundException};
|
||||||
use Intuit\Models\{ProviderToken};
|
use Intuit\Models\{ProviderToken,Payment};
|
||||||
use Intuit\Response\{Customer,Invoice,ListList};
|
use Intuit\Response\{Customer,Invoice,ListList,Taxcode};
|
||||||
|
|
||||||
final class API
|
final class API
|
||||||
{
|
{
|
||||||
@ -25,10 +25,14 @@ final class API
|
|||||||
private const CURLOPT_HEADER = FALSE;
|
private const CURLOPT_HEADER = FALSE;
|
||||||
|
|
||||||
private ProviderToken $token;
|
private ProviderToken $token;
|
||||||
|
private string $url;
|
||||||
|
|
||||||
public function __construct(ProviderToken $token,bool $tryprod=FALSE)
|
public function __construct(ProviderToken $token,bool $tryprod=FALSE)
|
||||||
{
|
{
|
||||||
$this->url = (config('app.env') == 'local' && ! $tryprod) ? 'https://sandbox-quickbooks.api.intuit.com' : 'https://quickbooks.api.intuit.com';
|
$this->url = ((config('app.env') === 'local') && (! $tryprod))
|
||||||
|
? 'https://sandbox-quickbooks.api.intuit.com'
|
||||||
|
: 'https://quickbooks.api.intuit.com';
|
||||||
|
|
||||||
$this->token = $token;
|
$this->token = $token;
|
||||||
|
|
||||||
Log::debug(sprintf('%s:Intuit API for id [%s]',static::LOGKEY,$token->realm_id));
|
Log::debug(sprintf('%s:Intuit API for id [%s]',static::LOGKEY,$token->realm_id));
|
||||||
@ -36,9 +40,17 @@ final class API
|
|||||||
|
|
||||||
/* STATIC */
|
/* STATIC */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL to get log into Quickbooks
|
||||||
|
*
|
||||||
|
* @param bool $tryprod
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function url(bool $tryprod=FALSE): string
|
public static function url(bool $tryprod=FALSE): string
|
||||||
{
|
{
|
||||||
return (config('app.env') == 'local' && ! $tryprod) ? 'https://app.sandbox.qbo.intuit.com/app' : 'https://app.qbo.intuit.com/app';
|
return ((config('app.env') === 'local') && (! $tryprod))
|
||||||
|
? 'https://app.sandbox.qbo.intuit.com/app'
|
||||||
|
: 'https://app.qbo.intuit.com/app';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,7 +62,7 @@ final class API
|
|||||||
private function convertHeaders(array $header): array
|
private function convertHeaders(array $header): array
|
||||||
{
|
{
|
||||||
return collect($header)
|
return collect($header)
|
||||||
->map(function($value,$key) { return sprintf('%s:%s',$key,$value); })
|
->map(fn($value,$key)=>sprintf('%s:%s',$key,$value))
|
||||||
->values()
|
->values()
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
@ -63,7 +75,7 @@ final class API
|
|||||||
* @return object|array
|
* @return object|array
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private function execute(string $path,array $parameters=[])
|
private function execute(string $path,array $parameters=[],string $query=NULL): mixed
|
||||||
{
|
{
|
||||||
$url = sprintf('%s/%s/company/%s/%s',$this->url,self::VERSION,$this->token->realm_id,$path);
|
$url = sprintf('%s/%s/company/%s/%s',$this->url,self::VERSION,$this->token->realm_id,$path);
|
||||||
|
|
||||||
@ -163,9 +175,41 @@ final class API
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
switch ($path) {
|
||||||
|
case 'query':
|
||||||
|
if (! $result->QueryResponse) {
|
||||||
|
Log::debug(sprintf('%s:Query response malformed',self::LOGKEY),['parameters'=>$parameters,'x'=>$x]);
|
||||||
|
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! object_get($result->QueryResponse,$query))
|
||||||
|
throw new NotFoundException(sprintf('%s:Query returned no results',self::LOGKEY));
|
||||||
|
|
||||||
|
return $result->QueryResponse;
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a customer by their email address
|
||||||
|
*
|
||||||
|
* @param string $id
|
||||||
|
* @param array $parameters
|
||||||
|
* @return Customer
|
||||||
|
* @throws InvalidQueryResultException
|
||||||
|
*/
|
||||||
|
public function getAccountQuery(string $id,array $parameters=[]): Customer
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('%s:Get a specific account [%s]',static::LOGKEY,$id));
|
||||||
|
|
||||||
|
$table = 'Customer';
|
||||||
|
$parameters['query'] = sprintf("select * from %s where Id='%s'",$table,$id);
|
||||||
|
$result = object_get($this->execute('query',$parameters,$table),$table);
|
||||||
|
|
||||||
|
return new Customer(array_pop($result));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of our classes
|
* Get a list of our classes
|
||||||
*
|
*
|
||||||
@ -182,28 +226,6 @@ final class API
|
|||||||
return new ListList($this->execute('query',$parameters),$key);
|
return new ListList($this->execute('query',$parameters),$key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a customer by their email address
|
|
||||||
*
|
|
||||||
* @param string $id
|
|
||||||
* @param array $parameters
|
|
||||||
* @return Customer
|
|
||||||
* @throws InvalidQueryResultException
|
|
||||||
*/
|
|
||||||
public function getAccountQuery(string $id,array $parameters=[]): Customer
|
|
||||||
{
|
|
||||||
Log::debug(sprintf('%s:Get a specific account [%s]',static::LOGKEY,$id));
|
|
||||||
|
|
||||||
$parameters['query'] = sprintf("select * from Customer where PrimaryEmailAddr='%s'",$id);
|
|
||||||
|
|
||||||
$x = $this->execute('query',$parameters);
|
|
||||||
|
|
||||||
if ((! $x->QueryResponse) || (! $x->QueryResponse->Customer) || (count($x->QueryResponse->Customer) !== 1))
|
|
||||||
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY));
|
|
||||||
|
|
||||||
return new Customer($x->QueryResponse);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a specific customer record
|
* Get a specific customer record
|
||||||
*
|
*
|
||||||
@ -247,14 +269,11 @@ final class API
|
|||||||
{
|
{
|
||||||
Log::debug(sprintf('%s:Get a specific invoice [%s]',static::LOGKEY,$id));
|
Log::debug(sprintf('%s:Get a specific invoice [%s]',static::LOGKEY,$id));
|
||||||
|
|
||||||
$parameters['query'] = sprintf("select * from Invoice where DocNumber='%s'",$id);
|
$table = 'Invoice';
|
||||||
|
$parameters['query'] = sprintf("select * from %s where DocNumber='%s'",$table,$id);
|
||||||
|
$result = object_get($this->execute('query',$parameters,$table),$table);
|
||||||
|
|
||||||
$x = $this->execute('query',$parameters);
|
return new Invoice(array_pop($result));
|
||||||
|
|
||||||
if ((! $x->QueryResponse) || (! $x->QueryResponse->Invoice) || (count($x->QueryResponse->Invoice) !== 1))
|
|
||||||
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY));
|
|
||||||
|
|
||||||
return new Invoice($x->QueryResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -286,7 +305,27 @@ final class API
|
|||||||
$key = 'Item';
|
$key = 'Item';
|
||||||
$parameters['query'] = 'select * from Item';
|
$parameters['query'] = 'select * from Item';
|
||||||
|
|
||||||
return new ListList($this->execute('query',$parameters),$key);
|
return new ListList($this->execute('query',$parameters,$key),$key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific payment record
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param array $parameters
|
||||||
|
* @return Payment
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function getPayment(int $id,array $parameters=[]): Payment
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('%s:Get a specific payment [%d]',static::LOGKEY,$id));
|
||||||
|
|
||||||
|
$x = $this->execute('payment/'.$id,$parameters);
|
||||||
|
|
||||||
|
if (! $x->Payment)
|
||||||
|
throw new InvalidQueryResultException(sprintf('%s:Query response malformed',self::LOGKEY));
|
||||||
|
|
||||||
|
return new Payment((array)$x->Payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,7 +345,26 @@ final class API
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of our invoices
|
* Find an taxcode by its ID
|
||||||
|
*
|
||||||
|
* @param string $id
|
||||||
|
* @param array $parameters
|
||||||
|
* @return Taxcode
|
||||||
|
* @throws InvalidQueryResultException
|
||||||
|
*/
|
||||||
|
public function getTaxCodeQuery(string $id,array $parameters=[]): Taxcode
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('%s:Get a specific invoice [%s]',static::LOGKEY,$id));
|
||||||
|
|
||||||
|
$table = 'TaxCode';
|
||||||
|
$parameters['query'] = sprintf("select * from %s where Id='%s'",$table,$id);
|
||||||
|
$result = object_get($this->execute('query',$parameters,$table),$table);
|
||||||
|
|
||||||
|
return new Taxcode(array_pop($result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of our tax codes
|
||||||
*
|
*
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return ListList
|
* @return ListList
|
||||||
@ -316,9 +374,9 @@ final class API
|
|||||||
{
|
{
|
||||||
Log::debug(sprintf('%s:Get a list of taxes',static::LOGKEY));
|
Log::debug(sprintf('%s:Get a list of taxes',static::LOGKEY));
|
||||||
$key = 'TaxCode';
|
$key = 'TaxCode';
|
||||||
$parameters['query'] = 'select * from Taxcode';
|
$parameters['query'] = 'select * from TaxCode';
|
||||||
|
|
||||||
return new ListList($this->execute('query',$parameters),$key);
|
return new ListList($this->execute('query',$parameters,$key),$key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -370,7 +428,9 @@ final class API
|
|||||||
{
|
{
|
||||||
Log::debug(sprintf('%s:Update invoice',static::LOGKEY),['params'=>$parameters]);
|
Log::debug(sprintf('%s:Update invoice',static::LOGKEY),['params'=>$parameters]);
|
||||||
|
|
||||||
return new Invoice($this->execute('invoice',array_merge($parameters,['method'=>'POST'])));
|
$result = $this->execute('invoice',array_merge($parameters,['method'=>'POST']));
|
||||||
|
|
||||||
|
return new Invoice($result->Invoice);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -384,6 +444,8 @@ final class API
|
|||||||
{
|
{
|
||||||
Log::debug(sprintf('%s:Update customer',static::LOGKEY),['params'=>$parameters]);
|
Log::debug(sprintf('%s:Update customer',static::LOGKEY),['params'=>$parameters]);
|
||||||
|
|
||||||
return new Customer($this->execute('customer',array_merge($parameters,['method'=>'POST'])));
|
$result = $this->execute('customer',array_merge($parameters,['method'=>'POST']));
|
||||||
|
|
||||||
|
return new Customer($result->Customer);
|
||||||
}
|
}
|
||||||
}
|
}
|
56
src/Commands/AccountGet.php
Normal file
56
src/Commands/AccountGet.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Commands;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Intuit\Exceptions\ConnectionIssueException;
|
||||||
|
use Intuit\Exceptions\NotFoundException;
|
||||||
|
use Intuit\Traits\ProviderTokenTrait;
|
||||||
|
|
||||||
|
class AccountGet extends Command
|
||||||
|
{
|
||||||
|
use ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'intuit:account:get'
|
||||||
|
.' {id : Account ID}'
|
||||||
|
.' {user? : User Email}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Get an account from quickbooks';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$to = $this->providerToken($this->argument('user'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
dump($to->API()->getAccountQuery($this->argument('id')));
|
||||||
|
|
||||||
|
} catch (ConnectException|ConnectionIssueException $e) {
|
||||||
|
$this->error($e->getMessage());
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
$this->error('Nothing found');
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
56
src/Commands/InvoiceGet.php
Normal file
56
src/Commands/InvoiceGet.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Commands;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Intuit\Exceptions\ConnectionIssueException;
|
||||||
|
use Intuit\Exceptions\NotFoundException;
|
||||||
|
use Intuit\Traits\ProviderTokenTrait;
|
||||||
|
|
||||||
|
class InvoiceGet extends Command
|
||||||
|
{
|
||||||
|
use ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'intuit:invoice:get'
|
||||||
|
.' {id : Invoice ID}'
|
||||||
|
.' {user? : User Email}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Get an invoice from the quickbooks';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$to = $this->providerToken($this->argument('user'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
dump($to->API()->getInvoiceQuery($this->argument('id')));
|
||||||
|
|
||||||
|
} catch (ConnectException|ConnectionIssueException $e) {
|
||||||
|
$this->error($e->getMessage());
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
$this->error('Nothing found');
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
61
src/Commands/PaymentGet.php
Normal file
61
src/Commands/PaymentGet.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Commands;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Intuit\Exceptions\ConnectionIssueException;
|
||||||
|
use Intuit\Exceptions\NotFoundException;
|
||||||
|
use Intuit\Traits\ProviderTokenTrait;
|
||||||
|
|
||||||
|
use App\Jobs\AccountingPaymentSync as Job;
|
||||||
|
|
||||||
|
class PaymentGet extends Command
|
||||||
|
{
|
||||||
|
use ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'intuit:payment:get'
|
||||||
|
.' {id : Payment ID}'
|
||||||
|
.' {user? : User Email}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Get a payment from the accounting provider';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$to = $this->providerToken($this->argument('user'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
dd($to->API()->getPayment($this->argument('id')));
|
||||||
|
|
||||||
|
} catch (ConnectException|ConnectionIssueException $e) {
|
||||||
|
$this->error($e->getMessage());
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
$this->error('Nothing found');
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($acc)
|
||||||
|
Job::dispatchSync($to,$acc);
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
57
src/Commands/TaxCodeGet.php
Normal file
57
src/Commands/TaxCodeGet.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Commands;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Intuit\Exceptions\ConnectionIssueException;
|
||||||
|
use Intuit\Exceptions\NotFoundException;
|
||||||
|
use Intuit\Traits\ProviderTokenTrait;
|
||||||
|
|
||||||
|
class TaxCodeGet extends Command
|
||||||
|
{
|
||||||
|
use ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'intuit:taxcode:get'
|
||||||
|
.' {id : Taxcode ID}'
|
||||||
|
.' {user? : User Email}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Get the defined tax codes from the accounting provider';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$to = $this->providerToken($this->argument('user'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $to->API()->getTaxCodeQuery($this->argument('id'));
|
||||||
|
dd($result,$result->getTaxRateRef());
|
||||||
|
|
||||||
|
} catch (ConnectException|ConnectionIssueException $e) {
|
||||||
|
$this->error($e->getMessage());
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
$this->error('Nothing found');
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
54
src/Commands/TokenRefresh.php
Normal file
54
src/Commands/TokenRefresh.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Intuit\Traits\ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh the Intuit tokens
|
||||||
|
*/
|
||||||
|
class TokenRefresh extends Command
|
||||||
|
{
|
||||||
|
use ProviderTokenTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'intuit:token:refresh'
|
||||||
|
.' {user? : User Email}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Refresh an access token';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
* @throws \Intuit\Exceptions\NotTokenException
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$to = $this->providerToken($this->argument('user'));
|
||||||
|
|
||||||
|
$old = $to->refresh_token;
|
||||||
|
|
||||||
|
$x = $to->refreshToken();
|
||||||
|
|
||||||
|
dump([
|
||||||
|
'access'=>$to->access_token,
|
||||||
|
'old'=>$old,
|
||||||
|
'refresh'=>$to->refresh_token,
|
||||||
|
'expires'=>(string)$to->refresh_token_expires_at,
|
||||||
|
'x'=>$x,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Intuit\Controllers;
|
namespace Intuit\Controllers;
|
||||||
|
|
||||||
|
use App\Events\ProviderPaymentCreated;
|
||||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Routing\Controller as BaseController;
|
use Illuminate\Routing\Controller as BaseController;
|
||||||
@ -47,6 +48,11 @@ class Webhook extends BaseController
|
|||||||
case 'entities':
|
case 'entities':
|
||||||
foreach ($data as $eventData) {
|
foreach ($data as $eventData) {
|
||||||
switch ($x=$eventData['name']) {
|
switch ($x=$eventData['name']) {
|
||||||
|
case 'Payment':
|
||||||
|
Log::alert(sprintf('We got a payment event [%s:%s]',$object,$x),['data'=>$eventData]);
|
||||||
|
event(new ProviderPaymentCreated('intuit',$eventData));
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Log::alert(sprintf('We dont know how to handle [%s:%s] yet',$object,$x),['data'=>$eventData]);
|
Log::alert(sprintf('We dont know how to handle [%s:%s] yet',$object,$x),['data'=>$eventData]);
|
||||||
}
|
}
|
||||||
|
9
src/Exceptions/NotFoundException.php
Normal file
9
src/Exceptions/NotFoundException.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class NotFoundException extends Exception
|
||||||
|
{
|
||||||
|
}
|
9
src/Exceptions/NotTokenException.php
Normal file
9
src/Exceptions/NotTokenException.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class NotTokenException extends Exception
|
||||||
|
{
|
||||||
|
}
|
@ -3,18 +3,21 @@
|
|||||||
namespace Intuit\Models;
|
namespace Intuit\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Laravel\Socialite\Facades\Socialite;
|
use Laravel\Socialite\Facades\Socialite;
|
||||||
|
|
||||||
class ProviderToken extends Model
|
class ProviderToken extends Model
|
||||||
{
|
{
|
||||||
protected $dates = [
|
private const LOGKEY = 'IPT';
|
||||||
'access_token_expires_at',
|
|
||||||
'refresh_token_expires_at',
|
protected $casts = [
|
||||||
|
'access_token_expires_at' => 'datetime:Y-m-d H:i:s',
|
||||||
|
'refresh_token_expires_at' => 'datetime:Y-m-d H:i:s',
|
||||||
];
|
];
|
||||||
|
|
||||||
/* ATTRIBUTES */
|
/* ATTRIBUTES */
|
||||||
|
|
||||||
public function getAccessTokenAttribute($value): ?string
|
public function getAccessTokenAttribute(string $value): ?string
|
||||||
{
|
{
|
||||||
if (! $this->hasAccessTokenExpired())
|
if (! $this->hasAccessTokenExpired())
|
||||||
return $value;
|
return $value;
|
||||||
@ -29,11 +32,16 @@ class ProviderToken extends Model
|
|||||||
|
|
||||||
public function hasAccessTokenExpired(): bool
|
public function hasAccessTokenExpired(): bool
|
||||||
{
|
{
|
||||||
return $this->access_token_expires_at->isPast();
|
return $this
|
||||||
|
->access_token_expires_at
|
||||||
|
->isPast();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function refreshToken(): bool
|
public function refreshToken(): bool
|
||||||
{
|
{
|
||||||
return Socialite::with($this->provider->name)->refreshtoken($this);
|
Log::debug(sprintf('%s:= Refreshing token for [%s]',self::LOGKEY,$this->provider->name));
|
||||||
|
|
||||||
|
return Socialite::with($this->provider->name)
|
||||||
|
->refreshToken($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ namespace Intuit\Providers;
|
|||||||
|
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Intuit\Commands\{AccountGet,InvoiceGet,PaymentGet,TaxCodeGet,TokenRefresh};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IntuitServiceProvider.
|
* Class IntuitServiceProvider.
|
||||||
@ -20,6 +21,14 @@ class IntuitServiceProvider extends ServiceProvider
|
|||||||
public function boot(Router $router)
|
public function boot(Router $router)
|
||||||
{
|
{
|
||||||
$this->mergeConfigFrom($this->_path.'/config/intuit.php','intuit');
|
$this->mergeConfigFrom($this->_path.'/config/intuit.php','intuit');
|
||||||
|
|
||||||
|
$this->commands([
|
||||||
|
AccountGet::class,
|
||||||
|
InvoiceGet::class,
|
||||||
|
PaymentGet::class,
|
||||||
|
TaxCodeGet::class,
|
||||||
|
TokenRefresh::class,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,8 +38,7 @@ class IntuitServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
if (! $this->_path) {
|
if (! $this->_path)
|
||||||
$this->_path = realpath(__DIR__.'/../../src');
|
$this->_path = realpath(__DIR__.'/../../src');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,7 @@ use Carbon\Carbon;
|
|||||||
use GuzzleHttp\RequestOptions;
|
use GuzzleHttp\RequestOptions;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Laravel\Passport\Exceptions\InvalidAuthTokenException;
|
use Laravel\Passport\Exceptions\InvalidAuthTokenException;
|
||||||
use Laravel\Socialite\Two\InvalidStateException;
|
use Laravel\Socialite\Two\InvalidStateException;
|
||||||
use Laravel\Socialite\Two\ProviderInterface;
|
use Laravel\Socialite\Two\ProviderInterface;
|
||||||
@ -16,6 +17,8 @@ use App\Models\{ProviderOauth,ProviderToken};
|
|||||||
|
|
||||||
class IntuitProvider extends AbstractProvider implements ProviderInterface
|
class IntuitProvider extends AbstractProvider implements ProviderInterface
|
||||||
{
|
{
|
||||||
|
private const LOGKEY = 'SIP';
|
||||||
|
|
||||||
private const hosts = [
|
private const hosts = [
|
||||||
'authorise' => 'https://appcenter.intuit.com/connect/oauth2',
|
'authorise' => 'https://appcenter.intuit.com/connect/oauth2',
|
||||||
'tokenendpoint' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
|
'tokenendpoint' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
|
||||||
@ -104,12 +107,16 @@ class IntuitProvider extends AbstractProvider implements ProviderInterface
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function refreshtoken(ProviderToken $to): bool
|
public function refreshToken($to)
|
||||||
{
|
{
|
||||||
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
|
Log::debug(sprintf('%s:= Refreshing token for [%d]',self::LOGKEY,$to->id));
|
||||||
RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider),
|
|
||||||
RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token),
|
$response = $this
|
||||||
]);
|
->getHttpClient()
|
||||||
|
->post($this->getTokenUrl(),[
|
||||||
|
RequestOptions::HEADERS => $this->getAuthorisationHeader($to->provider),
|
||||||
|
RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($to->refresh_token),
|
||||||
|
]);
|
||||||
|
|
||||||
switch ($response->getStatusCode()) {
|
switch ($response->getStatusCode()) {
|
||||||
case '200':
|
case '200':
|
||||||
@ -125,10 +132,11 @@ class IntuitProvider extends AbstractProvider implements ProviderInterface
|
|||||||
if (($x=Arr::get($body,'refresh_token')) !== $to->refresh_token) {
|
if (($x=Arr::get($body,'refresh_token')) !== $to->refresh_token) {
|
||||||
$to->refresh_token = $x;
|
$to->refresh_token = $x;
|
||||||
$to->refresh_token_expires_at = Carbon::now()->addSeconds(Arr::get($body,'x_refresh_token_expires_in'));
|
$to->refresh_token_expires_at = Carbon::now()->addSeconds(Arr::get($body,'x_refresh_token_expires_in'));
|
||||||
}
|
|
||||||
$to->save();
|
|
||||||
|
|
||||||
return TRUE;
|
Log::debug(sprintf('%s:= Refresh token updated.',self::LOGKEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $to->save();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new InvalidAuthTokenException(sprintf('Invalid response [%d] refreshing token for [%s] (%s)',$response->getStatusCode(),$to->user->email,$response->getBody()));
|
throw new InvalidAuthTokenException(sprintf('Invalid response [%d] refreshing token for [%s] (%s)',$response->getStatusCode(),$to->user->email,$response->getBody()));
|
||||||
|
@ -55,8 +55,8 @@ abstract class Base implements \JsonSerializable
|
|||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function search(string $key, mixed $value): mixed
|
public function search(string $key,mixed $value): mixed
|
||||||
{
|
{
|
||||||
return $this->_data->search(function($item) use ($key,$value) { return $item->{$key} == $value; });
|
return $this->_data->search(fn($item)=>$item->{$key}===$value);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,6 +18,6 @@ class Customer extends Base
|
|||||||
if (object_get($response,'time'))
|
if (object_get($response,'time'))
|
||||||
unset($response->time);
|
unset($response->time);
|
||||||
|
|
||||||
$this->_model = new CustomerModel((array)$response->Customer);
|
$this->_model = new CustomerModel((array)$response);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,6 +18,6 @@ class Invoice extends Base
|
|||||||
if (object_get($response,'time'))
|
if (object_get($response,'time'))
|
||||||
unset($response->time);
|
unset($response->time);
|
||||||
|
|
||||||
$this->_model = new InvoiceModel((array)$response->Invoice);
|
$this->_model = new InvoiceModel((array)$response);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -144,9 +144,7 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
|
|||||||
*/
|
*/
|
||||||
private function data(object $response,string $type): Collection
|
private function data(object $response,string $type): Collection
|
||||||
{
|
{
|
||||||
if (! ($x=object_get($response->QueryResponse,$type)))
|
$x = $response->{$type};
|
||||||
return collect();
|
|
||||||
|
|
||||||
switch (Arr::get(self::TYPES,$type)) {
|
switch (Arr::get(self::TYPES,$type)) {
|
||||||
case Category::class:
|
case Category::class:
|
||||||
$data = collect(Category::hydrate($x));
|
$data = collect(Category::hydrate($x));
|
||||||
@ -177,8 +175,8 @@ class ListList extends Base implements \Countable, \ArrayAccess, \Iterator
|
|||||||
throw new \Exception(sprintf('%s:Unknown object type: %s',self::LOGKEY,$type));
|
throw new \Exception(sprintf('%s:Unknown object type: %s',self::LOGKEY,$type));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->startPosition = $response->QueryResponse->startPosition;
|
$this->startPosition = $response->startPosition;
|
||||||
$this->maxResults = $response->QueryResponse->maxResults;
|
$this->maxResults = $response->maxResults;
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
namespace Intuit\Response;
|
namespace Intuit\Response;
|
||||||
|
|
||||||
use Intuit\Models\Tax as TaxModel;
|
use Illuminate\Support\Collection;
|
||||||
|
use Intuit\Models\Taxcode as TaxcodeModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an Invoice Intuit Response to API calls
|
* This is an Invoice Intuit Response to API calls
|
||||||
@ -18,6 +19,12 @@ class Taxcode extends Base
|
|||||||
if (object_get($response,'time'))
|
if (object_get($response,'time'))
|
||||||
unset($response->time);
|
unset($response->time);
|
||||||
|
|
||||||
$this->_model = new TaxModel((array)$response->Tax);
|
$this->_model = new TaxcodeModel((array)$response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTaxRateRef(): Collection
|
||||||
|
{
|
||||||
|
return collect($this->SalesTaxRateList->TaxRateDetail)
|
||||||
|
->pluck('TaxRateRef.value');
|
||||||
}
|
}
|
||||||
}
|
}
|
33
src/Traits/ProviderTokenTrait.php
Normal file
33
src/Traits/ProviderTokenTrait.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intuit\Traits;
|
||||||
|
|
||||||
|
use App\Models\{ProviderOauth,ProviderToken,User};
|
||||||
|
use Intuit\Exceptions\NotTokenException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a provider token for API calls
|
||||||
|
*/
|
||||||
|
trait ProviderTokenTrait
|
||||||
|
{
|
||||||
|
private const provider = 'intuit';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a provider token to use for an API call
|
||||||
|
*
|
||||||
|
* @return ProviderToken
|
||||||
|
*/
|
||||||
|
protected function providerToken(string $user=NULL): ProviderToken
|
||||||
|
{
|
||||||
|
$uo = User::where('email',$user ?: config('osb.admin'))
|
||||||
|
->sole();
|
||||||
|
|
||||||
|
$so = ProviderOauth::where('name',self::provider)
|
||||||
|
->sole();
|
||||||
|
|
||||||
|
if (! ($to=$so->token($uo)))
|
||||||
|
throw new NotTokenException(sprintf('Unknown Tokens for [%s]',$uo->email));
|
||||||
|
|
||||||
|
return $to;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user