Laravel Soulbscription is a fantastic package that provides a straightforward interface to handle subscriptions and feature consumption.
Installation
You can install the package via composer:
composer require lucasdotvin/laravel-soulbscription
The package migrations are loaded automatically, but you can still publish them with this command:
php artisan vendor:publish --tag="soulbscription-migrations"
php artisan migrate
Usage
To start using it, you just have to add the given trait to your User model (or any entity you want to have subscriptions):
<?php
namespace App\Models;
use LucasDotVin\Soulbscription\Models\Concerns\HasSubscriptions;
class User
{
use HasSubscriptions;
}
And that’s it!
Setting Features Up
First things first, you have to define the features you’ll offer. In the example below, we are creating two features: one to handle how many minutes each user can spend with deploys and if they can use subdomains.
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use LucasDotVin\Soulbscription\Models\Feature;
class FeatureSeeder extends Seeder
{
public function run()
{
$deployMinutes = Feature::create([
'consumable' => true,
'name' => 'deploy-minutes',
'periodicity_type' => PeriodicityType::Day,
'periodicity' => 1,
]);
$customDomain = Feature::create([
'consumable' => false,
'name' => 'custom-domain',
]);
}
}
Creating Plans
Now you need to define the plans available for subscription in your app:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use LucasDotVin\Soulbscription\Models\Plan;
class PlanSeeder extends Seeder
{
public function run()
{
$silver = Plan::create([
'name' => 'silver',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
]);
$gold = Plan::create([
'name' => 'gold',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
]);
}
}
Everything here is quite simple, but it is worth emphasizing: by receiving the periodicity options above, the two plans are defined as monthly.
Associating Plans with Features
As each feature can belong to multiple plans (and they can have multiple features), you have to associate them:
use LucasDotVin\Soulbscription\Models\Feature;
// ...
$deployMinutes = Feature::whereName('deploy-minutes')->first();
$subdomains = Feature::whereName('subdomains')->first();
$silver->features()->attach($deployMinutes, ['charges' => 15]);
$gold->features()->attach($deployMinutes, ['charges' => 25]);
$gold->features()->attach($subdomains);
It is necessary to pass a value to charges when associating a consumable feature with a plan.
Subscribing
Now that you have a set of plans with their own features, it is time to subscribe users to them. Registering subscriptions is quite simple:
<?php
namespace App\Listeners;
use App\Events\PaymentApproved;
class SubscribeUser
{
public function handle(PaymentApproved $event)
{
$subscriber = $event->user;
$plan = $event->plan;
$subscriber->subscribeTo($plan);
}
}
There are a lot of methods and features available in this package, If you want to dig more then please visit Github.