Subscriptionify: Feature-Based Subscription Management for Laravel

发布日期 经过

Subscriptionify: Feature-Based Subscription Management for Laravel image

Subscriptionify is a Laravel package, built by Rasel Islam Rafi , for modelling subscription plans and the features they unlock. Where Laravel Cashier wraps a payment provider's billing API, Subscriptionify stays gateway-agnostic: it tracks plans, feature quotas, and usage in your own database, leaving the question of who collects the money to you. That makes it a fit for applications that bill through a provider Cashier doesn't cover, charge from a prepaid balance, or grant access without charging at all. It requires PHP 8.2 and supports Laravel 11, 12, and 13.

Installation publishes a config file and migrations that create six tables for plans, features, the plan/feature pivot, subscriptions, usage records, and direct feature grants:

作曲家 要求 revoltify/subscriptionify
php 工匠 供应商:发布 --tag=subscriptionify-config
php 工匠 供应商:发布 --tag=subscriptionify-migrations
php 工匠 迁移

Any model can become billable by implementing the Subscribable contract and adding the InteractsWithSubscriptions trait, so you can attach subscriptions to a User , 一个 Organisation , or a Workspace , depending on how your application is structured:

使用 Revoltify\Subscriptionify\Concerns\InteractsWithSubscriptions ;
使用 Revoltify\Subscriptionify\Contracts\Subscribable ;
班级 工作区 延伸 模型 实现 Subscribable
{
使用 InteractsWithSubscriptions ;
}

Four feature types for different quota patterns

The core idea is that not every feature behaves the same way. A plan might gate access to a capability, meter a depletable monthly allowance, or cap a running total. Subscriptionify models these as four distinct feature types, each with its own consumption rules:

  • 切换 is a plain on/off gate, used for capabilities a plan either includes or doesn't.
  • Consumable is a depletable quota that resets on a schedule, such as a monthly allowance of API calls.
  • 限制 is a hard cap on a running total that can be freed again, such as active projects or seats, where deleting one frees a slot.
  • Metered tracks pay-per-use consumption with no cap, charging per unit.

Features are created once, then attached to plans through a pivot that carries the allocation for that plan:

使用 Revoltify\Subscriptionify\Models\Feature ;
使用 Revoltify\Subscriptionify\Enums\FeatureType ;
$报告 = 特征 :: 创造 ([
'姓名' => 'Exported Reports' ,
'slug' => 'reports' ,
'类型' => FeatureType :: Consumable ,
]);
$plan -> 特征 () -> ($reports, [
'价值' => 500 , // 0 means unlimited
'unit_price' => '0.02000000' , // price per unit once the quota is exceeded
'reset_period' => 1 ,
'reset_interval' => '月' ,
]);

Usage tracking on the subscribable model

Once a model is subscribed to, the usage methods live directly on it. You check access, test whether a number of units is available, and record consumption without reaching into the subscription or pivot records yourself:

$workspace -> 订阅 ($plan);
$workspace -> hasFeature 'reports' (英文): // is the feature available at all?
$workspace -> canConsume 'reports' , 10 (英文): // are 10 units available right now?
$workspace -> consume 'reports' , 10 (英文): // record usage, throws if the quota is exceeded
$workspace -> tryConsume 'reports' , 10 (英文): // same, but returns false instead of throwing
$workspace -> remainingUsage 'reports' (英文): // units left in the current period

为了 Limit features, release() hands units back to free a slot, which is what separates a limit from a consumable that only counts down:

$workspace -> consume “项目” , 1 (英文): // creating a project
$workspace -> 发布 “项目” , 1 (英文): // deleting it frees the slot

Direct grants independent of the plan

Plans aren't the only way to allocate features. grantFeature() assigns a feature directly to a subscribable and grants it on top of whatever the plan provides. If the plan includes 500 reports and you grant another 1,000, the available quota becomes 1,500. This covers one-off top-ups, promotional bonuses, and per-customer adjustments without creating a bespoke plan for each case:

$workspace -> grantFeature 'reports' , 价值 : 1_000 (英文):
// With auto-reset on its own schedule
使用 Revoltify\Subscriptionify\Enums\Interval ;
$workspace -> grantFeature 'reports' , 价值 : 100 , resetPeriod : 1 , resetInterval : Interval :: (英文):
// Remove the grant; the plan's allocation still applies
$workspace -> revokeFeature 'reports' (英文):

Optional overage and metered billing

Billing is opt-in. Implement the HasFunds contract alongside Subscribable , and the package begins charging against a balance you control. With HasFunds in place, consumable and limit features charge overage once their quota is exhausted (provided a unit_price is set), and metered features charge per unit from the first use:

使用 Revoltify\Subscriptionify\Contracts\HasFunds ;
班级 工作区 延伸 模型 实现 Subscribable , HasFunds
{
使用 InteractsWithSubscriptions ;
民众 功能 获取余额 () : 细绳
{
返回 $this -> balance;
}
民众 功能 hasSufficientFunds 细绳 金额) : 布尔值
{
返回 bccomp $this -> balance, $amount, 8 >= 0 ;
}
民众 功能 deductFunds 细绳 金额, 细绳 $description) : 空白
{
$this -> 更新 ([ '平衡' => bcsub $this -> balance, $amount, 8 )]);
}
}

Because amounts are passed as strings and compared with bccomp , the math is performed in arbitrary-precision rather than floating-point. Without HasFunds , the same features fall back to hard limits that throw when exceeded, so you can ship quota enforcement first and add billing later without rewriting your consumption code.

Gating access with middleware and Blade

For protecting routes, three middleware aliases are registered and return a 403 on failure:

路线 :: 中间件 'subscribed' -> 团体 功能 (){
// any active or trialling subscription
});
路线 :: 中间件 'plan:pro' -> 团体 功能 (){
// a specific plan
});
路线 :: 中间件 'feature:reports' -> 团体 功能 (){
// a specific feature
});

The matching Blade directives gate content in views, including states for trials, free plans, and the post-cancellation grace period:

@feature 'custom-branding'
{{-- shown only when the feature is available --}}
@endfeature
@onTrial
{{-- trial countdown banner --}}
@endonTrial

By default, both resolve the subscribable from auth()->user() , which you can change with Subscriptionify::resolveSubscribableUsing() if your billable model isn't the authenticated user.

Lifecycle, events, and scheduled expiry

Subscriptions carry the usual lifecycle methods ( changePlan() , renew() , cancel() , cancelNow() , 和 resume() during the grace period), and each transition dispatches an event such as SubscriptionCreated , SubscriptionCancelled , 或者 FeatureConsumed for your own listeners to act on. An included artisan command transitions active subscriptions past their end date to expired:

使用 照明\支持\立面\日程安排 ;
日程 :: 命令 'subscriptionify:expire-overdue' -> 每小时 ();

To read the full feature list, configuration options, and customisation hooks, visit the package on GitHub

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

访问 Laravel Cloud
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
绝不妥协标志

绝不妥协

来自 No Compromises 播客的两位经验丰富的开发者 Joel 和 Aaron 现在可以为您的 Laravel 项目提供服务。⬧ 固定费用 9500 美元/月。⬧ 无冗长的销售流程。⬧ 无需签订合同。⬧ 100% 退款保证。

绝不妥协
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
Toolkit: Reusable AI Tools for the Laravel AI SDK image

Toolkit: Reusable AI Tools for the Laravel AI SDK

阅读文章
Laracon US 2026 Reveals Its Full Speaker Lineup image

Laracon US 2026 Reveals Its Full Speaker Lineup

阅读文章
The State of PHP 2026 Survey Is Now Open image

The State of PHP 2026 Survey Is Now Open

阅读文章
Version-Controlled Documentation Inside Your Laravel App with Laradocs image

Version-Controlled Documentation Inside Your Laravel App with Laradocs

阅读文章
Typed Translation Accessors in Laravel 13.15.0 image

Typed Translation Accessors in Laravel 13.15.0

阅读文章
Refresh Your Laravel Database Without Dropping Every Table image

Refresh Your Laravel Database Without Dropping Every Table

阅读文章