From c231c9186feddfa83a2158989583360145d74ffd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 21 Aug 2024 15:07:52 +1000 Subject: [PATCH] Refactor for quickbooks --- .../ImportQuickbooksController.php | 100 +++--------------- .../AuthorizedQuickbooksRequest.php | 69 ++++++++++++ app/Services/Import/Quickbooks/SdkWrapper.php | 16 +-- 3 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php diff --git a/app/Http/Controllers/ImportQuickbooksController.php b/app/Http/Controllers/ImportQuickbooksController.php index 09e75dc81f3c..558188c91aaf 100644 --- a/app/Http/Controllers/ImportQuickbooksController.php +++ b/app/Http/Controllers/ImportQuickbooksController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\Quickbooks\AuthorizedQuickbooksRequest; use \Closure; use App\Utils\Ninja; use App\Models\Company; @@ -18,98 +19,31 @@ use App\Services\Import\Quickbooks\QuickbooksService; class ImportQuickbooksController extends BaseController { - // protected QuickbooksService $service; - private $import_entities = [ + private array $import_entities = [ 'client' => 'Customer', 'invoice' => 'Invoice', 'product' => 'Item', 'payment' => 'Payment' ]; - // public function __construct(QuickbooksService $service) { - // 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) + public function onAuthorized(AuthorizedQuickbooksRequest $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); } /** diff --git a/app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php b/app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php new file mode 100644 index 000000000000..b7b2f3f5584e --- /dev/null +++ b/app/Http/Requests/Quickbooks/AuthorizedQuickbooksRequest.php @@ -0,0 +1,69 @@ +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(); + } +} diff --git a/app/Services/Import/Quickbooks/SdkWrapper.php b/app/Services/Import/Quickbooks/SdkWrapper.php index e14c86689f67..6e2d111b60b3 100644 --- a/app/Services/Import/Quickbooks/SdkWrapper.php +++ b/app/Services/Import/Quickbooks/SdkWrapper.php @@ -28,7 +28,7 @@ final class SdkWrapper implements QuickbooksInterface return ($this->sdk->getOAuth2LoginHelper())->getState(); } - public function getAccessToken() : array + public function getAccessToken() { return $this->getTokens(); } @@ -44,15 +44,17 @@ final class SdkWrapper implements QuickbooksInterface return $this->getTokens(); } - private function getTokens() : array { + private function getTokens() + { $token =($this->sdk->getOAuth2LoginHelper())->getAccessToken(); - $access_token = $token->getAccessToken(); - $refresh_token = $token->getRefreshToken(); - $access_token_expires = $token->getAccessTokenExpiresAt(); - $refresh_token_expires = $token->getRefreshTokenExpiresAt(); + return $token + // $access_token = $token->getAccessToken(); + // $refresh_token = $token->getRefreshToken(); + // $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