Tables Laravel 5 and Cashier on Company

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 { /* |-------------------------------------------------------------------------- | 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 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 { /** * 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 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.

+5
source share
1 answer

Turns out it works. When I manually change my dates in the database to be outside my trial, it returns false, just as if I'm in a trial period, it returns true. In my case, I needed to check onTrial() , and also if the current url was localhost: 8000 / order - if not, then the application redirects them to this order page until they enter their card information. I will post my latest middleware here if someone in the future has a similar situation and needs a functioning code. (Still don't know if this is the best approach, but it works)

 <?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); } } 
+2
source

Source: https://habr.com/ru/post/1261222/


All Articles