laravel-spark-addons
v0.0.2
Published
Grow your revenue by offering multiple add-on subscriptions in your Laravel Spark app.
Downloads
3
Readme
Spark Addons
Grow your revenue by offering multiple add-on subscriptions in your Laravel Spark app.
- Define "add-on" subscriptions
- Supported metered (pay as you go) and licensed (monthly fee) usage types
- Support multiple plans for each add-on
- Allow users to subscribe and cancel their subscription to an add-on plan
- Implemented in a similar style to existing Spark functionality
Installation
- Include the package
composer require benmag/laravel-spark-addons
- Publish and run the migrations
php artisan vendor:publish --tag=spark-addons-migrations && php artisan migrate
- Update change your Spark class in
config/app.php
[
'aliases' => [
'Spark' => \Benmag\SparkAddons\Spark::class,
]
]
Switch the
use Laravel\Spark\Spark;
class inSparkServiceProvider
touse Benmag\SparkAddons\Spark;
Add the
BillableAddonSubscriptions
trait to your Team model
namespace App;
use Laravel\Spark\Team as SparkTeam;
use Benmag\SparkAddons\Traits\BillableAddonSubscriptions;
class Team extends SparkTeam
{
use BillableAddonSubscriptions;
//...
}
- Ensure the Vue components for
spark-addons
get built by adding
require("./../../../vendor/benmag/laravel-spark-addons/resources/assets/js/bootstrap");
To your resources/assets/js/components/bootstrap.js
file.
- Add your
spark-addons
config to your global Spark object in your layout file (resources/views/vendor/spark/layouts/app.blade.php
)
<!-- Global Spark Object -->
<script>
window.Spark = <?php echo json_encode(array_merge(
Spark::scriptVariables(), [
'spark-addons' => config('spark-addons')
]
)); ?>
</script>
- Show current add-on subscriptions in the subscriptions page by adding:
<addon-subscriptions
:user="user"
:team="team"
:billable-type="billableType">
</addon-subscriptions>
To the views/vendor/spark/settings/subscription.blade.php
view
- Configure your add-on subscriptions! (See the
Usage
section below)
Usage
You can now define available add-on plans in a similar style to how you define your Spark plans inside your SparkServiceProvider
.
// SocketCluster
Spark::addon(new \Benmag\SparkAddons\Addon([
'id' => 'socketcluster',
'name' => 'SocketCluster',
'description' => "A scalable framework for real-time apps and services.",
]));
// SocketCluster - Plan: Hobby
Spark::addonPlan('socketcluster', 'Hobby', 'socketcluster:hobby')
->price(19)
->trialDays(15)
->features([
"Powered by 512mb RAM",
"No complicated setup",
"Email support"
])
->attributes([
'description' => "Single 512mb SocketCluster instance, perfect for development",
]);
// Servers
Spark::addon(new \Benmag\SparkAddons\Addon([
'id' => 'servers',
'name' => 'Servers',
'description' => "High performance cloud servers provisioned for you.",
]));
// Servers: Standard 1X
Spark::addonPlan('servers', 'Standard 1X', 'servers:standard-1x')
->usageType('metered')
->price(0.10)
->features([
'512MB RAM',
'1 Core Processor',
'20GB SSD Disk',
'1TB Transfer'
])
->attributes([
'description' => "512MB RAM. Perfect server to power hobby and small apps.",
]);
//
Configuration
Publish config
php artisan vendor:publish --tag=spark-addons-config
Publish assets
php artisan vendor:publish --tag=spark-addons-assets
Publish views
php artisan vendor:publish --tag=spark-addons-views
Metered Billing
Metered billing or usage based billing allows you to charge your customer an amount based on what they have actually consumed by taking advantage of Stripe's metered billing.
For example, at Codemason, our users can use Codemason Servers to run their apps. These servers are billed by the hour at the corresponding rate and can be cancelled at any time. At the end of the month, the customer is billed for the amount of hours they have accumulated.
By default metered add-on plans are measuring usage by the hour. However, it's easy to swap this implementation out for your
own, just as you would for any other Spark interaction with the Spark::swap
technique.
The behind the scenes of how this works is fairly simple: at the end of the billing cylce, Stripe fires a invoice.created
webhook approximately an hour before the invoice is finalised and a charge is attempted. When your app receives that
webhook, it calculates the number of hours the add-on has been active for and updates Stripe with the usage.
To make use of this functionality, you will make some additional configurations. Don't worry, it's still very easy and very straight forward.
Override the default Stripe Webhook (required)
The main thing we will need to do is make sure that the SparkAddonsServiceProvider
is being manually
registered after the SparkServiceProvider
. This is to ensure that we're using our new controller that listens for
the invoice.created
webhook to perform usage calculations at the end of the month.
Disable auto-discovery so we can override the existing spark/webhook
that Laravel Spark provides
Do this by adding the following to your composer.json
file:
"extra": {
"laravel": {
"dont-discover": [
"laravel/dusk",
"benmag/laravel-spark-addons"
]
}
},
Manually register the SparkAddonsServiceProvider
after the SparkServiceProvider
in the providers
array in config/app.php
'providers' => [
Laravel\Spark\Providers\SparkServiceProvider::class,
App\Providers\SparkServiceProvider::class,
Benmag\SparkAddons\SparkAddonsServiceProvider::class,
// ...
]
Interactions
These are the interactions, you are able to override them with your own custom implementations where required using Spark::swap
|Interaction |Description | |---------------------------|----------------------------------------------------------------------------------------------------------------------------| |CalculateMeteredUsage |Calculate the hour based usage for the add-on subscription. | |RecordUsageUsingStripe |Records the usage on Stripe | |SubscribeAddon |Adds a new add-on subscription to a owner | |SubscribeTeamAddon |Adds a new add-on subscription to a owner | |CancelAddonSubscription |Cancels an add-on subscription. If the add-on subscription is license based, it will reduce the subscription quantity | |CancelTeamAddonSubscription|Cancels an add-on subscription. If the add-on subscription is license based, it will reduce the subscription quantity | |ResumeAddonSubscription |Resume a cancelled add-on subscription (only available if the add-on subscription is license based and in the grace period) | |ResumeTeamAddonSubscription|Resume a cancelled add-on subscription (only available if the add-on subscription is license based and in the grace period) |
Example
We use this approach at Codemason to manage the subscriptions for our add-ons. Codemason is an end-to-end cloud platform and with Codemason Add-on’s, users are able to launch production-grade apps with a click.
About Codemason
We ❤️ Laravel Spark. We used Laravel Spark for Codemason so we could focus on building functionality our users loved, instead of writing boilerplate code. Codemason is the Laravel Spark of servers/hosting
Working on a Laravel Spark project? People who use Spark know how important their time is. Save yourself hours of fighting with servers by using Codemason.
Don't forget to use the coupon SPARK-3-FREE
to get 3 months free access!