Skip to content

Instantly share code, notes, and snippets.

@muzafarali
Created December 14, 2021 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzafarali/acb7bd999da1a4262117d9f491a4de0d to your computer and use it in GitHub Desktop.
Save muzafarali/acb7bd999da1a4262117d9f491a4de0d to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Traits\StripeConnect;
use App\Models\StripeAccount;
use Illuminate\Support\Facades\DB;
// Stripe Connect Controller
// This controller will do the following things
// Start onboard process
// Return from the onboard process and retrieve the information
// Destroy and disconnect the stripe connect accounts
class StripeConnectController extends Controller
{
public function startOnBoardProcess() {
try {
$user = auth()->user();
$account = StripeConnect::getOrCreateAccount($user, ["email" => $user->email]);
$getAccountLink = StripeConnect::createAccountLink($account->account_id, [
"return_url" => url()->current()."/$account->account_id/return",
"refresh_url" => url()->previous()
]);
return redirect($getAccountLink->url);
} catch (\Throwable $th) {
throw $th;
}
}
public function returnFromOnBoardProcess(Request $request, $account_id) {
$stripeConnectAccount = StripeConnect::getAccount($account_id); // you can find getAccount in side gist StripeConnectTraits.php
$account = StripeAccount::where("account_id", $stripeConnectAccount->id)->first();
$account->charges_enabled = (int) $stripeConnectAccount->charges_enabled;
$account->save();
if ( $account->charges_enabled ) {
$message = ["success" => "Stripe connect account is successfuly added!"];
} else if ( $stripeConnectAccount->details_submitted === false ) {
$message = ["error" => "Please try again, You haven't complete the stripe connect process!"];
} else {
$message = ["info" => "Stripe connect account is under review!"];
}
return redirect()->route("settings.index")->with($message);
}
public function destroy( $account_id ) {
try {
DB::beginTransaction();
$user = auth()->user();
$user->stripeAccount->where('account_id', $account_id)->delete();
$user->plans()->where('is_offline', 0)->delete();
DB::commit();
return redirect()->back()->with(['success' => 'Successfully disconnected your stripe account!']);
} catch (\Throwable $th) {
DB::rollBack();
throw $th;
return redirect()->back()->with(['error' => 'Stripe account failed to disconnect!']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment