ExpDev07 / laravel-cashier-stripe-connect

💲 Adds Stripe Connect functionality to Laravel's main billing package, Cashier. Simply works as a drop-in on top of Cashier, with no extra configuration.
https://github.com/ExpDev07/laravel-cashier-stripe-connect
MIT License
61 stars 27 forks source link
cashier laravel laravel-cashier package stripe stripe-connect

laravel-cashier-stripe-connect

Total Downloads Latest Stable Version License

Newer Package Available

A more comprehensive package is now available, built upon this package so can be upgraded to from this package, it offers multi-model, multi-tenancy support, UUID support and the following features:

  • Multi Model support - Previously only supported the User model, now any model can have the Connect Billable trait added to it and immediately inherit functionality.
  • Tenancy for Laravel Support (Multi Tenant SaaS Plugin)
  • Manage connected account onboarding
  • Direct Charges
  • Destination Charges
  • Connected account customer management (Direct Customers)
  • Connected account payment method management
  • Connected account subscriptions ( Direct Subscriptions )
  • Connected account product & price management
  • Connect Webhook Support (On behalf of connected accounts)
  • Connected Account Apple Pay Domain Registering

Click here to access the new package

As a result of the new package, this package will no longer be maintained.

Buy Me a Coffee at ko-fi.com

💲 Adds Stripe Connect functionality to Laravel's main billing package, Cashier. Simply works as a drop-in on top of Cashier, with no extra configuration.

Installation

  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.

Note: the package will not work as intended if you do not install Laravel's official Cashier package first.

Use

The library builds on the official Cashier library, so getting up and started is a breeze.

Setup model

Add the Billable traits to your model. You can use them individually or together. You can also create your own Billable trait and put them together there. In addition, the model should also implement the StripeAccount interface.

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use ExpDev07\CashierConnect\Contracts\StripeAccount;
use Laravel\Cashier\Billable as CashierBillable;
use ExpDev07\CashierConnect\Billable as ConnectBillable;

class User extends Authenticatable implements StripeAccount
{
    use CashierBillable;
    use ConnectBillable;

    ///

}

Create controller

Create a controller to manage on-boarding process. The example below registers an Express account for the user.

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)
        );
    }

}

Example

// Get user. This user has added the Billable trait and implements StripeAccount.
$user = User::query()->find(1);

// Transfer 10 USD to the user.
$user->transferToStripeAccount(1000);

// Payout 5 dollars to the user's bank account, which will arrive in 1 week.
$user->payoutStripeAccount(500, Date::now()->addWeek());

License

Please refer to LICENSE.md for this project's license.

Contributors

This list only contains some of the most notable contributors. For the full list, refer to GitHub's contributors graph.

Thanks to

Taylor Otwell for his amazing framework and all the contributors of Cashier.