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

115 lines
3.6 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)
*
* @license https://opensource.org/licenses/AAL
*/
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-04-20 16:08:33 +02:00
use App\Models\CompanyGateway;
use App\PaymentDrivers\Stripe\Connect\Account;
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-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) {
$config = decrypt($company_gateway->config);
if(property_exists($config, 'account_id'))
return render('gateways.stripe.connect.existing');
2021-04-22 11:55:19 +02:00
}
2021-05-12 05:15:51 +02:00
else
2021-05-10 02:22:07 +02:00
$company_gateway = CompanyGatewayFactory::create($request->getCompany()->id, $request->getContact()->id);
2021-04-22 11:55:19 +02:00
2021-05-12 05:00:46 +02:00
/* Set Credit Card To Enabled */
$gateway_types = $company_gateway->driver(new Client)->gatewayTypes();
$fees_and_limits = new \stdClass;
$fees_and_limits->{$gateway_types[0]} = new FeesAndLimits;
2021-05-12 05:15:51 +02:00
$company_gateway->gateway_key = 'd14dd26a47cecc30fdd65700bfb67b34';
2021-05-12 05:00:46 +02:00
$company_gateway->fees_and_limits = $fees_and_limits;
2021-04-22 11:55:19 +02:00
$company_gateway->save();
2021-04-20 16:08:33 +02:00
2021-05-12 05:00:46 +02:00
/* Link account if existing account exists */
if($account_id = $this->checkAccountAlreadyLinkToEmail($company_gateway, $request->getContact()->email)) {
$config = json_decode(decrypt($company_gateway->config));
$config->account_id = $account_id;
$company_gateway->config = encrypt(json_encode($config));
$company_gateway->save();
return render('gateways.stripe.connect.existing');
}
2021-05-12 05:15:51 +02:00
$data = [
'type' => 'standard',
'email' => $request->getContact()->email,
'country' => $request->getCompany()->country()->iso_3166_2,
];
$account = Account::create($data);
$link = Account::link($account->id, $token);
$company_gateway->config = encrypt(json_encode(['account_id' => $account->id]));
$company_gateway->save();
2021-05-12 05:00:46 +02:00
2021-04-20 16:08:33 +02:00
return redirect($link['url']);
}
2021-04-21 00:38:50 +02:00
public function completed()
{
2021-04-21 16:36:08 +02:00
return render('gateways.stripe.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-04-20 16:08:33 +02:00
}