How to integrate Stripe Connect in Laravel / PHP ?

Laravel
July 13, 20231 minuteuserVishal Ribdiya
How to integrate Stripe Connect in Laravel / PHP ?

Stripe Connect is the fastest way to integrate payment into your marketplace.

In this tutorial we are going to integrate the stripe connect with Laravel.

To integrate the stripe connect you must need the stripe secret and the stripe key. You can get that credentials from your Stripe account.

So let's start integrating...

Installation

composer require srmklive/paypal:~2.0

Onboard the seller using Stripe connect

Define the route for onboarding the seller.

  Route::get('onboard-seller', [StripeController::class, 'onboard']);

Now on StripeController.php write the code for the onboarding link.

use Stripe\Stripe;
use Stripe\Account;
use App\Models\User;
use Stripe\AccountLink;

public function onBoard()
{
    $user = Auth::user();

    /** @var User $user */
    if (empty($user->stripe_on_boarding_completed_at)) {
        Stripe::setApiKey(stripeKey());

        if (empty($user->stripe_connect_id)) {
            /** @var Account $account */
            $account = Account::create([
                'type'         => 'express',
                'email'        => $user->email,
                'country'      => 'US',
                'capabilities' => [
                    'card_payments' => ['requested' => true],
                    'transfers'     => ['requested' => true],
                ],
                'settings'     => [
                    'payouts' => [
                        'schedule' => [
                            'interval' => 'manual',
                        ],
                    ],
                ],
            ]);

            $user->stripe_connect_id = $account->id;
            $user->save();
        }

        $user->fresh();

        $onBoardLink = AccountLink::create([
            'account'     => $user->stripe_connect_id,
            'refresh_url' => route('organization.dashboard'),
            'return_url'  => route('stripe.onboard-result', Crypt::encrypt($user->stripe_connect_id)),
            'type'        => 'account_onboarding',
        ]);

        return redirect($onBoardLink->url);
    }


    $loginLink = $this->stripeClient->accounts->createLoginLink($user->stripe_connect_id, []);

    return redirect($loginLink->url);
}

Please note that you have to generate routes for return and cancel URLs.

Once a merchant is successfully onboarded stripe will again redirect him to the return_url

Below you can find the code in which we managed the success callback.

public function onBoardResult($encodedToken)
{
    /** @var User $user */
    $user = User::whereStripeConnectId(Crypt::decrypt($encodedToken))->firstOrFail();

    $user->stripe_on_boarding_completed_at = Carbon::now();
    $user->save();

    return redirect(route('dashboard'));
}