Skip to content

Commit

Permalink
Bring it all together
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Hamp committed Mar 11, 2022
1 parent 7df44ac commit a50c7a9
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 15 deletions.
21 changes: 21 additions & 0 deletions config/stripe_connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [
'stripe' => [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],

'routes' => [
'account' => [
'refresh' => 'stripe-connect.refresh',
'return' => 'stripe-connect.return',
'complete' => 'home',
]
],

'payable' => [
'account_id_column' => 'stripe_account_id',
'account_status_column' => 'stripe_account_active',
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStripeConnectDetailsToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('stripe_account_id')->nullable();
$table->boolean('stripe_account_active')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('stripe_account_id', 'stripe_account_active');
});
}
}
27 changes: 27 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;

Route::middleware('auth')
->prefix('stripe-connect')
->name('stripe-connect.')
->group(function () {
Route::get('return', function () {
$account = Auth::user()->retrieveStripeAccount();

Auth::user()
->setStripeAccountStatus($account->details_submitted)
->save();

return Route::has(Config::get('stripe_connect.routes.complete'))
? Response::redirectToRoute(Config::get('stripe_connect.routes.complete'))
: Response::redirectTo('/');
})->name('return');

Route::get('refresh', function () {
return Response::redirectTo(Auth::user()->getStripeAccountLink());
})->name('refresh');
});
11 changes: 11 additions & 0 deletions src/Interfaces/StripeConnect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SimonHamp\LaravelStripeConnect\Interfaces;

use Stripe\StripeClient;

/** @mixin StripeClient */
interface StripeConnect
{

}
26 changes: 11 additions & 15 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
<?php

namespace SimonHamp\LaravelStripeConnect;

namespace Rap2hpoutre\LaravelStripeConnect;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

use SimonHamp\LaravelStripeConnect\Interfaces\StripeConnect as StripeConnectInterface;
use Stripe\StripeClient;

class ServiceProvider extends BaseServiceProvider
{

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->loadMigrationsFrom(__DIR__.'/../migrations');
$this->mergeConfigFrom(__DIR__.'/../config/stripe_connect.php', 'stripe_connect');
$this->publishes([__DIR__.'/../config', __DIR__.'/../migrations']);
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
App::singleton(StripeConnectInterface::class, function () {
return new StripeClient(Config::get('stripe_connect.stripe.secret'));
});
}
}
102 changes: 102 additions & 0 deletions src/Traits/Payable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace SimonHamp\LaravelStripeConnect\Traits;

use Stripe\Account;
use Stripe\StripeClient;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Config;
use SimonHamp\LaravelStripeConnect\Interfaces\StripeConnect;

trait Payable
{
protected static StripeClient $stripe;

protected Account $stripe_connect_account;

protected static function bootPayable()
{
static::$stripe = App::make(StripeConnect::class);
}

/**
* Create a new Stripe Connect account for this model
*/
public function createStripeAccount(array $details): self
{
$this->stripe_connect_account = static::$stripe->accounts->create($details);

$this->setStripeAccountId($this->stripe_connect_account->id)->save();

return $this;
}

/**
* Get the latest details about this account from Stripe
*/
public function retrieveStripeAccount(): Account
{
return $this->stripe_connect_account = static::$stripe->accounts->retrieve($this->getStripeAccountId());
}

public function getStripeAccountId()
{
return $this->{$this->getStripeAccountIdColumn()};
}

public function isStripeAccountActive()
{
return $this->{$this->getStripeAccountStatusColumn()};
}

/**
* Get the redirect URL needed to take this account through Stripe's onboarding flow
*/
public function getStripeAccountLink($type = 'account_onboarding'): string
{
$link = static::$stripe->accountLinks->create(
[
'account' => $this->getStripeAccountId(),
'refresh_url' => URL::route(Config::get('stripe_connect.routes.account.refresh')),
'return_url' => URL::route(Config::get('stripe_connect.routes.account.return')),
'type' => 'account_onboarding',
]
);

return $link->url;
}

public function pay($amount, $currency)
{
return static::$stripe->transfers->create([
'amount' => $amount,
'currency' => $currency,
'destination' => $this->getStripeAccountId(),
]);
}

protected function getStripeAccountIdColumn()
{
return Config::get('stripe_connect.payable.account_id_column');
}

protected function setStripeAccountId($id)
{
$this->{$this->getStripeAccountIdColumn()} = $id;

return $this;
}

protected function getStripeAccountStatusColumn()
{
return Config::get('stripe_connect.payable.account_status_column');
}

protected function setStripeAccountStatus($status)
{
$this->{$this->getStripeAccountStatusColumn()} = $status;

return $this;
}
}

0 comments on commit a50c7a9

Please sign in to comment.