承接 laravelcm/laravel-subscriptions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravelcm/laravel-subscriptions

Composer 安装命令:

composer require laravelcm/laravel-subscriptions

包简介

Laravel 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.

README 文档

README

Laravel v10.x Build Status Coding Standards Total Downloads Packagist Packagist

Laravel Subscriptions

Laravel 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.

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 laravelcm/laravel-subscriptions
  2. Publish resources (migrations and config files):

    php artisan vendor:publish --provider="Laravelcm\Subscriptions\SubscriptionServiceProvider"
  3. Execute migrations via the following command:

    php artisan migrate
  4. Done!

Usage

Add Subscriptions to User model

Laravel 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 Laravelcm\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

use Laravelcm\Subscriptions\Models\Plan;
use Laravelcm\Subscriptions\Models\Feature;
use Laravelcm\Subscriptions\Interval;

$plan = Plan::create([
    'name' => 'Pro',
    'description' => 'Pro plan',
    'price' => 9.99,
    'signup_fee' => 1.99,
    'invoice_period' => 1,
    'invoice_interval' => Interval::MONTH->value,
    'trial_period' => 15,
    'trial_interval' => Interval::DAY->value,
    'sort_order' => 1,
    'currency' => 'USD',
]);

// Create multiple plan features at once
$plan->features()->saveMany([
    new Feature(['name' => 'listings', 'value' => 50, 'sort_order' => 1]),
    new Feature(['name' => 'pictures_per_listing', 'value' => 10, 'sort_order' => 5]),
    new Feature(['name' => 'listing_duration_days', 'value' => 30, 'sort_order' => 10, 'resettable_period' => 1, 'resettable_interval' => 'month']),
    new Feature(['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:

use Laravelcm\Subscriptions\Models\Plan;

$plan = Plan::find(1);

// 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();

Both $plan->features and $plan->subscriptions 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 Laravelcm\Subscriptions\Models\Feature;
use Laravelcm\Subscriptions\Models\Subscription;

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

// Query the feature itself directly
$amountOfPictures = Feature::where('slug', 'pictures_per_listing')->first()->value;

// Get feature value through the subscription instance
$amountOfPictures = 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.

use Laravelcm\Subscriptions\Models\Plan;
use App\Models\User;

$user = User::find(1);
$plan = 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:

use Laravelcm\Subscriptions\Models\Plan;
use Laravelcm\Subscriptions\Models\Subscription;

$plan = Plan::find(2);
$subscription = 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:

use Laravelcm\Subscriptions\Models\Feature;

// Find plan feature
$feature = 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

use Laravelcm\Subscriptions\Models\Subscription;
use App\Models\User;

// Get subscriptions by plan
$subscriptions = Subscription::byPlanId($plan_id)->get();

// Get bookings of the given user
$user = User::find(1);
$bookingsOfSubscriber = Subscription::ofSubscriber($user)->get(); 

// Get subscriptions with trial ending in 3 days
$subscriptions = Subscription::findEndingTrial(3)->get();

// Get subscriptions with ended trial
$subscriptions = Subscription::findEndedTrial()->get();

// Get subscriptions with period ending in 3 days
$subscriptions = Subscription::findEndingPeriod(3)->get();

// Get subscriptions with ended period
$subscriptions = Subscription::findEndedPeriod()->get();

Models

Laravel Subscriptions uses 4 models:

Laravelcm\Subscriptions\Models\Plan;
Laravelcm\Subscriptions\Models\Feature;
Laravelcm\Subscriptions\Models\Subscription;
Laravelcm\Subscriptions\Models\SubscriptionUsage;

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 developers@laravel.cm. All security vulnerabilities will be promptly addressed.

About Laravel Cameroon

The community of PHP and Laravel developers in Cameroon, the largest gathering of developers in Cameroon.

License

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

(c) 2018-2023 Laravel Cameroun, Some rights reserved.

laravelcm/laravel-subscriptions 适用场景与选型建议

laravelcm/laravel-subscriptions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67.47k 次下载、GitHub Stars 达 241, 最近一次更新时间为 2023 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「subscription」 「feature」 「laravel」 「value」 「plan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laravelcm/laravel-subscriptions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravelcm/laravel-subscriptions 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravelcm/laravel-subscriptions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 67.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 241
  • 点击次数: 28
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 241
  • Watchers: 5
  • Forks: 41
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-02