Giter Club home page Giter Club logo

laravel-subscriptions's Introduction

Rinvex Subscriptions

⚠️ This package is abandoned and no longer maintained. No replacement package was suggested. ⚠️

👉 If you are interested to step on as the main maintainer of this package, please reach out to me!


Rinvex Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.

Packagist Scrutinizer Code Quality Travis StyleCI License

Considerations

  • Payments are out of scope for this package.
  • You may want to extend some of the core models, in case you need to override the logic behind some helper methods like renew(), cancel() etc. E.g.: when cancelling a subscription you may want to also cancel the recurring payment attached.

Installation

  1. Install the package via composer:

    composer require rinvex/laravel-subscriptions
  2. Publish resources (migrations and config files):

    php artisan rinvex:publish:subscriptions
  3. Execute migrations via the following command:

    php artisan rinvex:migrate:subscriptions
  4. Done!

Usage

Add Subscriptions to User model

Rinvex Subscriptions has been specially made for Eloquent and simplicity has been taken very serious as in any other Laravel related aspect. To add Subscription functionality to your User model just use the \Rinvex\Subscriptions\Traits\HasPlanSubscriptions trait like this:

namespace App\Models;

use Rinvex\Subscriptions\Traits\HasPlanSubscriptions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasPlanSubscriptions;
}

That's it, we only have to use that trait in our User model! Now your users may subscribe to plans.

Note: you can use HasPlanSubscriptions trait on any subscriber model, it doesn't have to be the user model, in fact any model will do.

Create a Plan

$plan = app('rinvex.subscriptions.plan')->create([
    'name' => 'Pro',
    'description' => 'Pro plan',
    'price' => 9.99,
    'signup_fee' => 1.99,
    'invoice_period' => 1,
    'invoice_interval' => 'month',
    'trial_period' => 15,
    'trial_interval' => 'day',
    'sort_order' => 1,
    'currency' => 'USD',
]);

// Create multiple plan features at once
$plan->features()->saveMany([
    new PlanFeature(['name' => 'listings', 'value' => 50, 'sort_order' => 1]),
    new PlanFeature(['name' => 'pictures_per_listing', 'value' => 10, 'sort_order' => 5]),
    new PlanFeature(['name' => 'listing_duration_days', 'value' => 30, 'sort_order' => 10, 'resettable_period' => 1, 'resettable_interval' => 'month']),
    new PlanFeature(['name' => 'listing_title_bold', 'value' => 'Y', 'sort_order' => 15])
]);

Get Plan Details

You can query the plan for further details, using the intuitive API as follows:

$plan = app('rinvex.subscriptions.plan')->find(1);

// Get all plan features                
$plan->features;

// Get all plan subscriptions
$plan->planSubscriptions;

// Check if the plan is free
$plan->isFree();

// Check if the plan has trial period
$plan->hasTrial();

// Check if the plan has grace period
$plan->hasGrace();

Both $plan->features and $plan->planSubscriptions are collections, driven from relationships, and thus you can query these relations as any normal Eloquent relationship. E.g. $plan->features()->where('name', 'listing_title_bold')->first().

Get Feature Value

Say you want to show the value of the feature pictures_per_listing from above. You can do so in many ways:

// Use the plan instance to get feature's value
$amountOfPictures = $plan->getFeatureBySlug('pictures_per_listing')->value;

// Query the feature itself directly
$amountOfPictures = app('rinvex.subscriptions.plan_feature')->where('slug', 'pictures_per_listing')->first()->value;

// Get feature value through the subscription instance
$amountOfPictures = app('rinvex.subscriptions.plan_subscription')->find(1)->getFeatureValue('pictures_per_listing');

Create a Subscription

You can subscribe a user to a plan by using the newSubscription() function available in the HasPlanSubscriptions trait. First, retrieve an instance of your subscriber model, which typically will be your user model and an instance of the plan your user is subscribing to. Once you have retrieved the model instance, you may use the newSubscription method to create the model's subscription.

$user = User::find(1);
$plan = app('rinvex.subscriptions.plan')->find(1);

$user->newPlanSubscription('main', $plan);

The first argument passed to newSubscription method should be the title of the subscription. If your application offer a single subscription, you might call this main or primary, while the second argument is the plan instance your user is subscribing to, and there's an optional third parameter to specify custom start date as an instance of Carbon\Carbon (by default if not provided, it will start now).

Change the Plan

You can change subscription plan easily as follows:

$plan = app('rinvex.subscriptions.plan')->find(2);
$subscription = app('rinvex.subscriptions.plan_subscription')->find(1);

// Change subscription plan
$subscription->changePlan($plan);

If both plans (current and new plan) have the same billing frequency (e.g., invoice_period and invoice_interval) the subscription will retain the same billing dates. If the plans don't have the same billing frequency, the subscription will have the new plan billing frequency, starting on the day of the change and the subscription usage data will be cleared. Also if the new plan has a trial period and it's a new subscription, the trial period will be applied.

Feature Options

Plan features are great for fine-tuning subscriptions, you can top-up certain feature for X times of usage, so users may then use it only for that amount. Features also have the ability to be resettable and then it's usage could be expired too. See the following examples:

// Find plan feature
$feature = app('rinvex.subscriptions.plan_feature')->where('name', 'listing_duration_days')->first();

// Get feature reset date
$feature->getResetDate(new \Carbon\Carbon());

Subscription Feature Usage

There's multiple ways to determine the usage and ability of a particular feature in the user subscription, the most common one is canUseFeature:

The canUseFeature method returns true or false depending on multiple factors:

  • Feature is enabled.
  • Feature value isn't 0/false/NULL.
  • Or feature has remaining uses available.
$user->planSubscription('main')->canUseFeature('listings');

Other feature methods on the user subscription instance are:

  • getFeatureUsage: returns how many times the user has used a particular feature.
  • getFeatureRemainings: returns available uses for a particular feature.
  • getFeatureValue: returns the feature value.

All methods share the same signature: e.g. $user->planSubscription('main')->getFeatureUsage('listings');.

Record Feature Usage

In order to effectively use the ability methods you will need to keep track of every usage of each feature (or at least those that require it). You may use the recordFeatureUsage method available through the user subscription() method:

$user->planSubscription('main')->recordFeatureUsage('listings');

The recordFeatureUsage method accept 3 parameters: the first one is the feature's name, the second one is the quantity of uses to add (default is 1), and the third one indicates if the addition should be incremental (default behavior), when disabled the usage will be override by the quantity provided. E.g.:

// Increment by 2
$user->planSubscription('main')->recordFeatureUsage('listings', 2);

// Override with 9
$user->planSubscription('main')->recordFeatureUsage('listings', 9, false);

Reduce Feature Usage

Reducing the feature usage is almost the same as incrementing it. Here we only substract a given quantity (default is 1) to the actual usage:

$user->planSubscription('main')->reduceFeatureUsage('listings', 2);

Clear The Subscription Usage Data

$user->planSubscription('main')->usage()->delete();

Check Subscription Status

For a subscription to be considered active one of the following must be true:

  • Subscription has an active trial.
  • Subscription ends_at is in the future.
$user->subscribedTo($planId);

Alternatively you can use the following methods available in the subscription model:

$user->planSubscription('main')->active();
$user->planSubscription('main')->canceled();
$user->planSubscription('main')->ended();
$user->planSubscription('main')->onTrial();

Canceled subscriptions with an active trial or ends_at in the future are considered active.

Renew a Subscription

To renew a subscription you may use the renew method available in the subscription model. This will set a new ends_at date based on the selected plan and will clear the usage data of the subscription.

$user->planSubscription('main')->renew();

Canceled subscriptions with an ended period can't be renewed.

Cancel a Subscription

To cancel a subscription, simply use the cancel method on the user's subscription:

$user->planSubscription('main')->cancel();

By default the subscription will remain active until the end of the period, you may pass true to end the subscription immediately:

$user->planSubscription('main')->cancel(true);

Scopes

Subscription Model

// Get subscriptions by plan
$subscriptions = app('rinvex.subscriptions.plan_subscription')->byPlanId($plan_id)->get();

// Get bookings of the given user
$user = \App\Models\User::find(1);
$bookingsOfSubscriber = app('rinvex.subscriptions.plan_subscription')->ofSubscriber($user)->get(); 

// Get subscriptions with trial ending in 3 days
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndingTrial(3)->get();

// Get subscriptions with ended trial
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndedTrial()->get();

// Get subscriptions with period ending in 3 days
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndingPeriod(3)->get();

// Get subscriptions with ended period
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndedPeriod()->get();

Models

Rinvex Subscriptions uses 4 models:

Rinvex\Subscriptions\Models\Plan;
Rinvex\Subscriptions\Models\PlanFeature;
Rinvex\Subscriptions\Models\PlanSubscription;
Rinvex\Subscriptions\Models\PlanSubscriptionUsage;

Roadmap

Looking for contributors!

The following are a set of limitations to be improved, or feature requests that's looking for contributors to implement, all PRs are welcome 🙂

  • Allow paying for multiple occurrences of the same plan (i.e. monthly plan, user can pay for 6 months of that plan) (#64)
  • Plan prorate fields in database isn't utilized, this should be implemented to consolidate extension dates, and prices (#68)
  • Change features to be in a many-to-many relationship with plans. Multiple plans can have the same feature, and many plans can have many features as well (#101)
  • Plan subscription timezone field in database isn't utilized, this should be implemented to respect timezone on date calculations (i.e. starts_at, ends_at, trial_ends_at) (#78)
  • Separate trial feature from the subscription periods and adjust subscriptions accordingly. Users should be able to have a trial period without having a subscription at all (#67)

Changelog

Refer to the Changelog for a full history of the project.

Support

The following support channels are available at your fingertips:

Contributing & Protocols

Thank you for considering contributing to this project! The contribution guide can be found in CONTRIBUTING.md.

Bug reports, feature requests, and pull requests are very welcome.

Security Vulnerabilities

If you discover a security vulnerability within this project, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.

About Rinvex

Rinvex is a software solutions startup, specialized in integrated enterprise solutions for SMEs established in Alexandria, Egypt since June 2016. We believe that our drive The Value, The Reach, and The Impact is what differentiates us and unleash the endless possibilities of our philosophy through the power of software. We like to call it Innovation At The Speed Of Life. That’s how we do our share of advancing humanity.

License

This software is released under The MIT License (MIT).

(c) 2016-2022 Rinvex LLC, Some rights reserved.

laravel-subscriptions's People

Contributors

6ichem avatar amrography avatar dependabot-preview[bot] avatar insign avatar jecovier avatar mo7zayed avatar omranic avatar ppalmeida avatar rattone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-subscriptions's Issues

Package dependancy problem (spatie/laravel-translatable 3.*)

Hi,

I already have spatie/laravel-translatable installed (version 3.1.0), trying to install the package, I'm getting:

  Problem 1
    - Installation request for rinvex/laravel-subscriptions ^1.0 -> satisfiable by rinvex/laravel-subscriptions[v1.0.0].
    - rinvex/laravel-subscriptions v1.0.0 requires spatie/laravel-translatable ^2.1.0 -> satisfiable by spatie/laravel-translatable[2.1.0, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.1.5, 2.2.0, 2.2.1] but these conflict with your requirements or minimum-stability.

Thanks.

Is this package still under maintenance? active development?

Also, why on earth you need to bind it to the container? why this is required:

 // Bind eloquent models to IoC container
        $this->app->singleton('rinvex.subscriptions.plan', $planModel = $this->app['config']['rinvex.subscriptions.models.plan']);
        $planModel === Plan::class || $this->app->alias('rinvex.subscriptions.plan', Plan::class);
        $this->app->singleton('rinvex.subscriptions.plan_features', $planFeatureModel = $this->app['config']['rinvex.subscriptions.models.plan_feature']);
        $planFeatureModel === PlanFeature::class || $this->app->alias('rinvex.subscriptions.plan_features', PlanFeature::class);
        $this->app->singleton('rinvex.subscriptions.plan_subscriptions', $planSubscriptionModel = $this->app['config']['rinvex.subscriptions.models.plan_subscription']);
        $planSubscriptionModel === PlanSubscription::class || $this->app->alias('rinvex.subscriptions.plan_subscriptions', PlanSubscription::class);
        $this->app->singleton('rinvex.subscriptions.plan_subscription_usage', $planSubscriptionUsageModel = $this->app['config']['rinvex.subscriptions.models.plan_subscription_usage']);
        $planSubscriptionUsageModel === PlanSubscriptionUsage::class || $this->app->alias('rinvex.subscriptions.plan_subscription_usage', PlanSubscriptionUsage::class);

??

Can't create a plan. Trait does not exist.

Hi There,

I am trying to create a plan but i keep getting this error:
PHP Fatal error: Trait 'Rinvex\Support\Traits\HasTranslations' not found.

I see that the trait does not exist. Any way to fix?

Use with different model

Can we use this on a different model (other than User)? We need subscriptions to be on an Account level (each account has multiple Users, so we can't store subscriptions on Users).

Plan subscription timezone

Hello,

Thanks for this great package, we already have [Timezone] column inside Plan subscriptions table, but as I can see, we don`t use it at all, so I think we can make some changes on the Period class in __construcotr function, by passing timezone field from Plan table:

public function __construct($interval = 'month', $count = 1, $start = '', $timezone = '')
    {
		
        $this->interval = $interval;

        if (empty($start)) {

	 $this->start = empty($timezone) ? now() : Carbon::now($timezone); // forexample:'Europe/London'
			
           /// $this->start = now();
		   
        } elseif (! $start instanceof Carbon) {
			
	 $this->start =  empty($timezone) ? new Carbon($start) : Carbon::createFromFormat('Y-m-d H:i:s', $start, $timezone);
			
            //$this->start = new Carbon($start);
			
        } else {
            $this->start = $start;
        }

        if ($count > 0) {
            $this->period = $count;
        }
        $start = clone $this->start;
        $method = 'add'.ucfirst($this->interval).'s';
        $this->end = $start->{$method}($this->period);
    }

Cachable issue

Hi,
Do you think Cachable should be detached from the core rather than being implicit?
Or we can create an easy switch using config.

Consider this scenario

  • Application User selects the subscription plan & makes payment.
  • We have created a new entry in plan_subscriptions
  • Now we want to let user access private pages because he is now a subscriber of our service.

But when we might go to check if the user is actually subscribed to the plan or not we might get the earlier cached result & user might still appear to be unsubscribed.

Your thoughts please?

Mutiple currencies and prices

Hello, i have a multiple language website.
I need to create one subscription with price for each language.

There is an option to create subscription and select the price and currency by user language?

Thanks,
Itsik

Column not found: 1054 Unknown column 'cacheDriver'

When trying to seed the Plan, the console outputs this error:

  Illuminate\Database\QueryException  : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cacheDriver' in 'field list' (SQL: insert into `plan_features` (`title`, `name`, `value`, `sort_order`, `resettable_period`, `resettable_interval`, `plan_id`, `cacheDriver`, `cacheLifetime`, `updated_at`, `created_at`) values ({"en":"listing_duration_days"}, basic_news, 1, 1, 1, month, 1, , -1, 2018-03-01 19:04:29, 2018-03-01 19:04:29))

  at /Users/pedropauloalmeida/WORK/PROJECTS/WAVEWEB/SPACE-ROCKET/space-rocket/vendor/laravel/framework/src/Illuminate/Database/Connection.php: 664
  660:         // If an exception occurs when attempting to run a query, we'll format the error
  661:         // message to include the bindings with SQL, which will make this exception a
  662:         // lot more helpful to the developer instead of just the database's errors.
  663:         catch (Exception $e) {
  664:             throw new QueryException(
  665:                 $query, $this->prepareBindings($bindings), $e
  666:             );
  667:         }
  668: 
  669:         return $result;

  Exception trace:

  1   PDOException::("SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cacheDriver' in 'field list'")
      /Users/pedropauloalmeida/WORK/PROJECTS/WAVEWEB/SPACE-ROCKET/space-rocket/vendor/laravel/framework/src/Illuminate/Database/Connection.php : 452

I think it comes from the trait CacheableEloquent. I do not know how it works and why the Plan needs it. Could you please check it out?

My factory methods are:

use Rinvex\Subscriptions\Models\Plan;
use Rinvex\Subscriptions\Models\PlanFeature;

$factory->define(Plan::class, function (Faker $faker) {
    return [
        'name' => $faker->word,
        'title' => $faker->words(3),
        'description' => $faker->sentence(5, true),
        'is_active' => 1,
        'price' => $faker->randomFloat($nbMaxDecimals = 2, $min = 19.0, $max = 50.0),
        'signup_fee' => $faker->randomFloat($nbMaxDecimals = 2, $min = 1.0, $max = 10.0),
        'invoice_period' => 1,
        'invoice_interval' => 'month',
        'trial_period' => 15,
        'trial_interval' => 'day',
        'sort_order' => 1,
        'currency' => 'BRL',
        'active_subscribers_limit' => 10,
        'grace_period' => 0,
        'grace_interval' => 'day',
        'prorate_day' => 1,
        'prorate_period' => 1,
        'prorate_extend_due' => 1,
    ];
});

$factory->define(PlanFeature::class, function (Faker $faker) {
    return [
        'title' => 'listing_duration_days',
        'name' => 'listing_duration_days',
        'value' => 30,
        'sort_order' => 10,
        'resettable_period' => 1,
        'resettable_interval' => 'month'
    ];
});

And the seeder:

public function run()
    {
        $basicPlan = factory(Plan::class)->create([
            'title' => 'Basic',
            'name' => 'basic',
            'description' => "", //
            'price' => 4.90, // Price per user <= 10 users
            'active_subscribers_limit' => 10,
            'signup_fee' => 0.00,
            'invoice_period' => 1,
            'invoice_interval' => 'month',
            'trial_period' => 15,
            'trial_interval' => 'day',
            'sort_order' => 1,
            'currency' => 'BRL',
        ]);

        $basicPlan->features()->saveMany([
            factory(PlanFeature::class)->make(['name' => 'basic_news', 'value' => 1, 'sort_order' => 0]), // notícias
            factory(PlanFeature::class)->make(['name' => 'basic_announcements', 'value' => 1, 'sort_order' => 1]), // comunicados
            factory(PlanFeature::class)->make(['name' => 'basic_capsule', 'value' => 1, 'sort_order' => 2]), // biblioteca
            factory(PlanFeature::class)->make(['name' => 'basic_book', 'value' => 1, 'sort_order' => 3]),
        ]);

        $premiumPlan = factory(Plan::class)->create([
            'title' => 'Basic',
            'name' => 'basic',
            'description' => "Plano básico", //
            'price' => 9.90, // Price per user >= 10 users
            'active_subscribers_limit' => 10000,
            'signup_fee' => 0.00,
            'invoice_period' => 1,
            'invoice_interval' => 'month',
            'trial_period' => 15,
            'trial_interval' => 'day',
            'sort_order' => 1,
            'currency' => 'BRL',
        ]);

        $premiumPlan->features()->saveMany([
            factory(PlanFeature::class)->make(['name' => 'premium_news', 'value' => 1, 'sort_order' => 0]), // notícias
            factory(PlanFeature::class)->make(['name' => 'premium_announcements', 'value' => 1, 'sort_order' => 1]), // comunicados
            factory(PlanFeature::class)->make(['name' => 'premium_capsule', 'value' => 1, 'sort_order' => 2]), // biblioteca
            factory(PlanFeature::class)->make(['name' => 'premium_book', 'value' => 1, 'sort_order' => 3]),
        ]);
    }

Name vs Title

Hi, guys.
I think I found a new small bug:

In HasSubscriptions trait, we're missing the "title" in subscriptions->create method:

return $this->subscriptions()->create([
            'name' => $subscription,
            'plan_id' => $plan->getKey(),
            'trial_ends_at' => $trial->getEndDate(),
            'starts_at' => $period->getStartDate(),
            'ends_at' => $period->getEndDate(),
        ]);

Then it leads to errors in Validation:

#validationErrors: Illuminate\Support\MessageBag {#818
      #messages: array:1 [
        "title" => array:1 [
          0 => "The title field is required."
        ]
      ]
      #format: ":message"
    }

I did not get the difference between "title" and "name". What's de difference in these properties and when should I use each one of those?

So, what I did was just this:

return $this->subscriptions()->create([
            'name' => $subscription,
            'title' => $subscription,
            'plan_id' => $plan->getKey(),
            'trial_ends_at' => $trial->getEndDate(),
            'starts_at' => $period->getStartDate(),
            'ends_at' => $period->getEndDate(),
        ]); 

And the tests are green until now. Could you check if it works well for you too?

Thank you.

[Exception] On creating new plan / the given data was invalid

Reported by: Almeida Silva

$plan = app('rinvex.subscriptions.plan')->create([
            'name' => 'Pro',
            'description' => 'Pro plan',
            'price' => 9.99,
            'invoice_period' => 1,
            'invoice_interval' => 'month',
            'trial_period' => 15,
            'trial_interval' => 'd',
            'sort_order' => 1,
            'currency' => 'USD',
        ]);
.Illuminate\Support\MessageBag {#732
  #messages: array:2 [
    "signup_fee" => array:1 [
      0 => "The signup fee field is required."
    ]
    "invoice_interval" => array:1 [
      0 => "The selected invoice interval is invalid."
    ]
  ]
  #format: ":message"
}

PlanFeature: canUseFeature null pointer exception

In PlanSubscription model, I have a problem in $usage variable, line 502: it returns NULL:

$usage = $this->usage()->byFeatureName($featureName)->first();

The query was scoped correctly (I think):

select * from `plan_subscription_usage` where `plan_subscription_usage`.`subscription_id` = ? and `plan_subscription_usage`.`subscription_id` is not null and `feature_id` = ?

But at line 510, the first attempt is if ($usage->expired() on a NULL pointer.

Hope it helps.

https://github.com/rinvex/subscriptions/blob/285f08eaec97048b4fec58da51ef8c083fdf5847/src/Models/PlanSubscription.php#L499

My test looks like this:

/** @ test */
    public function a_feature_can_be_used_by_a_user() {
        $plan = $this->_createPlan();
        $user = factory(Admin::class)->create(["email" => "[email protected]"]);
        $subscription = $user->newSubscription('pro', $plan);

        // Check if DB has the subscription:
        $plan = Plan::first();
        $subscriptions = $plan->subscriptions;
        $user->refresh();

        // These tests are OK:
        $this->assertNotNull($plan);
        $this->assertNotNull($subscriptions);
        $this->assertTrue($user->subscribedTo($plan->id));

        // Here are the null point exceptions:
        $this->assertTrue($user->subscription('pro')->canUseFeature('news'));
        $this->assertTrue($user->subscription('pro')->canUseFeature('communications'));
        $this->assertTrue($user->subscription('pro')->canUseFeature('help'));
        $this->assertTrue($user->subscription('pro')->canUseFeature('library'));
    }

    private function _createPlan() {
        $plan = factory(Plan::class)->create([
                "name" => "pro",
        ]);
        $plan->features()->saveMany([
            factory(PlanFeature::class)->make(['name' => 'news', 'value' => 50, 'sort_order' => 1]), // notícias
            factory(PlanFeature::class)->make(['name' => 'communications', 'value' => 10, 'sort_order' => 5]), // comunicados
            factory(PlanFeature::class)->make(['name' => 'help', 'value' => 30, 'sort_order' => 10, 'resettable_period' => 1, 'resettable_interval' => 'month']),
            factory(PlanFeature::class)->make(['name' => 'library', 'value' => 'Y', 'sort_order' => 15]) // biblioteca
        ]);
        return $plan;
    }

Request for PHP 7.3 and Laravel 6 support version

Not a lot of other laravel packages are ready for php 7.4.

I have been using your package since last year without any issue.

After php 7.4 is out, this package together with depending packages like (laravel-sluggable and laravel-support, etc) has updated for php 7.4.

I have submitted and closed an issue on this repo recently with a syntax error issue. After tracing the bug, I found out that it is because I am on php 7.3. Once I switched to php 7.4 everything is fine.

I have other packages which will break on php 7.4, so I cannot use this package anymore.

It will be really great if there is a version that work with php 7.3 and laravel 6.

Laravel 5.5 composer require

After the latest commit where the support for the Laravel 5.5 was dropped, how can I still use this package without update Laravel to 5.6? If I try to resolve the dependencies in many way but composer fails every time.

How disable the translation for entire package

Hi,
Hope you are doing well?
How to disable the translation for the entire package - like create a plan , as it creates an extra burden for plan name when I fetch it directly.

$plan = app('rinvex.subscriptions.plan')->create([
'name' => 'Pro',
'description' => 'Pro plan',
'price' => 9.99,
'signup_fee' => 1.99,
'invoice_period' => 1,
'invoice_interval' => 'month',
'trial_period' => 15,
'trial_interval' => 'day',
'sort_order' => 1,
'currency' => 'USD',
]);

In database it creates plan name as {"en":"Pro"} - I want only Pro

Thanks in advance
Warm Regards

Laravel 5.8 Support

What's the rationale behind enforcing Laravel ^6.0? Why not provide support for, at least from, v5.5 upwards?

Plus your releases are not descriptive enough for one to know where you started enforcing ^6.0 so I can just easily pull the release before that.

How do I create a plan without trial period?

In order to create a plan without trial, I set the trial_period to zero.

$plan = Plan::create([
    'slug' => 'free',
    'name' => 'Free Plan',
    'price' => 0,
    'signup_fee' => 0,
    'invoice_period' => 1,
    'invoice_interval' => 'month',
    'trial_period' => 0,
    'trial_interval' => 'day',
    'sort_order' => 1,
    'currency' => 'TWD',
]);

When a new user registered, the system will auto assign free plan to user.

// Assume today is 2019-11-24
$subscription = $user->newSubscription('primary', $plan);

// Unexpectedly the start date would be: 2019-11-25
// It should have been 2019-11-24
$subscription->starts_at;

I dig into the newSubscription function and found the helper service class called Period.

use Rinvex\Subscriptions\Services\Period;

public function newSubscription($subscription, Plan $plan): PlanSubscription
    {
        $trial = new Period($plan->trial_interval, $plan->trial_period, now());
        $period = new Period($plan->invoice_interval, $plan->invoice_period, $trial->getEndDate());

        return $this->subscriptions()->create([
            'name' => $subscription,
            'plan_id' => $plan->getKey(),
            'trial_ends_at' => $trial->getEndDate(),
            'starts_at' => $period->getStartDate(),
            'ends_at' => $period->getEndDate(),
        ]);
    }

So the root cause might be here. In the constructor of Period class:

namespace Rinvex\Subscriptions\Services;  

/**
 * Interval count.
 *
 * @var int
 */
protected $period = 1;

public function __construct($interval = 'month', $count = 1, $start = '')
    {
        $this->interval = $interval;

        if (empty($start)) {
            $this->start = now();
        } elseif (! $start instanceof Carbon) {
            $this->start = new Carbon($start);
        } else {
            $this->start = $start;
        }

        // It won't override the period variable 
        // and the default period is set to 1.
        if ($count > 0) {
            $this->period = $count;
        }

        $start = clone $this->start;
        $method = 'add'.ucfirst($this->interval).'s';
        $this->end = $start->{$method}($this->period);
    }

Renew need to calculate remaining days for extend

If you extend user's package it's renewing 1 month from now. We don't have automatic payment.

If user have 3 weeks already, user lose it's remaning days. We need end_data to be 1 month+3 week to be fair if user renewing before end date.

BadMethodCallException on byFeatureSlug

$user = $request->user();
$subscription = $user->subscription('main');
$feature = sprintf('%s-%s', $subscription->plan->name, $feature);

return $subscription->canUseFeature($feature);

i get this error:

"Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::byFeatureSlug()"

Call to undefined method guardAgainstUntranslatableAttribute()

Hi,

After upgrading spatie/laravel-translatable to 3.1, creating a new Plan model returns Call to undefined method guardAgainstUntranslatableAttribute().

I'm wondering why you are using rinvex/laravel-support HasTranslations trait instead of spatie and simply override in Plan.php.

Plus, HasTranslations.php definition of getTranslations method now does not match spatie/laravel-translatable: ^3.1,

Redirect loop on plan creation

I installed this package and setup everything as instructed but when i try to create a new plan, i get a redirect loop error. The code i am running is as shown below:
```
$plan = app('rinvex.subscriptions.plan')->create([
'name' => 'Pro',
'description' => 'Pro plan',
'price' => 9.99,
'invoice_period' => 1,
'invoice_interval' => 'month',
'trial_period' => 15,
'trial_interval' => 'd',
'sort_order' => 1,
'currency' => 'USD',
]);

Thanks.

Column `slug` does not exists

Hi, guys.

When I try to assert the use of a feature, I do like this:

// (... subscription is made on Controller, it goes ok as far as I can see)... Then:

$plan = app('rinvex.subscriptions.plan')->find(1);
$subscriptions = $plan->subscriptions;
$user->refresh();
$this->assertTrue($user->subscription('pro')->canUseFeature('news'));

And the error in console is:

1) Tests\Feature\SusbscriptionTest::a_user_can_subscribe_to_plan
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'slug' in 'where clause' (SQL: select * from `plan_subscriptions` where `plan_subscriptions`.`user_id` = 1 and `plan_subscriptions`.`user_id` is not null and `plan_subscriptions`.`user_type` = App\User and `slug` = pro limit 1)
...
Caused by
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'slug' in 'where clause'

I only could find slug in code at Rinvex\Subscriptions\Traits\HasSubscriptions:

public function subscription(string $subscriptionSlug): ?PlanSubscription
    {
        return $this->subscriptions()->where('slug', $subscriptionSlug)->first();
    }

If I change that 'slug' to 'name', I get this error:

1) Tests\Feature\SusbscriptionTest::a_user_can_subscribe_to_plan
Error: Call to a member function expired() on null

/Users/pedropauloalmeida/WORK/PROJECTS/WAVEWEB/SPACE-ROCKET/space-rocket/vendor/rinvex/subscriptions/src/Models/PlanSubscription.php:510
/Users/pedropauloalmeida/WORK/PROJECTS/WAVEWEB/SPACE-ROCKET/space-rocket/tests/Feature/SusbscriptionTest.php:33

So I checked the $plan->features->pluck('name') and the result is as expected:

Illuminate\Support\Collection {#704
  #items: array:4 [
    0 => "news"
    1 => "communications"
    2 => "help"
    3 => "library"
  ]
}

So, the problems I've found are:

  1. The Rinvex\Subscriptions\Traits\HasSubscriptions uses "slug" column, that do not exists anymore.
  2. If I change that column to "name", the model /Models/PlanSubscription.php:510 leads to a null point exception.

Could you please check it out?

Oh, a last warn: could it be related to this issue?
#22

Thank you.

The package requires illuminate/support

composer require rinvex/laravel-subscriptions
Cannot create cache directory /home/tanvir/.composer/cache/repo/https---repo.packagist.org/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/tanvir/.composer/cache/files/, or directory is not writable. Proceeding without cache
Using version ^1.0 for rinvex/laravel-subscriptions
./composer.json has been updated
Cannot create cache directory /home/tanvir/.composer/cache/repo/https---repo.packagist.org/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/tanvir/.composer/cache/files/, or directory is not writable. Proceeding without cache
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.6.33
    - Conclusion: don't install laravel/framework v5.6.33
    - rinvex/laravel-subscriptions v1.0.0 requires illuminate/support ~5.7.0 -> satisfiable by laravel/framework[5.7.x-dev], illuminate/support[5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9].
    - rinvex/laravel-subscriptions v1.0.1 requires illuminate/support ~5.7.0 -> satisfiable by laravel/framework[5.7.x-dev], illuminate/support[5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9].
    - rinvex/laravel-subscriptions v1.0.2 requires illuminate/support ~5.7.0 -> satisfiable by laravel/framework[5.7.x-dev], illuminate/support[5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9].
    - Can only install one of: laravel/framework[5.7.x-dev, v5.6.33].
    - don't install illuminate/support 5.7.17|don't install laravel/framework v5.6.33
    - don't install illuminate/support 5.7.18|don't install laravel/framework v5.6.33
    - don't install illuminate/support 5.7.19|don't install laravel/framework v5.6.33
    - don't install illuminate/support 5.7.x-dev|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.0|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.1|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.10|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.11|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.15|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.2|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.20|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.21|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.22|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.23|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.3|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.4|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.5|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.6|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.7|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.8|don't install laravel/framework v5.6.33
    - don't install illuminate/support v5.7.9|don't install laravel/framework v5.6.33
    - Installation request for laravel/framework (locked at v5.6.33, required as 5.6.*) -> satisfiable by laravel/framework[v5.6.33].
    - Installation request for rinvex/laravel-subscriptions ^1.0 -> satisfiable by rinvex/laravel-subscriptions[v1.0.0, v1.0.1, v1.0.2].

Prorata usage

I see the plan table has fields for maintaining Prorata config. Is there any docs on its usage or is this feature still pending?

rinvex.subscriptions.plan_subscription does not exist

@Omranic Thanks for this awesome plugin, but we got same error in this plugin.

we required the plan name which has been subscribed by the user, so we are tring to get this

$user = Auth::id();
$bookingsOfUser = app('rinvex.subscriptions.plan_subscription')->ofUser($user)->get();

but got an error:

ReflectionException (-1)
Class rinvex.subscriptions.plan_subscription does not exist

screenshot from 2019-01-31 20-08-15

If anybody knows the solution, let me know. Thanks in advance.

Support for laravel 5.8

Hi,
Looks like currently it does not supports laravel 5.8
Any plan to support it any time soon?
Thanks :)

How to use trial period

Is trial period feature actually available? There is a field in database and trial period is somehow used in calculations of subscription status. But I failed to find any information about how to start trial but not the subscription itself.

For example, if my plan has 1 month invoice period and a 7-day trial period, and I create a new subscription to this plan, then that subscription would be active for 1 month and 7 days? How can I automatically ask user for actual payment, when trial ends?

BadMethodCallException Call to undefined method Rinvex\Subscriptions\Models\Plan::getFeatureByName()

image

$plan = app('rinvex.subscriptions.plan')->find(1);
//$plan4 = app('rinvex.subscriptions.plan')->find(4);

    // Get all plan features  
    echo 'Get all plan features: ';              
    echo $plan->features;
    echo '<br/>';

    // Get all plan subscriptions
    echo 'Get all plan subscriptions: ';
    echo $plan->subscriptions;
    echo '<br/>';

    // Check if the plan is free
    echo 'Check if the plan is free: ';
    echo $plan->isFree();
    echo '<br/>';

    // Check if the plan has trial period
    echo 'Check if the plan has trial period: ';
    echo $plan->hasTrial();
    echo '<br/>';

    // Check if the plan has grace period
    echo 'Check if the plan has grace period: ';
    echo $plan->hasGrace();
    echo '<br/>';
   
    $amountOfPictures = $plan->getFeatureByName('referencing_my_places_and_my_programming')->value;

Cannot use with Cashier

It's not possible to use laravel-subscription with cashier, there is a conflict when adding the trait to the user model.
Any idea how to solve that?

Problem with composer require

Hi,

I am trying to install this package and use it with a laravel 5.5 project but i get this error instead:

Problem 1
- Installation request for rinvex/subscriptions ^0.0.2 -> satisfiable by rinvex/subscriptions[v0.0.2].
- rinvex/subscriptions v0.0.2 requires rinvex/cacheable dev-develop -> satisfiable by rinvex/cacheable[dev-develop] but these conflict with your requirements or minimum-stability.

Is there a propery way of installing it?
Regards
Peter

Slug column is unique, documentation uses it as a query

In the documentation you mention this type of call a lot. This for me meant that i could query the subscription by its slug and not id which is true. But its quite not what you expect as slugs are unique, so this would not be practical if you have multiple users, as the following user would get main-1 and so on.

$user->subscription('main')

Paying for a plan x times

How can i pay for a plan x times, e.g monthly bill, but i want to pay for 6 months , ... but i dont want to create a subscription record for it because its a one off occurrence

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.