I am new to laravel and have been working with cashier for a web app I am developing. In my app a user creates their account and company and they are allowed to use the app. Because a company can have many users, I need cashier to check if the company has a subscription or not.
In the cashier docs using Stripe I have set it up where no credit card is required up front and they can use the system for 14 days until being prompted for a credit card.
So far I have successfully created the cashier columns on my companies table and added the subsctiption table according to the docs.
add_cashier_table_fields.php migration file:
<?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()
{
//
}
}
Then in my company model I added the Billable trait as suggested. Company.php - model
<?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);
}
}
Now in my RegisterController.php file I have it where when a company is created it where it will Carbon the date 14 days from that day and add to 'trial_ends_at'
column
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;
}
}
I am trying to check if the current subscription is within its trial period or not using
if ($company->onTrial()) {}
I figure since I need to restrict access to the whole system (besides the registration pages) I should use a middleware to check for subscription status. So I created Subscription.php middleware with the following:
<?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);
}
}
Question: What is the best way to attach cashier to a company (instead of per user) and restrict access to the system if the subscription is not active?
When I var_dump($company->onTrial())
that it always prints false? I made sure the date was from earlier this year so I should be past the trial time but no matter if I am in the trial timeframe or not it always prints false. Is this the best approach for what I am trying to do? Apologies for all the code, I wanted to give everyone the whole picture because there is little information about this online.
The only thing I can see different from other posts about this topic is my Company model extends Model and not Authenticatable. I verified Subscription was added to my kernel.php file and the middleware is register in my routes file.