forked from rap2hpoutre/laravel-stripe-connect
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Hamp
committed
Mar 11, 2022
1 parent
7df44ac
commit a50c7a9
Showing
6 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
]; |
33 changes: 33 additions & 0 deletions
33
migrations/2022_03_11_090000_add_stripe_connect_details_to_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |