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

Merge pull request #5682 from turbo124/v5-develop

Fixes for stripe connect
This commit is contained in:
David Bomba 2021-05-12 13:01:07 +10:00 committed by GitHub
commit 4774027c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 14 deletions

View File

@ -12,9 +12,11 @@
namespace App\Http\Controllers;
use App\DataMapper\FeesAndLimits;
use App\Factory\CompanyGatewayFactory;
use App\Http\Requests\StripeConnect\InitializeStripeConnectRequest;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\CompanyGateway;
use App\PaymentDrivers\Stripe\Connect\Account;
use Stripe\Exception\ApiErrorException;
@ -70,8 +72,28 @@ class StripeConnectController extends BaseController
'config' => encrypt(json_encode(['account_id' => $account->id]))
]);
/* 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;
$company_gateway->fees_and_limits = $fees_and_limits;
$company_gateway->save();
/* 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');
}
return redirect($link['url']);
}
@ -79,4 +101,22 @@ class StripeConnectController extends BaseController
{
return render('gateways.stripe.connect.completed');
}
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;
}
}

View File

@ -1263,7 +1263,7 @@ class Import implements ShouldQueue
if(Ninja::isHosted() && $modified['gateway_key'] == 'd14dd26a37cecc30fdd65700bfb55b23'){
$modified['gateway_key'] = 'd14dd26a47cecc30fdd65700bfb67b34';
$modified['fees_and_limits'] = [];
$modified['fees_and_limits'] = '{}';
}
$company_gateway = CompanyGateway::create($modified);

View File

@ -30,6 +30,7 @@ use App\PaymentDrivers\Stripe\Utilities;
use App\Utils\Traits\MakesHash;
use Exception;
use Illuminate\Support\Carbon;
use Stripe\Account;
use Stripe\Customer;
use Stripe\Exception\ApiErrorException;
use Stripe\PaymentIntent;
@ -405,11 +406,26 @@ class StripePaymentDriver extends BaseDriver
*/
public function attach(string $payment_method, $customer): void
{
$this->init();
try {
$stripe_payment_method = $this->getStripePaymentMethod($payment_method);
$stripe_payment_method->attach(['customer' => $customer->id], null, $this->stripe_connect_auth);
$stripe_payment_method->attach(['customer' => $customer->id], $this->stripe_connect_auth);
} catch (ApiErrorException | Exception $e) {
$this->processInternallyFailedPayment($this, $e);
nlog($e->getMessage());
SystemLogger::dispatch([
'server_response' => $e->getMessage(),
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->client);
}
}
@ -422,18 +438,27 @@ class StripePaymentDriver extends BaseDriver
*/
public function detach(ClientGatewayToken $token)
{
$stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
try {
$stripe_payment_method = $this->getStripePaymentMethod($token->token);
$stripe_payment_method->detach($token->token, null, $this->stripe_connect_auth);
//$stripe->paymentMethods->detach($token->token, $this->stripe_connect_auth);
} catch (Exception $e) {
$this->init();
try{
$pm = $this->getStripePaymentMethod($token->token);
$pm->detach([], $this->stripe_connect_auth);
} catch (ApiErrorException | Exception $e) {
nlog($e->getMessage());
SystemLogger::dispatch([
'server_response' => $e->getMessage(), 'data' => request()->all(),
], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client);
'server_response' => $e->getMessage(),
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->client);
}
}
@ -452,9 +477,20 @@ class StripePaymentDriver extends BaseDriver
public function getStripePaymentMethod(string $source)
{
try {
return PaymentMethod::retrieve($source, $this->stripe_connect_auth);
} catch (ApiErrorException | Exception $e) {
return $this->processInternallyFailedPayment($this, $e);
}
}
public function getAllConnectedAccounts()
{
$this->init();
return Account::all();
}
}

View File

@ -29,7 +29,7 @@ class StripeConnectGateway extends Migration
Gateway::create($gateway);
if (Ninja::isNinja()) {
if (Ninja::isHosted()) {
Gateway::whereIn('id', [20])->update(['visible' => 0]);
Gateway::whereIn('id', [56])->update(['visible' => 1]);
}

View File

@ -12,6 +12,7 @@ namespace Database\Seeders;
use App\Models\Gateway;
use App\Models\GatewayType;
use App\Utils\Ninja;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
@ -97,6 +98,11 @@ class PaymentLibrariesSeeder extends Seeder
Gateway::whereIn('id', [1,15,20,39,55,50])->update(['visible' => 1]);
if (Ninja::isHosted()) {
Gateway::whereIn('id', [20])->update(['visible' => 0]);
Gateway::whereIn('id', [56])->update(['visible' => 1]);
}
Gateway::all()->each(function ($gateway) {
$gateway->site_url = $gateway->getHelp();
$gateway->save();