公司表上的Laravel 5和Cashier

时间:2016-12-13 20:59:52

标签: php mysql laravel-5 stripe-payments laravel-cashier

我是laravel的新手,并且正在与收银员合作开发我正在开发的网络应用程序。在我的应用程序中,用户创建他们的帐户和公司,并允许他们使用该应用程序。因为公司可以有很多用户,所以我需要收银员来检查公司是否有订阅。

在使用Stripe的cashier docs中,我已将其设置在前面不需要信用卡的地方,他们可以使用系统14天,直到被提示输入信用卡。

到目前为止,我已经在公司表上成功创建了收银台列,并根据文档添加了子列表。
add_cashier_table_fields.php迁移文件:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCashierTableFields extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::table('companies', function ($table) {
            $table->string('stripe_id')->nullable();
            $table->string('card_brand')->nullable();
            $table->string('card_last_four')->nullable();
            $table->timestamp('trial_ends_at')->nullable();
        });

        Schema::create('subscriptions', function ($table) {
            $table->increments('id');
            $table->integer('company_id');
            $table->string('name');
            $table->string('stripe_id');
            $table->string('stripe_plan');
            $table->integer('quantity');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

然后在我的公司模型中,我按照建议添加了Billable特征。 Company.php - 模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\Billable;

class Company extends Model
{
    use Billable;
    protected $dates = [
        'trial_ends_at', 
        'subscription_ends_at'
    ];

    protected $fillable = [
        'company_name',
        'trial_ends_at', 
        'subscription_ends_at'
    ];

    protected $cardUpFront = false;

    public function users()
    {
        return $this->hasMany(\App\User::class);
    }
}

现在在我的RegisterController.php文件中,我知道在创建公司的地方,它将从那天开始的14天后将碳添加到'trial_ends_at'列 的认证/ RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Company;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Carbon\Carbon;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'company_name' => 'required|unique:companies,company_name',
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $company = \App\Company::create([
            'company_name'=> $data['company_name'],
            'trial_ends_at' => Carbon::now()->addDays(14), //Collect CC# 14 days from now
        ]);

        $user = $company->users()->create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        $user->attachRole(1); //Admin role

        return $user;

    }
}

我正在尝试使用

检查当前订阅是否在试用期内
if ($company->onTrial()) {}

我认为,因为我需要限制对整个系统的访问(除了注册页面),我应该使用中间件来检查订阅状态。所以我用以下内容创建了Subscription.php中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use App\User;
use App\Company;
use Illuminate\Support\Facades\Auth;

class Subscription
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check()){
            //dd($request->user);
;            $companyID = Auth::user()->company_id;
            $company = Company::find($companyID);
            dd($company->onTrial());
            if($company->onTrial()){
                return redirect('order');
            }
        }
        return $next($request);
    }
}

问题:如果订阅不活跃,将收银员连接到公司(而不是每个用户)并限制对系统的访问权限的最佳方法是什么? 当我var_dump($company->onTrial())它总是打印错误?我确定这个日期是从今年早些时候开始的,所以我应该超过审判时间,但无论我是否处于试用期限内,它总是打印错误。这是我尝试做的最好的方法吗?对所有代码表示道歉,我想向大家展示全部图片,因为网上的信息很少。

我唯一可以看到与other posts about this topic不同的是我的公司模型扩展了Model而不是Authenticatable。我验证了Subscription已添加到我的kernel.php文件中,中间件在我的路由文件中注册。

1 个答案:

答案 0 :(得分:3)

原来这是有效的。当我在数据库中手动更改我的日期在我的审判之外它返回false,同样如果我在我的试用期间它返回true。在我的情况下,我需要检查onTrial()以及当前网址是否为localhost:8000 / order - 如果没有,则应用程序会将其重定向到该订单页面,直到他们输入卡片信息为止。我在这里发布我的最终中间件,以防将来有人遇到类似情况并需要正常运行的代码。 (仍然不知道这是否是最好的方法,但它有效)

<?php

namespace App\Http\Middleware;

use Closure;
use App\User;
use App\Company;
use Illuminate\Support\Facades\Auth;

class Subscription
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check()){
            //dd($request->user);
            $companyID = Auth::user()->company_id;
            $company = Company::find($companyID);
            //dd($company->onTrial());
            if(!$company->onTrial() && $request->path() != 'order'){ //If trial has expired redirect to order page
                return redirect('order');
            }
        }
        return $next($request);
    }
}