dtv8189 2016-12-13 20:59
浏览 41

公司表上的Laravel 5和Cashier

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.

  • 写回答

1条回答 默认 最新

  • drqyxkzbs21968684 2016-12-14 17:14
    关注

    Turns out this is working. When I change my dates manually in the database to be outside of my trial it returns false, likewise if I am in my 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 app redirects them to that order page until they enter their card info. Im posting my final middleware here in case anyone in the future has a similar situation and needs the 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);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题