lacodix/laravel-plans

A Laravel package to manage plans, features and subscriptions and track billing in your Laravel SAAS.

Fund package maintenance!
lacodix

Installs: 47

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:laravel-package

v1.0.1 2024-09-27 18:43 UTC

This package is auto-updated.

Last update: 2024-10-27 18:47:25 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Documentation

You can find the entire documentation for this package on our documentation site. Including several usecases with detailed explanation.

What it does

  • Manage all Plans and Addons of your SaaS where users can subscribe to.
  • Subscribe to one or more plans with different billing intervals.
  • Manage optional features, if you need it. You can also stick with plans and just check for a subscribed plan.
  • Offer countable and uncountable features. Use uncountable features to just enable/disable a functionality. Use countable features, for things like tokens, credits, and others. Attach different values of the feature to different plans, including "unlimited". Auto reset the values after given intervals.
  • Consume Features as long as some amount is available. Split up usage on different plans, depending on the order of the plans.
  • Translate your plans and features. Identify both by slug in your app, but offer it to your users localized, powered by spatie/laravel-translatable
  • Order your plans, features (for visualisation to your users) and subscriptions (to keep control over usage order)
  • Allow meta data in plans and subscriptions. The usage of meta data is up to you. In some usecase we take meta data to save currency or additional price information. This information can be used by a subsequent billing system.

What it doesn't

  • Billing. We don't create invoices or keep track on bills. Use the invoice service of your choice, doesn't matter which. Stripe, PayPal, your own software and any other. You just get events from this package, when subscriptions are created or renewed, and this can be used to trigger invoices. You are also free to wait for payment before you create the subscription or the other way round.
  • Pricing. Yes there is a price column in the plan model, that can be used for visualisation. You also can use meta data for additional price information like we do it in some examples. But you don't need to use it. You can keep your prices in your billing system or save it separate from the plans. But indeed, if you use the price-column you can also get calculated prices for partial subscription intervals.

Installation

composer require lacodix/laravel-plans

Quickstart

To get familiar with all settings and possibilities, please see the more detailed examples. This is only a very quick overview how the package can work.

Subscribers

Add our HasSubscription Trait to any model.

use Lacodix\LaravelPlans\Models\Traits\HasSubscriptions;

class User extends Authenticatable {
    use HasSubscriptions;
    
    ...
}

Plans and Features

Create a Plan with Features (the latter is optional, if you don't need feature functionality).

use Lacodix\LaravelPlans\Enums\Interval;
use Lacodix\LaravelPlans\Models\Feature;
use Lacodix\LaravelPlans\Models\Plan;

$myPlan = Plan::create([
    'slug' => 'my-plan',
    'name' => 'My Plan', // can also be locale-array - see Feature below
    'price' => 50.0,
    'active' => true,
    'billing_interval' => Interval::MONTH,
    'billing_period' => 1,
    'meta' => [
        'price_per_token' => 0.05,
    ],
]);

$myFeature = Feature::create([
    'slug' => 'tokens',
    'name' => [
        'de' => 'Zusätzliche Tokens',
        'en' => 'Additional Tokens',
    ],
]);

$myPlan->features()->attach($myFeature, [
    'value' => 1000,
    'resettable_period' => 1,
    'resettable_interval' => Interval::MONTH,
]);

Subscribe and renew

// Subscribe to multiple plans
$user->subscribe($myPlan1, 'main');
$user->subscribe($myPlan2, 'addon');

// Change Subscription
$user->subscribe($myPlan3, 'main'); // will replace myPlan1 subscription

// Renew
$user->subscriptions()->first()->renew();

// Cancel
$user->subscriptions()->first()->cancel();

Testing

composer test

Contributing

Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.

composer rector:test
composer insights
composer csfixer:test
composer phpstan:test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

This package is inspired by Laravel Subscriptions created by Laravel Cameroon and was initially started as a fork of it. After several decisions to go different ways for subscription calculation, it was rewritten from scratch, but still contains several simple methods and other code parts of the original. So if this package doesn't fit your needs, try a look into Laravel Cameroons subscription package.

License

The MIT License (MIT). Please see License File for more information.