From 7aa172b1dbcd819f2df84e64929829bc39d0f23b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 8 Nov 2021 09:27:36 +1100 Subject: [PATCH 1/4] Refactor Forwarding --- .../Controllers/Migration/StepsController.php | 128 +++++++++++++++--- resources/views/accounts/management.blade.php | 17 ++- routes/web.php | 1 + 3 files changed, 125 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index a6320a82cd24..b342a96dd0bc 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -11,6 +11,8 @@ use App\Http\Requests\MigrationTypeRequest; use App\Jobs\HostedMigration; use App\Libraries\Utils; use App\Models\Account; +use App\Models\AccountGatewayToken; +use App\Models\Client; use App\Services\Migration\AuthService; use App\Services\Migration\CompanyService; use App\Services\Migration\CompleteService; @@ -19,6 +21,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use Validator; +use GuzzleHttp\RequestOptions; class StepsController extends BaseController { @@ -104,33 +107,124 @@ class StepsController extends BaseController public function forwardUrl(Request $request) { - $rules = [ - 'url' => 'nullable|url', - ]; + $this->autoForwardUrl(); + + // $rules = [ + // 'url' => 'nullable|url', + // ]; - $validator = Validator::make($request->all(), $rules); + // $validator = Validator::make($request->all(), $rules); - if ($validator->fails()) { - return back() - ->withErrors($validator) - ->withInput(); - } + // if ($validator->fails()) { + // return back() + // ->withErrors($validator) + // ->withInput(); + // } - $account_settings = \Auth::user()->account->account_email_settings; + // $account_settings = \Auth::user()->account->account_email_settings; - if(strlen($request->input('url')) == 0) { - $account_settings->is_disabled = false; - } - else { - $account_settings->is_disabled = true; - } + // if(strlen($request->input('url')) == 0) { + // $account_settings->is_disabled = false; + // } + // else { + // $account_settings->is_disabled = true; + // } - $account_settings->forward_url_for_v5 = rtrim($request->input('url'),'/'); + // $account_settings->forward_url_for_v5 = rtrim($request->input('url'),'/'); + // $account_settings->save(); + + return back(); + } + + public function disableForwarding() + { + $account = \Auth::user()->account; + + $account_settings = $account->account_email_settings; + $account_settings->is_disabled = false; $account_settings->save(); return back(); } + private function autoForwardUrl() + { + + $url = 'https://invoicing.co/api/v1/confirm_forwarding'; + + $headers = [ + 'X-API-HOSTED-SECRET' => config('ninja.ninja_hosted_secret'), + 'X-Requested-With' => 'XMLHttpRequest', + 'Content-Type' => 'application/json', + ]; + + $account = \Auth::user()->account; + $gateway_reference = ''; + + $ninja_client = Client::where('public_id', $account->id)->first(); + + if($ninja_client){ + $agt = AccountGatewayToken::where('client_id', $ninja_client->id)->first(); + + if($agt) + $gateway_reference = $agt->token; + } + + $body = [ + 'account_key' => $account->account_key, + 'email' => \Auth::user()->email, + 'plan' => $account->company->plan, + 'plan_term' =>$account->company->plan_term, + 'plan_started' =>$account->company->plan_started, + 'plan_paid' =>$account->company->plan_paid, + 'plan_expires' =>$account->company->plan_expires, + 'trial_started' =>$account->company->trial_started, + 'trial_plan' =>$account->company->trial_plan, + 'plan_price' =>$account->company->plan_price, + 'num_users' =>$account->company->num_users, + 'gateway_reference' => $gateway_reference, + ]; + + $client = new \GuzzleHttp\Client([ + 'headers' => $headers, + ]); + + $response = $client->post($url,[ + RequestOptions::JSON => $body, + RequestOptions::ALLOW_REDIRECTS => false + ]); + + if($response->getStatusCode() == 401){ + info($response->getBody()); + + } elseif ($response->getStatusCode() == 200) { + + $message_body = json_decode($response->getBody(), true); + + $forwarding_url = $message_body['forward_url']; + + $account_settings = $account->account_email_settings; + + if(strlen($forwarding_url) == 0) { + $account_settings->is_disabled = false; + } + else { + $account_settings->is_disabled = true; + } + + $account_settings->forward_url_for_v5 = rtrim($forwarding_url,'/'); + $account_settings->save(); + + + } else { + info(json_decode($response->getBody()->getContents())); + + } + + + + } + public function endpoint() { diff --git a/resources/views/accounts/management.blade.php b/resources/views/accounts/management.blade.php index 1c920624b7f6..ee798f631b44 100644 --- a/resources/views/accounts/management.blade.php +++ b/resources/views/accounts/management.blade.php @@ -277,15 +277,13 @@
- Once you are ready to forward your customers, enter your client portal URL for V5 here:

- Please note once enabled. Your V4 account will become disabled. This means that your recurring invoices and any reminders will no longer fire from V4.

To renable your V4 installation simply set the forwarding url to a blank/empty value. -

+
- + @if($errors->has('url'))
@foreach ($errors->get('url') as $message) @@ -301,6 +299,17 @@
+
+
+
+ +
+ If you need to rollback to v4, please disable forwarding using this link. + Disable Forwarding +
+
+
+ diff --git a/routes/web.php b/routes/web.php index 107c8e9d5bb4..77fa114091b4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -160,6 +160,7 @@ Route::group(['middleware' => ['lookup:user', 'auth:user']], function () { Route::post('migration/companies', 'Migration\StepsController@handleCompanies'); Route::get('migration/completed', 'Migration\StepsController@completed'); Route::post('migration/forward', 'Migration\StepsController@forwardUrl'); + Route::get('migration/disable_forward', 'Migration\StepsController@disableForwarding'); Route::get('migration/import', 'Migration\StepsController@import'); From bdecb380fc8bd5b5c1770f061af391abc365c0f9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 12 Nov 2021 07:29:19 +1100 Subject: [PATCH 2/4] Forwarding URL automation for v4 --- .../Controllers/Migration/StepsController.php | 41 ++++++++++--------- resources/views/accounts/management.blade.php | 2 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index b342a96dd0bc..55792d6f1651 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -107,31 +107,32 @@ class StepsController extends BaseController public function forwardUrl(Request $request) { - $this->autoForwardUrl(); + if(Utils::isNinjaProd()) + return $this->autoForwardUrl(); - // $rules = [ - // 'url' => 'nullable|url', - // ]; + $rules = [ + 'url' => 'nullable|url', + ]; - // $validator = Validator::make($request->all(), $rules); + $validator = Validator::make($request->all(), $rules); - // if ($validator->fails()) { - // return back() - // ->withErrors($validator) - // ->withInput(); - // } + if ($validator->fails()) { + return back() + ->withErrors($validator) + ->withInput(); + } - // $account_settings = \Auth::user()->account->account_email_settings; + $account_settings = \Auth::user()->account->account_email_settings; - // if(strlen($request->input('url')) == 0) { - // $account_settings->is_disabled = false; - // } - // else { - // $account_settings->is_disabled = true; - // } + if(strlen($request->input('url')) == 0) { + $account_settings->is_disabled = false; + } + else { + $account_settings->is_disabled = true; + } - // $account_settings->forward_url_for_v5 = rtrim($request->input('url'),'/'); - // $account_settings->save(); + $account_settings->forward_url_for_v5 = rtrim($request->input('url'),'/'); + $account_settings->save(); return back(); } @@ -221,7 +222,7 @@ class StepsController extends BaseController } - + return back(); } diff --git a/resources/views/accounts/management.blade.php b/resources/views/accounts/management.blade.php index ee798f631b44..f41b55195ef6 100644 --- a/resources/views/accounts/management.blade.php +++ b/resources/views/accounts/management.blade.php @@ -283,7 +283,7 @@
- + @if($errors->has('url'))
@foreach ($errors->get('url') as $message) From d73e56cbb6479c6d56ea1b4152bb62510fe32700 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 12 Nov 2021 16:04:47 +1100 Subject: [PATCH 3/4] disable forwarding --- app/Http/Controllers/Migration/StepsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index 55792d6f1651..77ab762d631c 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -142,6 +142,7 @@ class StepsController extends BaseController $account = \Auth::user()->account; $account_settings = $account->account_email_settings; + $account_settings->forward_url_for_v5 = ''; $account_settings->is_disabled = false; $account_settings->save(); From 53c8f8d6e025f03a0ef95fcc2dd3d539a75bca51 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 21 Nov 2021 13:49:01 +1100 Subject: [PATCH 4/4] Fixes for hosted auto forwarding --- .../Controllers/Migration/StepsController.php | 26 ++++++++++--------- app/Jobs/HostedMigration.php | 25 +++--------------- app/Services/Migration/CompleteService.php | 24 ++--------------- app/Traits/GenerateMigrationResources.php | 2 +- 4 files changed, 21 insertions(+), 56 deletions(-) diff --git a/app/Http/Controllers/Migration/StepsController.php b/app/Http/Controllers/Migration/StepsController.php index 77ab762d631c..ef4a89bc3121 100644 --- a/app/Http/Controllers/Migration/StepsController.php +++ b/app/Http/Controllers/Migration/StepsController.php @@ -89,14 +89,6 @@ class StepsController extends BaseController url('/migration/companies?hosted=true') ); - //old - // return redirect( - // url('/migration/auth') - // ); - - // return redirect( - // url('/migration/endpoint') - // ); } return redirect( @@ -153,6 +145,7 @@ class StepsController extends BaseController { $url = 'https://invoicing.co/api/v1/confirm_forwarding'; + // $url = 'http://devhosted.test:8000/api/v1/confirm_forwarding'; $headers = [ 'X-API-HOSTED-SECRET' => config('ninja.ninja_hosted_secret'), @@ -197,6 +190,7 @@ class StepsController extends BaseController ]); if($response->getStatusCode() == 401){ + info("autoForwardUrl"); info($response->getBody()); } elseif ($response->getStatusCode() == 200) { @@ -217,10 +211,21 @@ class StepsController extends BaseController $account_settings->forward_url_for_v5 = rtrim($forwarding_url,'/'); $account_settings->save(); + $billing_transferred = $message_body['billing_transferred']; + + if($billing_transferred == 'true'){ + + $company = $account->company; + $company->plan = null; + $company->plan_expires = null; + $company->save(); + } + } else { + info("failed to autoforward"); info(json_decode($response->getBody()->getContents())); - + // dd('else'); } return back(); @@ -335,7 +340,6 @@ class StepsController extends BaseController try { $migrationData = $this->generateMigrationData($request->all()); - $completeService->data($migrationData) ->endpoint(session('MIGRATION_ENDPOINT')) @@ -436,8 +440,6 @@ class StepsController extends BaseController Storage::makeDirectory('migrations'); $file = Storage::path("migrations/{$fileName}.zip"); - //$file = storage_path("migrations/{$fileName}.zip"); - ksort($localMigrationData); $zip = new \ZipArchive(); diff --git a/app/Jobs/HostedMigration.php b/app/Jobs/HostedMigration.php index b96e059b4069..f1d87c38cf26 100644 --- a/app/Jobs/HostedMigration.php +++ b/app/Jobs/HostedMigration.php @@ -54,15 +54,15 @@ class HostedMigration extends Job $completeService->data($migrationData) ->endpoint('https://v5-app1.invoicing.co') - // ->endpoint('http://ninja.test:8000') +// ->endpoint('http://devhosted.test:8000') ->start(); } private function getToken() { - $url = 'https://invoicing.co/api/v1/get_migration_account'; - // $url = 'http://ninja.test:8000/api/v1/get_migration_account'; + //$url = 'https://invoicing.co/api/v1/get_migration_account'; + $url = 'http://devhosted.test:8000/api/v1/get_migration_account'; $headers = [ 'X-API-HOSTED-SECRET' => $this->v4_secret, @@ -98,27 +98,10 @@ class HostedMigration extends Job $this->migration_token = $message_body['token']; } else { - info(json_decode($response->getBody()->getContents())); + // info(json_decode($response->getBody()->getContents())); } - - // $body = \Unirest\Request\Body::json($body); - - // $response = Request::post($url, $headers, $body); - - // if (in_array($response->code, [200])) { - - // $data = $response->body; - // info(print_r($data,1)); - // $this->migration_token = $data->token; - - // } else { - // info("getting token failed"); - // info($response->raw_body); - - // } - return $this; } diff --git a/app/Services/Migration/CompleteService.php b/app/Services/Migration/CompleteService.php index c7ce22b5bdbd..620cdeb1c470 100644 --- a/app/Services/Migration/CompleteService.php +++ b/app/Services/Migration/CompleteService.php @@ -75,12 +75,11 @@ class CompleteService 'headers' => $this->getHeaders(), ]); - $payload_data = [ 'multipart'=> array_merge($files, $payload), ]; - info(print_r($payload_data,1)); + // info(print_r($payload_data,1)); $response = $client->request("POST", $this->getUrl(),$payload_data); if($response->getStatusCode() == 200){ @@ -88,7 +87,7 @@ class CompleteService $this->isSuccessful = true; return json_decode($response->getBody(),true); }else { - info($response->raw_body); + // info($response->raw_body); $this->isSuccessful = false; $this->errors = [ @@ -98,25 +97,6 @@ class CompleteService return $this; - - - - // $body = \Unirest\Request\Body::multipart(['companies' => json_encode($data)], $files); - - // $response = Request::post($this->getUrl(), $this->getHeaders(), $body); - - // if (in_array($response->code, [200])) { - // $this->isSuccessful = true; - // } else { - // info($response->raw_body); - - // $this->isSuccessful = false; - // $this->errors = [ - // 'Oops, something went wrong. Migration can\'t be processed at the moment. Please checks the logs.', - // ]; - // } - - return $this; } public function isSuccessful() diff --git a/app/Traits/GenerateMigrationResources.php b/app/Traits/GenerateMigrationResources.php index c8eae3ec81a0..66531b2716f0 100644 --- a/app/Traits/GenerateMigrationResources.php +++ b/app/Traits/GenerateMigrationResources.php @@ -1543,7 +1543,7 @@ trait GenerateMigrationResources } catch(\Exception $e){ - info($config); + // info($config); }