我是 laravel 的新手,一直在与 cashier 合作开发我正在开发的网络应用程序。在我的应用程序中,用户创建了他们的帐户和公司,并允许他们使用该应用程序。因为一个公司可以有很多用户,我需要收银员检查公司是否有订阅。
在cashier docs使用 Stripe,我已经将其设置为预先不需要信用卡,他们可以使用该系统 14 天,直到系统提示您提供信用卡。
到目前为止,我已经成功地在我的公司表上创建了出纳员列,并根据文档添加了订阅表。
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()
{
//
}
}
然后在我的公司模型中,我按照建议添加了 Billable 特征。 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 时?我确定日期是今年早些时候的,所以我应该已经过了试用时间,但无论我是否在试用时间范围内,它总是打印错误。这是我想要做的最好的方法吗?抱歉所有代码,我想给大家完整的图片,因为网上关于这个的信息很少。
我唯一能看到的与other posts about this topic 不同的是是我的公司模型扩展模型而不是可验证的。我已验证订阅已添加到我的 kernel.php 文件中,并且中间件已在我的路由文件中注册。
最佳答案
事实证明这是有效的。当我在数据库中手动更改我的日期以使其不在我的试用期时,它会返回 false,同样,如果我在试用期,它会返回 true。在我的例子中,我需要检查 onTrial() 以及当前 url 是否为 localhost:8000/order - 如果不是,则应用程序将他们重定向到该订单页面,直到他们输入他们的卡信息。我在这里发布我的最终中间件,以防将来有人遇到类似情况并需要功能代码。 (仍然不知道这是否是最好的方法,但它有效)
<?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);
}
}
关于php - 公司 table 上的 Laravel 5 和 Cashier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130446/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested
我有一个.pfx格式的证书,我需要使用ruby提取公共(public)、私有(private)和CA证书。使用shell我可以这样做:#ExtractPublicKey(askforpassword)opensslpkcs12-infile.pfx-outfile_public.pem-clcerts-nokeys#ExtractCertificateAuthorityKey(askforpassword)opensslpkcs12-infile.pfx-outfile_ca.pem-cacerts-nokeys#ExtractPrivateKey(askforpassword)o
我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到
我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/
我正在尝试将cucumber项目的用户名和密码置于版本控制之外。有没有办法在命令行上手动将用户名和密码等变量传递给Cucumber脚本?我的备份计划是将它们放在一个YML文件中,然后将该文件添加到gitignore,这样它们就不会被置于版本控制中。 最佳答案 所以,我看到了您对铁皮人的评论,答案是肯定的。cucumberPASSWORD=my_passwordPASSWORD被设置为环境变量,您可以通过将其引用为ENV['PASSWORD']来使用它的值。例如,browser.text_field(:id=>'pwd').setEN
我刚刚迈出了编程的第一步。我刚刚完成了CodeAcademy的另一门类(class)。这次我被要求创建一个小电影目录。这是我的问题:如何在文件中保存/加载带有电影标题和评级的哈希值而不是自己的代码?下面是代码现在的样子(几句葡萄牙语,但您可以忽略它:movies={Memento:3,Primer:4,Ishtar:1}puts"Oquevocêgostariadefazer?"puts"--Digite'add'paraadicionarumfilme."puts"--Digite'update'paraatualizarumfilme."puts"--Digite'display'
我是Ruby新手,并被要求在我们的新项目中使用它。我们还被要求使用Padrino(Sinatra)作为后端/框架。我们被要求使用Rspec进行测试。我一直在寻找可以指导在Padrino上使用RspecforRuby的教程。我得到的主要是引用RoR。但是,我需要RubyonPadrino。请在任何入门/指南/引用/讨论等方面指导我。如有不妥之处请指正。可能是我没有针对我的问题搜索正确的词/短语组合。我正在使用Ruby1.9.3和Padrinov.0.10.6。注意:我还提到了SOquestion,但它没有帮助。 最佳答案 我没用过Pa