I am new to laravel and working with the cashier for the web application that I am developing. In my application, the user creates their account and company, and they are allowed to use the application. Since a company can have many users, I need a cashier to check if the company has a subscription or not.
In cashier documents using Stripe, I installed it where you do not need a credit card, and they can use the system for 14 days until you are asked to provide a credit card.
So far, I have successfully created the cashier columns in my companies table and added the subsctiption table according to the docs.
migration file 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() { // } }
Then in my company model, I added the Billable property, as suggested. .Php company - 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 this where, when the company is created, where it will be carbon date 14 days from this day and add Auth / RegisterController.php to the column 'trial_ends_at'
<?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 { use RegistersUsers; protected $redirectTo = '/home'; public function __construct() { $this->middleware('guest'); } 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', ]); } protected function create(array $data) { $company = \App\Company::create([ 'company_name'=> $data['company_name'], 'trial_ends_at' => Carbon::now()->addDays(14),
I am trying to check if the current subscription is for a trial period or not used
if ($company->onTrial()) {}
I believe that I need to restrict access to the entire system (in addition to the registration pages), I should use middleware to check my subscription status. So I created the 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 { public function handle($request, Closure $next) { if (Auth::check()){
Question: What is the best way to link the cashier to the company (instead of the user) and restrict access to the system if the subscription is not active? When I var_dump($company->onTrial()) , what does it always print false? I made sure that the date was at the beginning of this year, so I have to go through the trial time, but regardless of whether I am in the trial timeframe or not, it always prints false. Is this the best approach to what I'm trying to do? Apologizing for the whole code, I wanted to give everyone the whole picture, because there is little information on this on the Internet.
The only thing I can distinguish from other posts about this topic is my company model, which extends the model, not Authenticatable. I confirmed that the subscription was added to my kernel.php file, and the middleware is registered in the routes file.