1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/StripeConnectController.php

161 lines
5.5 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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-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;
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)
{
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']);
2024-05-04 01:22:04 +02:00
// $company_gateway = CompanyGateway::query()
// ->where('gateway_key', 'd14dd26a47cecc30fdd65700bfb67b34')
// ->where('company_id', $request->getCompany()->id)
// ->first();
2021-04-22 11:55:19 +02:00
2024-05-04 01:22:04 +02:00
// if ($company_gateway) {
// $config = $company_gateway->getConfig();
2021-05-10 02:22:07 +02:00
2024-05-04 01:22:04 +02:00
// if (property_exists($config, 'account_id') && strlen($config->account_id) > 5) {
// 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');
2023-11-24 00:23:40 +01:00
$redirect_uri = config('ninja.app_url').'/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');
}
2024-02-29 11:34:29 +01:00
$response = false;
2021-05-18 14:03:19 +02:00
try {
2023-08-11 06:18:58 +02:00
/** @class \stdClass $response
* @property string $scope
* @property string $stripe_user_id
* @property string $stripe_publishable_key
* @property string $refresh_token
* @property string $livemode
* @property string $access_token
* @property string $token_type
* @property string $stripe_user
* @property string $stripe_account
* @property string $error
*/
/** @var \stdClass $response */
2021-05-18 14:03:19 +02:00
$response = \Stripe\OAuth::token([
'grant_type' => 'authorization_code',
'code' => $request->input('code'),
2021-05-18 14:03:19 +02:00
]);
2024-01-31 04:44:16 +01:00
nlog($response);
2024-02-13 05:25:18 +01:00
} catch (\Exception $e) {
2024-02-29 11:34:29 +01:00
}
if(!$response) {
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
2023-08-11 06:18:58 +02:00
$company = Company::query()->where('company_key', $request->getTokenContent()['company_key'])->first();
2021-05-18 04:13:00 +02:00
$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);
2024-01-14 05:05:00 +01:00
$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';
}
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
2023-10-26 04:57:44 +02:00
try {
$stripe = $company_gateway->driver()->init();
$a = \Stripe\Account::retrieve($response->stripe_user_id, $stripe->stripe_connect_auth);
2024-01-14 05:05:00 +01:00
if($a->business_name ?? false) {
$company_gateway->label = substr("Stripe - {$a->business_name}", 0, 250);
$company_gateway->save();
}
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
nlog("could not harvest stripe company name");
}
if(isset($request->getTokenContent()['is_react']) && $request->getTokenContent()['is_react']) {
2023-11-24 00:23:40 +01:00
$redirect_uri = config('ninja.react_url').'/#/settings/online_payments';
} else {
2024-02-29 11:34:29 +01:00
$redirect_uri = config('ninja.app_url');
}
2021-05-12 05:00:46 +02:00
2024-03-12 01:30:05 +01:00
\Illuminate\Support\Facades\Cache::pull($request->token);
2023-08-11 06:18:58 +02:00
//response here
2024-02-29 11:34:29 +01:00
return view('auth.connect.completed', ['url' => $redirect_uri]);
2024-05-04 01:22:04 +02:00
2021-05-12 05:00:46 +02:00
}
2021-05-18 04:13:00 +02:00
2021-04-20 16:08:33 +02:00
}