我是新手,正在使用Cashier开发Web应用程序。在我的应用程序中,用户创建他们的帐户和公司,并允许他们使用该应用程序。因为一个公司可以有很多用户,所以我需要Cashier检查公司是否有订阅。
在Cashier文档中使用Stripe,我已经设置了前期不需要信用卡,他们可以使用系统14天,直到被提示输入信用卡。
到目前为止,我已经成功地在我的公司表上创建了Cashier列,并根据文档添加了订阅表。
add_cashier_table_fields.php迁移文件:
现在在我的RegisterController.php文件中,当一个公司被创建时,它会将日期从那天起推迟14天,并添加到'trial_ends_at'列中。 Auth/RegisterController.php
我正在尝试检查当前订阅是否处于试用期内,使用的方法是
我觉得既然我需要限制整个系统的访问(除了注册页面),我应该使用一个中间件来检查订阅状态。因此,我创建了Subscription.php中间件,其中包含以下内容:
问题:如何将收银员附加到公司(而不是每个用户),并在订阅未激活时限制对系统的访问?当我使用
我唯一能看到与其他帖子不同的是我的Company模型扩展了Model而不是Authenticatable。我验证了Subscription是否添加到了我的kernel.php文件中,并且中间件在我的路由文件中注册。
在Cashier文档中使用Stripe,我已经设置了前期不需要信用卡,他们可以使用系统14天,直到被提示输入信用卡。
到目前为止,我已经成功地在我的公司表上创建了Cashier列,并根据文档添加了订阅表。
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'列中。 Auth/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())
时,它总是打印false?我确保日期是今年早些时候的,所以我应该已经过了试用时间,但无论我是否在试用时间范围内,它总是打印false。这是我尝试做的最好方法吗?很抱歉有这么多代码,我想给每个人整个图片,因为关于这个问题的信息很少。我唯一能看到与其他帖子不同的是我的Company模型扩展了Model而不是Authenticatable。我验证了Subscription是否添加到了我的kernel.php文件中,并且中间件在我的路由文件中注册。