mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Refactor for quickbooks
This commit is contained in:
parent
f8e06d7ca3
commit
c231c9186f
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\Quickbooks\AuthorizedQuickbooksRequest;
|
||||||
use \Closure;
|
use \Closure;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
@ -18,98 +19,31 @@ use App\Services\Import\Quickbooks\QuickbooksService;
|
|||||||
|
|
||||||
class ImportQuickbooksController extends BaseController
|
class ImportQuickbooksController extends BaseController
|
||||||
{
|
{
|
||||||
// protected QuickbooksService $service;
|
|
||||||
|
|
||||||
private $import_entities = [
|
private array $import_entities = [
|
||||||
'client' => 'Customer',
|
'client' => 'Customer',
|
||||||
'invoice' => 'Invoice',
|
'invoice' => 'Invoice',
|
||||||
'product' => 'Item',
|
'product' => 'Item',
|
||||||
'payment' => 'Payment'
|
'payment' => 'Payment'
|
||||||
];
|
];
|
||||||
|
|
||||||
// public function __construct(QuickbooksService $service) {
|
public function onAuthorized(AuthorizedQuickbooksRequest $request)
|
||||||
// parent::__construct();
|
|
||||||
|
|
||||||
// $this->service = $service;
|
|
||||||
// $this->middleware(
|
|
||||||
// function (Request $request, Closure $next) {
|
|
||||||
|
|
||||||
// // Check for the required query parameters
|
|
||||||
// if (!$request->has(['code', 'state', 'realmId'])) {
|
|
||||||
// return abort(400,'Unauthorized');
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $rules = [
|
|
||||||
// 'state' => [
|
|
||||||
// 'required',
|
|
||||||
// 'valid' => function ($attribute, $value, $fail) {
|
|
||||||
// if (!Cache::has($value)) {
|
|
||||||
// $fail('The state is invalid.');
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// ]
|
|
||||||
// ];
|
|
||||||
// // Custom error messages
|
|
||||||
// $messages = [
|
|
||||||
// 'state.required' => 'The state is required.',
|
|
||||||
// 'state.valid' => 'state token not valid'
|
|
||||||
// ];
|
|
||||||
// // Perform the validation
|
|
||||||
// $validator = Validator::make($request->all(), $rules, $messages);
|
|
||||||
// if ($validator->fails()) {
|
|
||||||
// // If validation fails, redirect back with errors and input
|
|
||||||
// return redirect('/')
|
|
||||||
// ->withErrors($validator)
|
|
||||||
// ->withInput();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $token = Cache::pull($request->state);
|
|
||||||
// $request->merge(['company' => Cache::get($token) ]);
|
|
||||||
|
|
||||||
// return $next($request);
|
|
||||||
// }
|
|
||||||
// )->only('onAuthorized');
|
|
||||||
// $this->middleware(
|
|
||||||
// function ( Request $request, Closure $next) {
|
|
||||||
// $rules = [
|
|
||||||
// 'token' => [
|
|
||||||
// 'required',
|
|
||||||
// 'valid' => function ($attribute, $value, $fail) {
|
|
||||||
// if (!Cache::has($value) || (!Company::where('company_key', (Cache::get($value))['company_key'])->exists() )) {
|
|
||||||
// $fail('The company is invalid.');
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// ]
|
|
||||||
// ];
|
|
||||||
// // Custom error messages
|
|
||||||
// $messages = [
|
|
||||||
// 'token.required' => 'The token is required.',
|
|
||||||
// 'token.valid' => 'Token note valid!'
|
|
||||||
// ];
|
|
||||||
// // Perform the validation
|
|
||||||
// $validator = Validator::make(['token' => $request->token ], $rules, $messages);
|
|
||||||
// if ($validator->fails()) {
|
|
||||||
// return redirect()
|
|
||||||
// ->back()
|
|
||||||
// ->withErrors($validator)
|
|
||||||
// ->withInput();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //If validation passes, proceed to the next middleware/controller
|
|
||||||
// return $next($request);
|
|
||||||
// }
|
|
||||||
// )->only('authorizeQuickbooks');
|
|
||||||
// }
|
|
||||||
|
|
||||||
public function onAuthorized(Request $request)
|
|
||||||
{
|
{
|
||||||
$realm = $request->query('realmId');
|
|
||||||
$company_key = $request->input('company.company_key');
|
|
||||||
$company_id = $request->input('company.id');
|
|
||||||
$tokens = ($auth_service = $this->service->getOAuth())->accessToken($request->query('code'), $realm);
|
|
||||||
$auth_service->saveTokens($company_key, ['realm' => $realm] + $tokens);
|
|
||||||
|
|
||||||
return response(200);
|
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
|
||||||
|
$company = $request->getCompany();
|
||||||
|
$qb = new QuickbooksService($company);
|
||||||
|
|
||||||
|
$realm = $request->query('realmId');
|
||||||
|
$access_token_object = $qb->getAuth()->accessToken($request->query('code'), $realm);
|
||||||
|
nlog($access_token_object);
|
||||||
|
$company->quickbooks = $access_token_object;
|
||||||
|
$company->save();
|
||||||
|
// $company_key = $request->input('company.company_key');
|
||||||
|
// $company_id = $request->input('company.id');
|
||||||
|
// $auth_service->saveTokens($company_key, ['realm' => $realm] + $tokens);
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Success'], 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
69
app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php
Normal file
69
app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Quickbooks;
|
||||||
|
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class AuthorizedQuickbooksRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return is_array($this->getTokenContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'code' => 'required|string',
|
||||||
|
'state' => 'required|string',
|
||||||
|
'realmId' => 'required|string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve one-time token instance.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getTokenContent()
|
||||||
|
{
|
||||||
|
$token = Cache::get($this->state);
|
||||||
|
|
||||||
|
$data = Cache::get($token);
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContact()
|
||||||
|
{
|
||||||
|
return User::findOrFail($this->getTokenContent()['user_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCompany()
|
||||||
|
{
|
||||||
|
return Company::where('company_key', $this->getTokenContent()['company_key'])->firstOrFail();
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,7 @@ final class SdkWrapper implements QuickbooksInterface
|
|||||||
return ($this->sdk->getOAuth2LoginHelper())->getState();
|
return ($this->sdk->getOAuth2LoginHelper())->getState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAccessToken() : array
|
public function getAccessToken()
|
||||||
{
|
{
|
||||||
return $this->getTokens();
|
return $this->getTokens();
|
||||||
}
|
}
|
||||||
@ -44,15 +44,17 @@ final class SdkWrapper implements QuickbooksInterface
|
|||||||
return $this->getTokens();
|
return $this->getTokens();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTokens() : array {
|
private function getTokens()
|
||||||
|
{
|
||||||
|
|
||||||
$token =($this->sdk->getOAuth2LoginHelper())->getAccessToken();
|
$token =($this->sdk->getOAuth2LoginHelper())->getAccessToken();
|
||||||
$access_token = $token->getAccessToken();
|
return $token
|
||||||
$refresh_token = $token->getRefreshToken();
|
// $access_token = $token->getAccessToken();
|
||||||
$access_token_expires = $token->getAccessTokenExpiresAt();
|
// $refresh_token = $token->getRefreshToken();
|
||||||
$refresh_token_expires = $token->getRefreshTokenExpiresAt();
|
// $access_token_expires = $token->getAccessTokenExpiresAt();
|
||||||
|
// $refresh_token_expires = $token->getRefreshTokenExpiresAt();
|
||||||
|
|
||||||
return compact('access_token', 'refresh_token','access_token_expires', 'refresh_token_expires');
|
// return compact('access_token', 'refresh_token','access_token_expires', 'refresh_token_expires');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function refreshToken(): array
|
public function refreshToken(): array
|
||||||
|
Loading…
x
Reference in New Issue
Block a user