Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dillinghamio/7f3b776e0ff1007cc877d63d6aaee10d to your computer and use it in GitHub Desktop.

Select an option

Save dillinghamio/7f3b776e0ff1007cc877d63d6aaee10d to your computer and use it in GitHub Desktop.
Per Team User Subscription in Laravel Spark

Per Team User Subscription in Laravel Spark

If you want the ability to charge a team owner based on how many members their team has, like $10/user/month, then you utilize the Laravel Cashier functionality of incrementing the quantity.

You listen for when a new team member is added and you increase the quantity of the subscription by the amount of users and also listen for when a team member is removed to downsize charges. - Not Braintree Compatible


Within EventServiceProvider.php
'Laravel\Spark\Events\Teams\TeamMemberAdded' => [
    'App\Listeners\UpdatePerUserCharge'
],

'Laravel\Spark\Events\Teams\TeamMemberRemoved' => [
    'App\Listeners\UpdatePerUserCharge'
],
And add a new listener
<?php

namespace App\Listeners;

class UpdatePerUserCharge
{
    public function handle($event)
    {
        $userCount = $event->team->users()->count();

        $event->team->subscription()->updateQuantity($userCount);
    }
}

Made by: @dillinghamio Find More Awesome Spark Stuff Here

@impeto

impeto commented Apr 26, 2016

Copy link
Copy Markdown

Here is an idea: kick up a notification after you update the quantity ;)

@dillinghamio

dillinghamio commented Apr 26, 2016

Copy link
Copy Markdown
Author

@impeto I had that in there but took it out to keep it simple. :)

@Max-Hutschenreiter

Copy link
Copy Markdown

You should also handle TeamMemberRemoved. Basically the same Code in the handle function. Else you charge eventually to much :)

@dillinghamio

Copy link
Copy Markdown
Author

@Max-Hutschenreiter good catch, I subscribed the same listener to both events

@dpde

dpde commented Apr 27, 2016

Copy link
Copy Markdown

What happens when the team owner adds 5 users and removes them 20 days later. What will be billed in that case?

@dillinghamio

dillinghamio commented Apr 27, 2016

Copy link
Copy Markdown
Author

@dpde its pro-rated on stripes side. Stripe will see that they used the subscription x 5 for 20 days and x1 for the remainder.

@rokde

rokde commented May 3, 2016

Copy link
Copy Markdown

Hint: That does not work with Braintree service, because Braintree does not support a quantity.

@dillinghamio

Copy link
Copy Markdown
Author

@rokde updated the description. thanks

@kmukku

kmukku commented Jun 11, 2016

Copy link
Copy Markdown

If you start with a fresh install there's no users or teams and this listener will choke registration process. I tried to add null checks with no luck...

@jamesgraham

Copy link
Copy Markdown

@kmukku you can just wrap the code in your listener like:

        if ($event->team->subscription() != null) {
            $event->team->subscription()->updateQuantity($userCount);
        }

@jfoxworth

Copy link
Copy Markdown

Accounts can have multiple teams, so how do you denote for which team the user is billed for? If I have a sass company where I want to charge a company $10 per user per month and then let that company create teams for various permissions issues, how do I decide which of those teams I am billing for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment