rockbuzz/lara-pricing
Composer 安装命令:
composer require rockbuzz/lara-pricing
包简介
Pricing management
README 文档
README
Laravel package to manage plan subscriptions with restrictive and limited functionalities.
Requirements
PHP >=7.3
Install
$ composer require rockbuzz/lara-pricing
$ php artisan vendor:publish --provider="Rockbuzz\LaraPricing\ServiceProvider" --tag="migrations"
$ php artisan migrate
Add the Subscribeable interface and trait to the template you will have plan susbcriptions.
use Rockbuzz\LaraPricing\Traits\Subscribable; use Rockbuzz\LaraPricing\Contracts\Subscribable as SubscribableContract; class Account extends Model implements SubscribableContract { use Subscribable; }
Usage
You can subscribe to a plan.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $account = Account::create(); $account->subscribe($plan);
when subscribing the event Rockbuzz\LaraPricing\Events\SubscriptionCreated is dispatched
You can unsubscribe the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $account = Account::create(); $account->subscribe($plan); $account->unsubscribe();
when unsubscribing the event Rockbuzz\LaraPricing\Events\SubscriptionCanceled is dispatched
You can take the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $account = Account::create(); $account->subscribe($plan); dd($account->currentSubscription()->toArray()); # output [ "id" => 1 "uuid" => "66d0ef96-961d-4b55-8328-115ec44e05f2" "start_at" => "2022-12-11T14:39:55.000000Z" "finish_at" => null "canceled_at" => null "due_day" => null "subscribable_id" => "1" "subscribable_type" => "Tests\Models\Account" "plan_id" => "1" "immutable_plan" => array:10 [ "name" => "Plan A" "price" => 2990 "interval" => "month" "period" => 1 "uuid" => "6e9a7100-c314-4653-a40f-24ec22aaaa84" "slug" => "plan-a" "updated_at" => "2022-12-11T14:39:55.000000Z" "created_at" => "2022-12-11T14:39:55.000000Z" "id" => 1 "features" => [] ] "created_at" => "2022-12-11T14:39:55.000000Z" "updated_at" => "2022-12-11T14:39:55.000000Z" "deleted_at" => null ]
You can subscribe to a plan with feature restriction.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; use Rockbuzz\LaraPricing\Enums\PlanFeatureValue; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan B', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([ $feature->id => ['value' => PlanFeatureValue::POSITIVE] ]);// Y, OK and TRUE values are accepted $account = Account::create(); $account->subscribe($plan); dd($account->currentSubscription()->toArray()); #output [ "id" => 1 "uuid" => "be571da1-f145-4acf-9494-ebcbc84b0a8f" "start_at" => "2022-12-11T15:00:54.000000Z" "finish_at" => null "canceled_at" => null "due_day" => null "subscribable_id" => "1" "subscribable_type" => "Tests\Models\Account" "plan_id" => "1" "immutable_plan" => array:10 [ "name" => "Plan B" "price" => 2990 "interval" => "month" "period" => 1 "uuid" => "c703d2fd-76b5-461f-92f2-05b3a2a4e3ab" "slug" => "plan-b" "updated_at" => "2022-12-11T15:00:54.000000Z" "created_at" => "2022-12-11T15:00:54.000000Z" "id" => 1 "features" => array:1 [ 0 => array:9 [ "id" => 1 "uuid" => "b1adf248-ae4b-464d-9667-462e51de6d2f" "name" => "aut" "slug" => "aut" "order_column" => "1" "created_at" => "2022-12-11T15:00:54.000000Z" "updated_at" => "2022-12-11T15:00:54.000000Z" "deleted_at" => null "pivot" => array:3 [ "plan_id" => "1" "feature_id" => "1" "value" => "Y" ] ] ] ] "created_at" => "2022-12-11T15:00:54.000000Z" "updated_at" => "2022-12-11T15:00:54.000000Z" "deleted_at" => null ]
You can check if a feature is available in the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => PlanFeatureValue::POSITIVE]]); $account = Account::create(); $account->subscribe($plan); dd($account->featureEnabled($feature->slug)); #output true
You can subscribe to a plan with feature limit.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); dd($account->currentSubscription()->toArray()); #output [ "id" => 1 "uuid" => "e39d1ce4-08c7-422a-b940-b7575aa613e2" "start_at" => "2022-12-11T15:08:38.000000Z" "finish_at" => null "canceled_at" => null "due_day" => null "subscribable_id" => "1" "subscribable_type" => "Tests\Models\Account" "plan_id" => "1" "immutable_plan" => array:10 [ "name" => "Plan B" "price" => 2990 "interval" => "month" "period" => 1 "uuid" => "14fdab88-a6d5-4d4d-b2c8-ccb3786778e6" "slug" => "plan-b" "updated_at" => "2022-12-11T15:08:38.000000Z" "created_at" => "2022-12-11T15:08:38.000000Z" "id" => 1 "features" => array:1 [ 0 => array:9 [ "id" => 1 "uuid" => "f0935f56-2dd8-49bc-b2e2-52bc5dcf15f0" "name" => "eos" "slug" => "eos" "order_column" => "1" "created_at" => "2022-12-11T15:08:38.000000Z" "updated_at" => "2022-12-11T15:08:38.000000Z" "deleted_at" => null "pivot" => array:3 [ "plan_id" => "1" "feature_id" => "1" "value" => "10" ] ] ] ] "created_at" => "2022-12-11T15:08:38.000000Z" "updated_at" => "2022-12-11T15:08:38.000000Z" "deleted_at" => null ]
You can get the usage amount of a feature in the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); dd($account->featureEnabled($feature->slug)); #output 10
You can check the usage amount of a feature in the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); dd($account->consumedUse($feature->slug)); #output 0
You can check the rest of usage of a feature of the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); dd($account->remainingUse($feature->slug)); #output 10
You can increment the usage of a functionality in the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); $account->incrementUse($feature->slug);
Optionally you can pass a second parameter with integer value to increment, default is 1.
$account->incrementUse($feature->slug, 2);
You can decrement the usage of a functionality in the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); $account->decrementUse($feature->slug);
Optionally you can pass a second parameter with integer value to decrement, default is 1.
$account->decrementUse($feature->slug, 2);
You can check if you can use a feature of the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); dd($account->canUse($feature->slug)); #output true
You can clear the usages of a functionality from the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $feature = Feature::create(['name' => 'Feature Name']); $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $plan->features()->attach([$feature->id => ['value' => '10']]); $account = Account::create(); $account->subscribe($plan); $account->cleanUse($feature->slug);
You can manage recurrences of the current subscription.
use Tests\Models\Account; use Rockbuzz\LaraPricing\Models\Plan; $plan = Plan::create([ 'name' => 'Plan Name', 'price' => 2990,// in cents 'interval' => 'month', 'period' => 1 ]); $account = Account::create(); # by default when signing the recurrence is on $account->subscribe($plan); dump($account->isRecurrent()); # output true $account->cancelRecurrence(); dd($account->isRecurrent()); # output false
When canceling a recurrence event Rockbuzz\LaraPricing\Events\SubscriptionCancelRecurrence is dispatched.
License
The Lara Pricing is open-sourced software licensed under the MIT license.
rockbuzz/lara-pricing 适用场景与选型建议
rockbuzz/lara-pricing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 220 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pricing」 「features」 「plans」 「subscriptions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rockbuzz/lara-pricing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rockbuzz/lara-pricing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rockbuzz/lara-pricing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS 3.x
Adds some direct links and composer auto-fill capabilities.
Application helper classes and methods.
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Manage acquisition, selling & renting prices for products and services in your Laravel Application
Provides basic functionality of a common application
统计信息
- 总下载量: 220
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-02