codeforest/plan-config

计划配置允许您轻松定义您的 SaaS 应用订阅计划属性和限制。

1.2.0 2016-05-23 18:01 UTC

This package is auto-updated.

Last update: 2024-09-17 18:57:08 UTC


README

这个 Laravel 5 包使您轻松管理 SaaS 应用订阅计划的规则/限制。

如何安装

通过 Composer 拉取此包。

"require": {
    "seanstewart/plan-config": "dev-master"
}

app/config/app.php 中包含服务提供者。

'providers' => [
    Seanstewart\PlanConfig\PlanConfigServiceProvider::class
];

app/config/app.php 中包含外观(可选)。

'aliases' => [
    'Plan'       => Seanstewart\PlanConfig\Plan::class
];

然后您需要通过运行命令来生成配置

php artisan vendor:publish

如何使用

假设您的应用程序具有限制用户添加小部件数量的订阅计划。您将有一些逻辑来检查用户在他们的账户中允许拥有的小部件数量。使用 Plan Config,您可以通过调用 helper 函数 plan() 来做到这一点。

if($this->getCurrentNumberOfWidgets < plan('limits.widgets'))
{
    // Allow the user to add a new widget
}

plan() 辅助函数知道当前用户订阅了哪个计划,并从您的 plans.php 配置文件中获取您定义的限制。您可以在应用程序的任何地方使用辅助函数(视图、控制器、模型、中间件等)。使用前面的示例,您的计划配置文件将如下所示

'plans' => [

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

如果您的用户订阅了银色计划,他们只能添加 10 个小部件。您甚至可以将其修改为使用其他属性,例如计划的标题、描述或定价。

'plans' => [

    'bronze' => [
        'title'       => 'Bronze Plan',
        'description' => 'This is some description for a Bronze Plan',
        'price'       => '19.00',
        'currency'    => 'USD',
        'limits'      => [
            'widgets' => 5
        ]
    ],

    'silver' => [
        'title'       => 'Silver Plan',
        'description' => 'This is some description for the Silver Plan',
        'price'       => '29.00',
        'currency'    => 'USD',
        'limits'      => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

配置您的计划

要配置您的计划,请打开 app/plans.php 并开始添加计划详细信息。默认情况下,该包假定您正在使用 Laravel 内置的 Auth,并且用户的计划存储在 User 模型中。您可以在配置中设置用于确定用户计划的字段...

'plan_field' => 'stripe_plan'

要配置您的计划,请在 'plans' 数组中添加您的计划数据。

'plans' => [

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

如果您有适用于所有计划的规则,您可以定义默认或后备计划。在配置文件中,设置您的后备计划...

'fallback_plan' => '_default',

然后在您的计划数组中定义 _default 计划。

'plans' => [

    '_default' => [
        'limits' => [
            'purple_widgets' => 20
        ]
    ]

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

]

在上面的示例中,调用 plan('limits.purple_widgets') 将返回后备计划中的值。

或者您可以使用外观并调用 Plan::get('limits.purple_widgets')

我为什么创建这个

我一直觉得管理 SaaS 应用程序的订阅和计划可能很复杂。我觉得将这些值存储在数据库中并不是最好的方法,因为您的大部分值和限制不会经常更改。当构建 Election Runner 时,这是一个允许学校和机构运行选举的 Web 应用程序,我们需要完成这项任务。希望其他人会发现这和我们一样有用!