rabol/laravel-simple-subscription
Composer 安装命令:
composer require rabol/laravel-simple-subscription
包简介
Simple subscription package for Laravel
README 文档
README
This is a simple to use subscription package for Laravel.
It is heavvly inspired by the renvex/laravel-subscriptions package, just simpler and working :)
Sorry, My point is that the renvex packages seems to be abandond.
Payments
- Payment support is in the works, so stay tuned.
- We will most likley use laraveldaily/laravel-invoices and then a payment package
Installation
You can install the package via composer:
composer require rabol/laravel-simple-subscription
You can publish and run the migrations with:
php artisan vendor:publish --provider="Rabol\SimpleSubscription\SimpleSubscriptionServiceProvider" --tag="laravel-simple-subscription-migrations" php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Rabol\SimpleSubscription\SimpleSubscriptionServiceProvider" --tag="laravel-simple-subscription-config"
This is the contents of the published config file:
return [
];
Usage
Add Subscriptions to your model
For the sake of simplicity there is a trait that can be added to any model.
The most common use case is to add Subscription functionality to your User model just use the Rabol\SimpleSubscription\Traits\HasSubscriptions trait like this:
namespace App\Models; use Rabol\SimpleSubscription\Traits\HasSubscriptions; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasSubscriptions; }
That's it, now you can use subscriptions on your user model only have to use that trait in our User model! Now your users may subscribe to plans.
Create a new Plan
$newPlan = SimpleSubscriptionPlan::create([ 'name' => 'My cool subscription', 'description' => 'This is a very cool plan', 'is_active' => true, 'price' => 12.50, 'signup_fee' => 0, 'currency' => 'eur', 'trial_period' => 1, 'trial_interval' => 'week', 'invoice_period' => 1, 'invoice_interval' => 'month', 'grace_period' => 3, 'grace_interval' => 'day', ]);
Add a feature
$planFeature = $newPlan ->features() ->create([ 'name' => 'My cool feature', 'description' => 'This is my cool feature', 'value' => 100, 'resettable_period' => 2, 'resettable_interval' => 'month', 'sort_order' => 1, ]);
Subscription plan details
$plan = SimpleSubscriptionPlan::find(1); or $plan = SimpleSubscriptionPlan::whereName('My cool subscription')->first(); // Get all plan features $plan->features; // Get all plan subscriptions $plan->subscriptions; // 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();
Get Feature Value
There are different ways to get information about a feature on a plan To get the value of the feature 'My cool feature'.:
// Use the plan instance to get feature's value $myCoolFeatureValue = $plan->getFeatureByName('My Cool feature')->value;
Get feature usage
// Get the usage of the feature you can $myCoolFeatureUsage = SimpleSubscriptionPlanFeature::wherePlanId(1)->whereName('Feature 1')->first()->usage()->first()->used;
Create a Subscription
You can subscribe a user to a plan by using the newSubscription() function available in the HasSubscriptions 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 = SimpleSubscriptionPlan::whereName('My cool subscription')->first(); $user->newSubscription($plan);
you can also specify the start date like this:
$user = User::find(1); $plan = SimpleSubscriptionPlan::whereName('My cool subscription')->first(); $user->newSubscription($plan, Carbon::today());
If no start date is specified, the subscription will start today. The end date is calculated from the settings on the plan.
Change the Plan
To change the plan do this:
$newPlan = SimpleSubscriptionPlan::whereName('My second cool plan')->first(); $subscription = SimpleSubscriptionPlanSubscription::whereName('My cool subscription')->first(); // Change subscription plan $subscription->changePlan($newPlan);
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.
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
- Or feature has remaining uses available.
$user->subscription('My cool subscription')->canUseFeature('My cool feature');
Other feature methods on the user subscription instance are:
getFeatureUsage: returns how many times the user has used a particular feature.getFeatureRemaining: returns available uses for a particular feature.getFeatureValue: returns the feature value.
All methods share the same signature: e.g.
$user->subscription('My cool subscription')->getFeatureUsage('My cool feature');.
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->subscription('My cool subscription')->recordFeatureUsage('My cool feature');
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->subscription('My cool subscription')->recordFeatureUsage('My cool feature', 2); // Override with 9 $user->subscription('My cool subscription')->recordFeatureUsage('My cool feature', 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:
The difference from the recordFeatureUsage method is that the reduceFeatureUsage will not look at reset date
$user->subscription('My cool subscription')->reduceFeatureUsage('My cool feature', 2);
Increase Feature Usage
Increasing the feature usage is almost the same as recordFeatureUsage. this function simply add a given quantity to the value:
One more difference from the recordFeatureUsage method is that the increaseFeatureUsage will not look at reset date
$user->subscription('My cool subscription')->increaseFeatureUsage('My cool feature', 2);
Clear The Subscription Usage Data
$user->subscription('My cool subscription')->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_atis in the future.
$user->subscribedToPlanId($planId); or $user->subscribedToPlanName('My cool plan');
Alternatively you can use the following methods available in the subscription model:
$user->subscription('My cool subscription')->active(); $user->subscription('My cool subscription')->canceled(); $user->subscription('My cool subscription')->ended(); $user->subscription('My cool subscription')->onTrial();
Canceled subscriptions with an active trial or
ends_atin 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->subscription('My cool subscription')->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->subscription('My cool subscription')->cancel();
By default the subscription will remain active until the end of the period, you may pass true to end the subscription immediately:
$user->subscription('My cool subscription')->cancel(true);
Scopes
Subscription Model
// Get subscriptions by plan $subscriptions = SimpleSubscriptionPlanSubscription::byPlanId($plan_id)->get(); // Get bookings of the given user $user = \App\Models\User::find(1); $bookingsOfUser = SimpleSubscriptionPlanSubscription::ofSubscriber($user)->get(); // Get subscriptions with trial ending in 3 days $subscriptions = SimpleSubscriptionPlanSubscription::findEndingTrial(3)->get(); // Get subscriptions with ended trial $subscriptions = SimpleSubscriptionPlanSubscription::findEndedTrial()->get(); // Get subscriptions with period ending in 3 days $subscriptions = SimpleSubscriptionPlanSubscription::findEndingPeriod(3)->get(); // Get subscriptions with ended period $subscriptions = SimpleSubscriptionPlanSubscription::findEndedPeriod()->get();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
rabol/laravel-simple-subscription 适用场景与选型建议
rabol/laravel-simple-subscription 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rabol」 「laravel-simple-subscription」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rabol/laravel-simple-subscription 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rabol/laravel-simple-subscription 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rabol/laravel-simple-subscription 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Logviewer for Filament admin
Get up and running with subscriptions in your Laravel app in minutes instead of days
This is a simple but handy package that can help you setup your local development environment
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-20