getTokenContent())) { abort(400, 'Invalid token'); } MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']); $company = Company::where('company_key', $request->getTokenContent()['company_key'])->first(); $company_gateway = CompanyGateway::query() ->where('gateway_key', 'd14dd26a47cecc30fdd65700bfb67b34') ->where('company_id', $request->getCompany()->id) ->first(); if ($company_gateway) { $config = $company_gateway->getConfig(); if (property_exists($config, 'account_id') && strlen($config->account_id) > 5) { return view('auth.connect.existing'); } } $stripe_client_id = config('ninja.ninja_stripe_client_id'); $redirect_uri = 'https://invoicing.co/stripe/completed'; $endpoint = "https://connect.stripe.com/oauth/authorize?response_type=code&client_id={$stripe_client_id}&redirect_uri={$redirect_uri}&scope=read_write&state={$token}"; return redirect($endpoint); } public function completed(InitializeStripeConnectRequest $request) { \Stripe\Stripe::setApiKey(config('ninja.ninja_stripe_key')); if ($request->has('error') && $request->error == 'access_denied') { return view('auth.connect.access_denied'); } try { $response = \Stripe\OAuth::token([ 'grant_type' => 'authorization_code', 'code' => $request->input('code'), ]); } catch (\Exception $e) { return view('auth.connect.access_denied'); } MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']); $company = Company::where('company_key', $request->getTokenContent()['company_key'])->first(); $company_gateway = CompanyGateway::query() ->where('gateway_key', 'd14dd26a47cecc30fdd65700bfb67b34') ->where('company_id', $company->id) ->first(); if (! $company_gateway) { $company_gateway = CompanyGatewayFactory::create($company->id, $company->owner()->id); $fees_and_limits = new \stdClass; $fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits; $company_gateway->gateway_key = 'd14dd26a47cecc30fdd65700bfb67b34'; $company_gateway->fees_and_limits = $fees_and_limits; $company_gateway->setConfig([]); $company_gateway->token_billing = 'always'; // $company_gateway->save(); } $payload = [ 'account_id' => $response->stripe_user_id, 'token_type' => 'bearer', 'stripe_publishable_key' => $response->stripe_publishable_key, 'scope' => $response->scope, 'livemode' => $response->livemode, 'stripe_user_id' => $response->stripe_user_id, 'refresh_token' => $response->refresh_token, 'access_token' => $response->access_token, 'appleDomainVerification' => '', ]; $company_gateway->setConfig($payload); $company_gateway->save(); // StripeWebhook::dispatch($company->company_key, $company_gateway->id); //response here return view('auth.connect.completed'); } private function checkAccountAlreadyLinkToEmail($company_gateway, $email) { $client = Client::first() ? Client::first() : new Client; //Pull the list of Stripe Accounts and see if we match $accounts = $company_gateway->driver($client)->getAllConnectedAccounts()->data; foreach ($accounts as $account) { if ($account['email'] == $email) { return $account['id']; } } return false; } /********************************* * Stripe OAuth */ // public function initialize(InitializeStripeConnectRequest $request, string $token) // { // $stripe_key = config('ninja.ninja_stripe_key'); // $endpoint = "https://connect.stripe.com/oauth/authorize?response_type=code&client_id={$stripe_key}&scope=read_write"; // return redirect($endpoint); // } }