Giter Club home page Giter Club logo

laravel-cashier-stripe-connect's Introduction

laravel-cashier-stripe-connect

Total Downloads Latest Stable Version License

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.

  • ExpDev07 (Marius) - creator and maintainer.
  • Haytam Bakouane (hbakouane) - contributor.

Thanks to

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

laravel-cashier-stripe-connect's People

Contributors

c-fitzmaurice avatar expdev07 avatar fwartner avatar hbakouane avatar mariusspring avatar oleg-kolzhanov avatar tsotnekekelia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.