2016-12-13 85 views
4

我是laravel的新手,並且一直在爲我正在開發的網絡應用收銀員工作。在我的應用程序中,用戶創建他們的帳戶和公司,並且他們被允許使用該應用程序。因爲公司可以有很多用戶,所以我需要收銀員來檢查公司是否有訂閱。Laravel 5和公司的收銀臺

在使用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() 
    { 
     // 
    } 
} 

然後在我的公司模式,我添加了結算特點的建議。 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不同的唯一的事情是我的公司模型擴展了模型而不是Authenticatable。我驗證了Subscription是添加到我的kernel.php文件中,中間件是註冊在我的路由文件中。

+0

我不知道anwser,但感謝寫清楚詳細的問題! –

回答

1

原來這是行得通的。當我在數據庫中手動更改我的日期以使其不在我的審判範圍內時,它將返回false,同樣,如果我處於試用期,它將返回true。在我的情況下,我需要檢查onTrial(),並且如果當前URL是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); 
    } 
}