所以,我在Youtube上关于如何设置Stripe订阅的教程。我已正确插入钥匙,并且注册表格等都已设置完毕。唯一的问题是,我注意到如果用户不付费并返回主页,他们将能够看到所有内容。在他们完成付款之前拒绝他们访问的最佳方法是什么?
目前我的用户控制器就是这个
class SubscribersController < ApplicationController
before_filter :authenticate_user!
def new
end
def update
token = params[:stripeToken]
customer = Stripe::Customer.create(
card: token,
plan: 1020,
email: current_user.email
)
@user = User.find(current_user.id)
@user.subscribed = true
@user.stripeid = customer.id
@user.save
redirect_to people_path, notice: "Welcome"
end
end
注册管理员:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/subscribers/new'
end
end
答案 0 :(得分:1)
如果您不希望非付费客户能够访问该应用程序,则需要创建额外的before_filter方法。这可能需要您在用户表上创建一个额外的布尔字段,例如&#34;付费&#34;并将其用作过滤器。实施例...
class SubscribersController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_payment
def new
end
def update
token = params[:stripeToken]
customer = Stripe::Customer.create(
card: token,
plan: 1020,
email: current_user.email
)
@user = User.find(current_user.id)
@user.subscribed = true
@user.stripeid = customer.id
@user.save
redirect_to people_path, notice: "Welcome"
end
private
def authenticate_payment
@user = User.find(current_user.id)
unless @user.paid?
redirect_to root_url
end
end
编辑:如果您已经为该用户设置了订阅字段,则在确认付款时会将其标记为true,那么此类过滤器应该可以使用
def authenticate_subscription
@user = User.find(current_user)
unless @user.subscribed?
redirect_to root_url
end
end