smartech/subscriptions

管理Laravel/Lumen应用的用户订阅

0.1.4 2023-04-19 02:47 UTC

This package is auto-updated.

Last update: 2024-09-19 06:03:47 UTC


README

管理Laravel/Lumen应用的用户订阅


需求

  • php >= 5.6.6
  • laravel/laravel >= 5.* 或 laravel/lumen >= 5.*

安装

Laravel安装

1) 使用composer安装包。
$ composer require smartech/subscriptions
2) 添加包服务提供者(< laravel 5.5)。
Smartech\Subscriptions\SubscriptionServiceProvider::class
3) 发布配置文件(可选)。
$ php artisan vendor:publish --provider="Smartech\Subscriptions\SubscriptionServiceProvider"
4) 迁移表。
$ php artisan subscriptions:migrate

Lumen安装

1) 使用composer安装包。
$ composer require Smartech\Subscriptions
2) 在 bootstrap/app.php 中添加包服务提供者。
$app->register(Smartech\Subscriptions\SubscriptionServiceProvider::class);
3) 发布配置文件(可选)
  • 复制配置文件:vendor/smartech/subscriptions/config/subscriptions.php 到应用配置目录。

  • 设置Lumen解析配置文件。

$app->configure('subscriptions');
4) 迁移表。
$ php artisan subscriptions:migrate

配置

使用 HasSubscription 特性与 User 模型结合使用。

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Smartech\Subscriptions\Traits\HasSubscription;

class User extends Authenticatable 
{
    use HasSubscription;

    //
}

用法

订阅计划

  • 要订阅计划,您必须指定定价或请求试用期(如果该计划支持请求试用期)。
  • 通过定义每个计划的周期及其价格来设置定价,每个计划可以有多个定价(每月、每年或其他)。
$user = auth("api")->user();

try {
    $user->setPlan("premium") // the plan slug
        ->setPricing(1) // the pricing ID
        ->subscribe(); // return bool

    // user subscribed successfully

}  catch (SubscriptionException $error) {

    // All available exceptions

    if ($error instanceof PlanNotFoundException) {
        return response()->json("Plan not found");
    }

    if ($error instanceof PricingNotFoundException) {
        return response()->json("Pricing not found");
    }

    if ($error instanceof SubscriptionExistException) {
        return response()->json("User has already subscribed to a plan");
    }
}

订阅带试用期的计划

  • 如果请求的计划支持请求试用期,以下代码将使用计划表中定义的每个计划的周期订阅用户。
  • 在此处不应调用 setPricing,因为试用期总是免费的。
$user = auth("api")->user();

try {

$user->setPlan("premium")
    ->setTrial()
    ->subscribe(); // return bool

    // user subscribed successfully

}  catch (SubscriptionException $error) {

    // All available exceptions

    if ($error instanceof PlanNotFoundException) {
        return response()->json("Plan not found");
    }

    if ($error instanceof TrialNotSupportedException) {
        return response()->json("Trial is not supported for this plan");
    }

    if ($error instanceof TrialExistException) {
        return response()->json("Trial period has already been requested before.");
    }
}

更改订阅计划

  • 更改计划将为用户设置一个新的计划,并根据定价ID设置一个新的周期(started_atended_at)。
try {
    $user = auth("api")->user();
    
    $user->setPlan("premium") // the new plan slug
        ->setPricing(2) // the pricing ID
        ->change();

}  catch (SubscriptionException $error) {

    if ($error instanceof PlanNotFoundException) {
        return response()->json("Plan not found");
    }

    if ($error instanceof PricingNotFoundException) {
        return response()->json("Pricing not found");
    }
}

续订订阅

  • 续订订阅将仅根据当前定价ID设置新的周期(started_atended_at)。
/*
* can_be_renewed will check if:
* - The subscription is ended.
* - The subscription is not trial.
* - The subscribed plan is not default.
*/
if($user->subscription->can_be_renewed) {
    $user->subscription->renew();
}

取消订阅

  • 取消订阅将设置 canceled_at 字段。
  • 默认情况下,订阅将仍然有效,直到当前周期的结束。
/*
* can_be_renewed will check if:
* - The subscription is not trial.
* - The subscribed plan is not default as it can not be canceled.
*/

if($user->subscription->can_be_canceled) {
    $user->subscription->cancel();
}
  • 如果我们想立即取消订阅,只需调用 $user->subscription->cancel(true)

激活/停用订阅

  • 激活/停用订阅将设置 is_active 字段。
$user->subscription->activate();
$user->subscription->deactivate();

模型

订阅模型

$user = auth("api")->user();

// Getting user subscription

$subscription = $user->subscription;

// Getting user subscription plan 

$plan = $user->subscription->plan;

// Getting the user subscription pricing

$pricing = $user->subscription->pricing;

// Check if the user subscription is trial

$subscription->is_trial; // return bool

// Check if the user subscription is activated

$subscription->is_active; // return bool

// Check if the user subscription is ended

$subscription->is_ended; // return bool

// Check if the user is valid (activated and not ended)

$subscription->is_valid; // return bool

// Check if the user has canceled his subscription

$subscription->is_canceled; // return bool

/// Fetch all trial subscriptions

$subscriptions = app("subscriptions.models.subscription")->trial()->get();

// Fetch all valid/invalid subscriptions
// The valid subscription means both (activated and the period is not ended)

$subscriptions = app("subscriptions.models.subscription")->valid()->get();
$subscriptions = app("subscriptions.models.subscription")->invalid()->get();

// Fetch only active/inactive subscriptions

$subscriptions = app("subscriptions.models.subscription")->active()->get();
$subscriptions = app("subscriptions.models.subscription")->inactive()->get();

// Fetch only current/expired subscriptions

$subscriptions = app("subscriptions.models.subscription")->inPeriod()->get();
$subscriptions = app("subscriptions.models.subscription")->notInPeriod()->get();
// 

计划模型

// Getting user subscription plan.

auth("api")->user()->subscription->plan

// Getting the default plan

$plan = app("subscriptions.models.plan")->default()->first();

// Query the plan model

$plan = app("subscriptions.models.plan")->where("slug", "premium")->first();

// check if there is a trial support for this plan

$plan->has_trial; // return bool


// check if this plan is the default plan

$plan->is_default; // return bool

// Getting all plan pricings
// the pricings determine what price to pay for a period of time (monthly, yearly or a custom period)

$plan->pricings;

// Getting all plan features

$plan->features;

// Getting all available translations of all languages

$plan->translations;

// Getting the current translation based on user language

$plan->translation;

用户模型

获取用户订阅

auth("api")->user()->subscription;

检查用户是否有特定计划

auth("api")->user()->hasPlan("premium"); // return bool

检查用户是否有特定功能

auth("api")->user()->hasFeature("ad_free"); // return bool

扩展模型

所有模型都已绑定到IOC并列在 config/subscription.php 中。

例如,我们想扩展 Plan 模型。

  1. 编辑 config/subscription.php 并将 models.plan 设置为新的计划模型类 \App\Models\Plan::class

  2. 定义新的类继承自父计划类

<?php

namespace App\Models;

class Plan extends \Smartech\Subscriptions\Models\Plan
{
    // we can override model attributes/methods or create new methods here.
}
  1. 现在您可以从 app("subscriptions.models.plan") 或从 \App\Models\Plan 调用它。