1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/Controllers/StripeConnectController.php

154 lines
5.2 KiB
PHP
Raw Normal View History

2021-04-20 16:08:33 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-04-20 16:08:33 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-04-20 16:08:33 +02:00
*/
namespace App\Http\Controllers;
2021-05-12 05:00:46 +02:00
use App\DataMapper\FeesAndLimits;
2021-04-21 16:36:08 +02:00
use App\Factory\CompanyGatewayFactory;
2021-04-20 16:08:33 +02:00
use App\Http\Requests\StripeConnect\InitializeStripeConnectRequest;
2021-04-22 13:22:55 +02:00
use App\Libraries\MultiDB;
2021-05-12 05:00:46 +02:00
use App\Models\Client;
2021-05-18 04:13:00 +02:00
use App\Models\Company;
2021-04-20 16:08:33 +02:00
use App\Models\CompanyGateway;
2021-05-18 04:13:00 +02:00
use App\Models\GatewayType;
use App\PaymentDrivers\Stripe\Jobs\StripeWebhook;
2021-04-20 16:08:33 +02:00
use Stripe\Exception\ApiErrorException;
class StripeConnectController extends BaseController
{
/**
* Initialize Stripe Connect flow.
*
* @param string $token One-time token
* @throws ApiErrorException
*/
public function initialize(InitializeStripeConnectRequest $request, string $token)
{
2021-04-21 16:36:08 +02:00
// Should we check if company has set country in the ap? Otherwise this will fail.
2021-04-20 16:08:33 +02:00
if (! is_array($request->getTokenContent())) {
2021-04-22 15:40:36 +02:00
abort(400, 'Invalid token');
}
2021-04-22 15:40:36 +02:00
2021-04-22 13:22:55 +02:00
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
2021-05-18 07:53:00 +02:00
$company = Company::where('company_key', $request->getTokenContent()['company_key'])->first();
2021-05-10 02:22:07 +02:00
$company_gateway = CompanyGateway::query()
2021-04-22 11:55:19 +02:00
->where('gateway_key', 'd14dd26a47cecc30fdd65700bfb67b34')
->where('company_id', $request->getCompany()->id)
->first();
2021-05-10 02:22:07 +02:00
if ($company_gateway) {
2021-05-18 07:53:00 +02:00
$config = $company_gateway->getConfig();
2021-05-10 02:22:07 +02:00
2022-11-08 22:09:42 +01:00
if (property_exists($config, 'account_id') && strlen($config->account_id) > 5) {
2021-05-18 04:13:00 +02:00
return view('auth.connect.existing');
}
2021-04-22 11:55:19 +02:00
}
2021-05-18 04:13:00 +02:00
$stripe_client_id = config('ninja.ninja_stripe_client_id');
2021-05-18 11:54:57 +02:00
$redirect_uri = 'https://invoicing.co/stripe/completed';
2021-05-18 04:13:00 +02:00
$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'));
2023-02-16 02:36:09 +01:00
if ($request->has('error') && $request->error == 'access_denied') {
return view('auth.connect.access_denied');
}
2021-05-18 14:03:19 +02:00
try {
$response = \Stripe\OAuth::token([
'grant_type' => 'authorization_code',
'code' => $request->input('code'),
2021-05-18 14:03:19 +02:00
]);
} catch (\Exception $e) {
return view('auth.connect.access_denied');
2021-05-18 14:03:19 +02:00
}
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
2021-05-18 04:13:00 +02:00
$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();
}
2021-04-20 16:08:33 +02:00
2021-05-18 04:13:00 +02:00
$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' => '',
2021-05-18 04:13:00 +02:00
];
2021-05-18 07:53:00 +02:00
$company_gateway->setConfig($payload);
2021-05-12 05:15:51 +02:00
$company_gateway->save();
2021-05-12 05:00:46 +02:00
2022-07-19 00:34:39 +02:00
// StripeWebhook::dispatch($company->company_key, $company_gateway->id);
2021-05-18 04:13:00 +02:00
//response here
return view('auth.connect.completed');
2021-04-21 00:38:50 +02:00
}
2021-05-12 05:00:46 +02:00
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) {
2021-05-12 05:00:46 +02:00
return $account['id'];
}
2021-05-12 05:00:46 +02:00
}
return false;
}
2021-05-18 04:13:00 +02:00
/*********************************
* Stripe OAuth
*/
2021-05-18 04:13:00 +02:00
// 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);
// }
2021-04-20 16:08:33 +02:00
}