Create a Laravel Marketplace with Stripe using Cashier and this package

Haytam Bakouane
3 min readOct 23, 2021

--

Designed by me on Canva :-)

Check out my blog for more articles like this: https://hbakouane.dev/blog

Many people want to create a marketplace using Laravel and Cashier, but the problem is Cashier does not support this feature of sending and transacting money between customers and sellers, or a platform and its sellers. Thus, The package that we are going to talk about supports this feature.

What this package does is take Stripe Connect’s features and combine them with Laravel’s official package: Cashier.

This package is called “Laravel Cashier Stripe Connect”, Let’s take a look at it and see how to use it.

1 — Installing the package

  1. Enable Stripe Connect in your dashboard settings.
  2. Install Cashier: composer require laravel/cashier.
  3. Install package: composer require expdev07/laravel-cashier-stripe-connect.
  4. Run migrations: php artisan migrate.
  5. Configure Stripe keys for Cashier: Cashier Docs.

2 — Setting up the model

When you use Laravel Cashier, you should add the CashierBillable trait to your model, but with this package, you will be asked to add the ConnectBillable trait as well, like that:

use ExpDev07\CashierConnect\Billable as ConnectBillable;class User extends Authenticatable implements StripeAccount {
use CashierBillable;
use ConnectBillable;
}

2 - Setting up the controller

After settings up the model, you should set up your controller as well, so that it can take care of the onboarding process:

namespace App\Http\Controllers;use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use URL;
class StripeController extends Controller
{
/**
* Creates an onboarding link and redirects the user there.
*
* @param Request $request
* @return RedirectResponse
*/
public function board(Request $request): RedirectResponse
{
return $this->handleBoardingRedirect($request->user());
}
/**
* Handles returning from completing the onboarding process.
*
* @param Request $request
* @return RedirectResponse
*/
public function returning(Request $request): RedirectResponse
{
return $this->handleBoardingRedirect($request->user());
}
/**
* Handles refreshing of onboarding process.
*
* @param Request $request
* @return RedirectResponse
*/
public function refresh(Request $request): RedirectResponse
{
return $this->handleBoardingRedirect($request->user());
}
/**
* Handles the redirection logic of Stripe onboarding for the given user. Will
* create account and redirect user to onboarding process or redirect to account
* dashboard if they have already completed the process.
*
* @param User $user
* @return RedirectResponse
*/
private function handleBoardingRedirect(User $user): RedirectResponse
{
// Redirect to dashboard if onboarding is already completed.
if ($user->hasStripeAccountId() && $user->hasCompletedOnboarding()) {
return $user->redirectToAccountDashboard();
}
// Delete account if already exists and create new express account with
// weekly payouts.
$user->deleteAndCreateStripeAccount('express', [
'settings' => [
'payouts' => [
'schedule' => [
'interval' => 'weekly',
'weekly_anchor' => 'friday',
]
]
]
]);
// Redirect to Stripe account onboarding, with return and refresh url, otherwise.
return $user->redirectToAccountOnboarding(
URL::to('/api/stripe/return?api_token=' . $user->api_token),
URL::to('/api/stripe/refresh?api_token=' . $user->api_token)
);
}
}

3 — Add money to the vendor’s balance

When having a multi-vendor eCommerce application, you might want to raise the vendor’s balance in their Stripe account after a sale has been made (not to pay them), to do this, “Laravel Cashier Stripe Connect” provides a method that will take care of that.

Let’s say we want to add $10 to the vendor’s stripe balance:

$user->transferToStripeAccount(1000);

Payout 5 dollars to the user’s bank account, which will arrive in 1 week

$user->payoutStripeAccount(500, Date::now()->addWeek());

I hope this article helped you!

Checkout my blog for more articles like this: https://hbakouane.dev/blog

Read More:

Integration of Laravel + VueJs + AdminLTE made easy

Models Pruning in Laravel 8.50.0 explained

5 Javascript libraries that will help you build your stunning Portfolio

A Laravel SaaS Boilerplate to start working on your next project

--

--

Haytam Bakouane

It's Haytam here, I work with Laravel/Vue, I seek to learn new things daily as possibly as I could, Now I'm learning Tailwind, such a great framework!