PHPackages                             vvmalko/subscriptions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. vvmalko/subscriptions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

vvmalko/subscriptions
=====================

Subscription handling package for laravel 5 applications

8173PHP

Since Feb 8Pushed 10y ago1 watchersCompare

[ Source](https://github.com/vvMalko/subscriptions)[ Packagist](https://packagist.org/packages/vvmalko/subscriptions)[ RSS](/packages/vvmalko-subscriptions/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

porting from laravel 4 package. Original here: [ipunkt/subscriptions](https://github.com/ipunkt/subscriptions)

Subscriptions package for Laravel 5.x applications
==================================================

[](#subscriptions-package-for-laravel-5x-applications)

[![Latest Stable Version](https://camo.githubusercontent.com/dd43bb2ed462c678459efe40d5cd98b5064590bd171bc2832c0aeb1e4731f79d/68747470733a2f2f706f7365722e707567782e6f72672f76764d616c6b6f2f737562736372697074696f6e732f762f737461626c652e737667)](https://packagist.org/packages/vvMalko/subscriptions) [![Latest Unstable Version](https://camo.githubusercontent.com/4f60fffcb808cd42e709b5b90387075c77d072f38015e93d1f3da71a9bae0bae/68747470733a2f2f706f7365722e707567782e6f72672f76764d616c6b6f2f737562736372697074696f6e732f762f756e737461626c652e737667)](https://packagist.org/packages/vvMalko/subscriptions) [![License](https://camo.githubusercontent.com/35bdcfbf1cf66e9dc1698cc12db3bd67cad74dd7e3dd2caadf3d0b861ea70a9a/68747470733a2f2f706f7365722e707567782e6f72672f76764d616c6b6f2f737562736372697074696f6e732f6c6963656e73652e737667)](https://packagist.org/packages/vvMalko/subscriptions) [![Total Downloads](https://camo.githubusercontent.com/8dc7e1fe9f274e07a26179e2d6ae620a84313912c7c62537328faf8ffbda197e/68747470733a2f2f706f7365722e707567782e6f72672f76764d616c6b6f2f737562736372697074696f6e732f646f776e6c6f6164732e737667)](https://packagist.org/packages/vvMalko/subscriptions)

Installation
------------

[](#installation)

Add to your composer.json following lines

```
	"require": {
		"vvmalko/subscriptions": "dev-master"
	}
```

Run `php artisan vendor:publish`

Then edit `plans.php` and `defaults.php` in `config/vvmalko` to your needs. All known plans are still in there.

Add `vvMalko\Subscriptions\SubscriptionsServiceProvider::class,` to `providers` in `config/app.php`.

Add `'Subscription' => vvMalko\Subscriptions\SubscriptionsFacade::class,` to `aliases` in `config/app.php`.

Run `php artisan migrate` to migrate the necessary database tables.

Configuration
-------------

[](#configuration)

### Plan configuration

[](#plan-configuration)

```
	//  @see src/config/plans.php
	return [
    	'PLAN-ID' => [
    		'name' => 'TRIAL',
    		'description' => 'Trial subscription.',
    		'subscription_break' => 0,  // optional for preventing a subscription for X days after last subscription ends
    	],
    ];
```

The optional property `'subscription_break` can be used to prevent a subscriber to subscribe to this plan before a configured count of days will be gone. This is for example to have a TRIAL plan which can be subscribed to only once a year.

#### Benefit configuration for a plan

[](#benefit-configuration-for-a-plan)

```
//  @see src/config/plans.php
	return [
    	'PLAN-ID' => [
    		// [..]
			'benefits' => [
				'feature-1' => [],  // feature is present
				'feature-2-countable' => [
					'min' => 10,    // feature is present and has margins/countable range
				],
				'feature-3-countable' => [
					'min' => 10,
					'max' => 50,
				],
				'feature-4-countable' => [
					'max' => 50,    // min is automatically 0 (zero)
				],
			],
    	],
    ];
```

#### Payment options for a plan

[](#payment-options-for-a-plan)

```
	//  @see src/config/plans.php
    return [
        'PLAN-ID' => [
            // [..]
            'payments' => [
                [
                    'price' => 1,           // for 1.00
                    'quantity' => 12,       // in 12-times
                    'days' => 30,           // of 30-days
                    'methods' => ['paypal'], // allowed payment methods
                ],
                [
                    'price' => 2,           // for 2.00
                    'quantity' => 12,       // in 12-times
                    'days' => 30,           // of 30-days
                    'methods' => ['paypal', 'invoice'],
                ],
            ],
        ],
    ];
```

#### Choosing a default plan for all subscribers

[](#choosing-a-default-plan-for-all-subscribers)

For setting a default plan to all subscribers you can use the `src/config/defaults.php` and set the id for the default plan. So every call on plan-based feature checking will resolve this default plan when the subscriber has no plan yet.

#### Customer Model Setup (for example `User`)

[](#customer-model-setup-for-example-user)

```
use vvMalko\Subscriptions\Subscription\Contracts\SubscriptionSubscriber;

class User extends Model implements SubscriptionSubscriber
{
	...
	...
	...

	public function getSubscriberId()
    	{
        	return $this->id;
    	}

    public function getSubscriberModel()
    {
        return $this->table; //model_class set us `User`
    }
}
```

#### Controller Setup (for example `SubscriptionsController`)

[](#controller-setup-for-example-subscriptionscontroller)

```
use vvMalko\Subscriptions\SubscriptionsFacade as Subscription;

class SubscriptionsController extends Controller
{
	...
	...
	...

	private $subscriber;

	public function __construct()
	{
	    $this->subscriber = Auth::user();
	}

	//get user(subscriber) plans
	public function plans()
    	{
            $plan = Subscription::current($this->subscriber);
            return view('plans');
    	}

}
```

For setting a default plan to all subscribers you can use the `src/config/defaults.php` and set the id for the default plan. So every call on plan-based feature checking will resolve this default plan when the subscriber has no plan yet.

Usage
-----

[](#usage)

### Getting all plans

[](#getting-all-plans)

```
	/** @var Plan[] $plans */
	$plans = Subscription::plans();
```

If you use the subscription break in your plan configuration, fetch all plans with the `selectablePlans` method. This checks the last subscription for each breaking plan.

```
	/** @var Plan[] $plans */
	$plans = Subscription::selectablePlans($this->user);
```

### Getting the current plan for a subscriber

[](#getting-the-current-plan-for-a-subscriber)

```
	/** @var Plan|null $plan */
	$plan = Subscription::plan($subscriber);
```

### Does a subscription already exists for a subscriber

[](#does-a-subscription-already-exists-for-a-subscriber)

```
	Subscription::exists($subscriber); // returns true when a subscription exists
```

### Does a subscription expired? Compared period time and current time

[](#does-a-subscription-expired-compared-period-time-and-current-time)

```
	Subscription::expired($subscriber); // returns true|false or null when a subscription not exists
```

### Each plan can have benefits (features)

[](#each-plan-can-have-benefits-features)

```
	$plan->can('feature');               // returns true or false
	$plan->can('countable-feature', 14); // returns true or false
```

Or use the `Subscription` facade instead to check against current subscription plan for a subscriber. This is recommended:

```
	Subscription::can($subscriber, 'feature');               // returns true or false
	Subscription::can($subscriber, 'countable-feature', 14); // returns true or false
```

### Getting all possible payment options for a plan

[](#getting-all-possible-payment-options-for-a-plan)

```
	/** @var PaymentOption[] $paymentOptions */
	$paymentOptions = $plan->paymentOptions();
```

### Creating a new subscription

[](#creating-a-new-subscription)

```
	/** creating a subscription for a subscriber, maybe the current authenticated user */
	$subscription = Subscription::create($plan, $paymentOption, SubscriptionSubscriber $subscriber);
```

For creating a subscription you have to give the `Plan` or the id of a plan and the selected `PaymentOption`or the identifier for the payment option. The `$subscriber` is the entity the subscription belongs to. This can be any morphable eloquent object.

After a subscription was created successfully an event of type `vvMalko\Subscriptions\Subscription\Events\SubscriptionWasCreated` gets fired.

The underlying repository controls for duplicates itself. So for existing subscriptions it will update the current subscription and fires an event of type `vvMalko\Subscriptions\Subscription\Events\SubscriptionWasUpdated`instead.

You can upgrade the subscription to any other plan. The same method `Subscription::create()` handles this upgrade.

The fired events have both the current subscription, the selected plan and the payment option as properties. So you can listen on these events and do your own stuff.

### Getting the current subscription for a subscriber

[](#getting-the-current-subscription-for-a-subscriber)

```
	/** @var Subscription|null $subscription */
	$subscription = Subscription::current($subscriber);
```

### Check subscriber on a Trial

[](#check-subscriber-on-a-trial)

```
	/** be careful because current() can return null when no subscription existing */
	$onTrial = Subscription::current($subscriber)->onTrial();
```

### Check subscription paid

[](#check-subscription-paid)

```
	$subscription = Subscription::current($subscriber);
	$isPaid = $subscription->paid(); // or Subscription::paid($subscriber);
```

### Getting all periods for a subscription

[](#getting-all-periods-for-a-subscription)

```
	/** @var Period[] $periods */
	$periods = $subscription->periods;
```

Userland code
-------------

[](#userland-code)

### Fitting in you controllers

[](#fitting-in-you-controllers)

We use the `laracasts/commander` package for handling business commands and events.

```
	class SubscriptionsController extends \Controller
	{
		/**
         * use commandbus to execute commands
         */
        use Laracasts\Commander\CommanderTrait;

        // display an overview of all subscriptions
        public function index()
        {
            $subscribed = Subscription::exists($this->user);// $this->user represents a SubscriptionSubscriber interface
            if ( ! $subscribed) {
                $plans = Subscription::selectablePlans($this->user);    // unselectable plans filtered out already
                $defaultPlan = Subscription::plan($this->user);

                return View::make('subscriptions.create', compact('plans', 'defaultPlan'));
            }

            $plan = Subscription::plan($this->user);
            $subscription = Subscription::current($this->user);

            $paid = $subscription->paid();

            $subscriptions = Subscription::all($this->user);

            return View::make('subscriptions.index', compact('subscribed', 'plan', 'subscription', 'subscriptions', 'paid'));
        }

        //  create a plan (form)
        public function create($plan)
        {
            $plan = Subscription::findPlan($plan);

            $subscription = Subscription::all($this->user)->last();
            if (null !== $subscription && $subscription->subscription_ends_at->isPast())
                $subscription = null;

            $startDate = (null === $subscription) ? Carbon::now() : $subscription->subscription_ends_at->addSeconds(1);

            return View::make('subscriptions.create_plan', compact('plan', 'startDate'));
        }

        //  store the plan as subscription for user
        public function store()
        {
            try {
                $this->validate(Input::all());
            } catch (FormValidationException $e)
            {
                return Redirect::back()->withInput()->withErrors($e->getErrors());
            }

            $plan = Subscription::findPlan(Input::get('plan'));
            if (null === $plan)
                throw (new ModelNotFoundException('No plan ' . Input::get('plan') . ' found.'))->setModel(Plan::class);

            $this->execute(CreateSubscriptionCommand::class, Input::all());

            Flash::success('subscriptions.subscription_created');

            return Redirect::route('subscriptions.index');
        }
	}
```

And the corresponding command `CreateSubscriptionCommandHandler` is here (The `CreateSubscriptionCommand` is only a DTO for the input values):

```
	class CreateSubscriptionCommandHandler implements Laracasts\Commander\CommandHandler
	{
		use Laracasts\Commander\Events\DispatchableTrait;

		/**
         * authenticated user
         *
         * @var \Illuminate\Auth\Guard
         */
        private $auth;

        /**
         * @param AuthManager $auth
         */
        public function __construct(\Illuminate\Auth\AuthManager $auth)
        {
            $this->auth = $auth;
        }

		/**
         * Handle the command
         *
         * @param CreateSubscriptionCommand $command
         * @return mixed
         */
        public function handle($command)
        {
            /** @var User|SubscriptionSubscriber $user */
            $user = $this->auth->user();

            //  store invoice data

            //  create subscription
            $subscription = Subscription::create($command->plan, $command->payment_option, $user);

            //  fire event for "subscription created" or "subscription updated"
            $this->dispatchEventsFor($subscription);
        }
	}
```

Nearly the same you have to do for extending or upgrading a plan. You can use the same command, handler and controller action. The subscription repository handles automatically an update or create for a subscription plan.

### Registering a Listener

[](#registering-a-listener)

```
	# in your app/listeners.php for example
	Event::listen('vvMalko.Subscriptions.Subscription.Events.*', 'App\Subscriptions\Listeners\EmailNotifier');

	//  we use the laracasts/commander package, so you can inform you about a listener too
	class EmailNotifier extends Laracasts\Commander\Events\EventListener
    {
        /**
         * will be called when event SubscriptionWasCreated was fired
         *
         * @param SubscriptionWasCreated $event
         */
        public function whenSubscriptionWasCreated(SubscriptionWasCreated $event)
        {
            //  do something when a subscription was created (a new plan was set up and no plan exists before
            //  or every plan subscription before was in the past)
        }

        /**
         * will be called when event SubscriptionWasUpdated was fired
         *
         * @param SubscriptionWasUpdated $event
         */
        public function whenSubscriptionWasUpdated(SubscriptionWasUpdated $event)
        {
            //  do something when a subscription was updated (e.g. smaller plan before gets upgraded to a more-featured
            //  plan or a subscription was extended to get longer running)
        }
    }
```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16386820?v=4)[2v.Ma1k0](/maintainers/vvMalko)[@vvMalko](https://github.com/vvMalko)

### Embed Badge

![Health badge](/badges/vvmalko-subscriptions/health.svg)

```
[![Health](https://phpackages.com/badges/vvmalko-subscriptions/health.svg)](https://phpackages.com/packages/vvmalko-subscriptions)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k417.9M1.7k](/packages/nette-utils)[dgr/nohup

A library to run a command in the background, it will return the process's pid, and get it's is running status anytime in the another process, and can be stoped anytime. It support Windows, Linux and Mac osx.

531.7k](/packages/dgr-nohup)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
