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

162 lines
5.1 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
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;
use App\Exceptions\SystemError;
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;
2021-04-20 16:08:33 +02:00
use App\PaymentDrivers\Stripe\Connect\Account;
use Exception;
2021-05-18 04:13:00 +02:00
use Illuminate\Http\Request;
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
2021-04-22 14:56:00 +02:00
if(!is_array($request->getTokenContent()))
2021-04-22 15:40:36 +02:00
abort(400, 'Invalid token');
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
2021-07-14 07:34:29 +02:00
if(property_exists($config, 'account_id') && strlen($config->account_id) > 1)
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'));
2021-05-18 14:03:19 +02:00
try {
$response = \Stripe\OAuth::token([
'grant_type' => 'authorization_code',
'code' => $request->input('code'),
]);
}catch(\Exception $e)
{
2021-05-18 15:12:03 +02:00
2021-05-18 14:03:19 +02:00
nlog($e->getMessage());
throw new SystemError($e->getMessage(), 500);
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',
2021-05-18 07:53:00 +02:00
"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
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)
return $account['id'];
}
return false;
}
2021-05-18 04:13:00 +02:00
/*********************************
* 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);
// }
2021-04-20 16:08:33 +02:00
}