From a50c7a9bddbb8d0ae0be2ce3c67de97ee76ef9f5 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Fri, 11 Mar 2022 04:10:38 +0000 Subject: [PATCH] Bring it all together --- config/stripe_connect.php | 21 ++++ ..._stripe_connect_details_to_users_table.php | 33 ++++++ routes/web.php | 27 +++++ src/Interfaces/StripeConnect.php | 11 ++ src/ServiceProvider.php | 26 ++--- src/Traits/Payable.php | 102 ++++++++++++++++++ 6 files changed, 205 insertions(+), 15 deletions(-) create mode 100644 config/stripe_connect.php create mode 100644 migrations/2022_03_11_090000_add_stripe_connect_details_to_users_table.php create mode 100644 routes/web.php create mode 100644 src/Interfaces/StripeConnect.php create mode 100644 src/Traits/Payable.php diff --git a/config/stripe_connect.php b/config/stripe_connect.php new file mode 100644 index 0000000..024db2b --- /dev/null +++ b/config/stripe_connect.php @@ -0,0 +1,21 @@ + [ + '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', + ] +]; diff --git a/migrations/2022_03_11_090000_add_stripe_connect_details_to_users_table.php b/migrations/2022_03_11_090000_add_stripe_connect_details_to_users_table.php new file mode 100644 index 0000000..b4c5cb5 --- /dev/null +++ b/migrations/2022_03_11_090000_add_stripe_connect_details_to_users_table.php @@ -0,0 +1,33 @@ +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'); + }); + } +} diff --git a/routes/web.php b/routes/web.php new file mode 100644 index 0000000..8925401 --- /dev/null +++ b/routes/web.php @@ -0,0 +1,27 @@ +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'); +}); diff --git a/src/Interfaces/StripeConnect.php b/src/Interfaces/StripeConnect.php new file mode 100644 index 0000000..1ab8364 --- /dev/null +++ b/src/Interfaces/StripeConnect.php @@ -0,0 +1,11 @@ +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')); + }); } } \ No newline at end of file diff --git a/src/Traits/Payable.php b/src/Traits/Payable.php new file mode 100644 index 0000000..5b86b3c --- /dev/null +++ b/src/Traits/Payable.php @@ -0,0 +1,102 @@ +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; + } +}