mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 04:17:34 -05:00 
			
		
		
		
	Refactor Forwarding
This commit is contained in:
		
							parent
							
								
									9d7145c61b
								
							
						
					
					
						commit
						7aa172b1db
					
				@ -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,31 +107,122 @@ class StepsController extends BaseController
 | 
			
		||||
    public function forwardUrl(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $rules = [
 | 
			
		||||
            'url' => 'nullable|url',
 | 
			
		||||
        ];
 | 
			
		||||
        $this->autoForwardUrl();
 | 
			
		||||
        
 | 
			
		||||
        $validator = Validator::make($request->all(), $rules);
 | 
			
		||||
        // $rules = [
 | 
			
		||||
        //     'url' => 'nullable|url',
 | 
			
		||||
        // ];
 | 
			
		||||
 | 
			
		||||
        if ($validator->fails()) {
 | 
			
		||||
            return back()
 | 
			
		||||
                ->withErrors($validator)
 | 
			
		||||
                ->withInput();
 | 
			
		||||
        // $validator = Validator::make($request->all(), $rules);
 | 
			
		||||
 | 
			
		||||
        // if ($validator->fails()) {
 | 
			
		||||
        //     return back()
 | 
			
		||||
        //         ->withErrors($validator)
 | 
			
		||||
        //         ->withInput();
 | 
			
		||||
        // }
 | 
			
		||||
 | 
			
		||||
        // $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;
 | 
			
		||||
        // }
 | 
			
		||||
 | 
			
		||||
        // $account_settings->forward_url_for_v5 = rtrim($request->input('url'),'/');
 | 
			
		||||
        // $account_settings->save();
 | 
			
		||||
 | 
			
		||||
        return back();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        $account_settings = \Auth::user()->account->account_email_settings;
 | 
			
		||||
    public function disableForwarding()
 | 
			
		||||
    {
 | 
			
		||||
        $account = \Auth::user()->account;
 | 
			
		||||
 | 
			
		||||
        if(strlen($request->input('url')) == 0) {
 | 
			
		||||
        $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($request->input('url'),'/');
 | 
			
		||||
                $account_settings->forward_url_for_v5 = rtrim($forwarding_url,'/');
 | 
			
		||||
                $account_settings->save();
 | 
			
		||||
 | 
			
		||||
        return back();
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            info(json_decode($response->getBody()->getContents()));
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function endpoint()
 | 
			
		||||
 | 
			
		||||
@ -277,15 +277,13 @@
 | 
			
		||||
					<label for="modules" class="control-label col-lg-4 col-sm-4"></label>
 | 
			
		||||
					<div class="col-lg-8 col-sm-8">
 | 
			
		||||
						<div class="help-block">
 | 
			
		||||
							Once you are ready to forward your customers, enter your client portal URL for V5 here:<br/><br/>
 | 
			
		||||
							<b>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.</b> <br/><br/>To renable your V4 installation simply set the forwarding url to a blank/empty value.
 | 
			
		||||
						</div><br/>
 | 
			
		||||
						</div>
 | 
			
		||||
					</div>
 | 
			
		||||
        		</div>
 | 
			
		||||
				<div class="form-group">                    
 | 
			
		||||
                    <label for="url"  class="control-label col-lg-4 col-sm-4 text-right">{!! trans('texts.url') !!}</label>
 | 
			
		||||
                    <div class="col-lg-8 col-sm-8">
 | 
			
		||||
                    <input type="text" name="url" placeholder="https://subdomain.invoicing.co" class="form form-control" value="{{ $account->account_email_settings->forward_url_for_v5}}">
 | 
			
		||||
                    <input type="text" name="url" placeholder="https://subdomain.invoicing.co" class="form form-control" value="{{ $account->account_email_settings->forward_url_for_v5}}" disabled>
 | 
			
		||||
		        	@if($errors->has('url')) 
 | 
			
		||||
		                <div class="col-sm-5">
 | 
			
		||||
		                    @foreach ($errors->get('url') as $message)
 | 
			
		||||
@ -301,6 +299,17 @@
 | 
			
		||||
                	</div>
 | 
			
		||||
				</div>
 | 
			
		||||
				</form>
 | 
			
		||||
				<div class="col-lg-8 col-sm-8 col-lg-offset-4 col-sm-offset-4">
 | 
			
		||||
					<br>
 | 
			
		||||
					<br>
 | 
			
		||||
					 <span class="help-block">
 | 
			
		||||
					 	<br>
 | 
			
		||||
					 	If you need to rollback to v4, please disable forwarding using this link.
 | 
			
		||||
					 	<a class="button" href="/migration/disable_forward">Disable Forwarding</a>
 | 
			
		||||
					 </span>
 | 
			
		||||
				</div>
 | 
			
		||||
				</div>
 | 
			
		||||
				</div>
 | 
			
		||||
			</div>
 | 
			
		||||
		</div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user