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

77 lines
2.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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
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-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 14:56:00 +02:00
2021-04-22 13:22:55 +02:00
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
2021-04-20 16:08:33 +02:00
$data = [
2021-04-21 00:38:50 +02:00
'type' => 'standard',
2021-04-21 16:36:08 +02:00
'email' => $request->getContact()->email,
'country' => $request->getCompany()->country()->iso_3166_2,
2021-04-20 16:08:33 +02:00
];
2021-04-22 11:55:19 +02:00
$exists = CompanyGateway::query()
->where('gateway_key', 'd14dd26a47cecc30fdd65700bfb67b34')
->where('company_id', $request->getCompany()->id)
->first();
if ($exists) {
return render('gateways.stripe.connect.existing');
2021-04-22 11:55:19 +02:00
}
2021-04-20 16:08:33 +02:00
$account = Account::create($data);
2021-04-21 16:36:08 +02:00
$link = Account::link($account->id, $token);
2021-04-20 16:08:33 +02:00
2021-04-22 15:40:36 +02:00
$company_gateway = CompanyGatewayFactory::create($request->getCompany()->id, $request->getContact()->id);
2021-04-22 11:55:19 +02:00
$company_gateway->fill([
'gateway_key' => 'd14dd26a47cecc30fdd65700bfb67b34',
'fees_and_limits' => [],
'config' => encrypt(json_encode(['account_id' => $account->id]))
]);
$company_gateway->save();
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-04-20 16:08:33 +02:00
}