【问题标题】:Denying user permission that haven't completed the checkout process with stripe subscription通过条带订阅拒绝尚未完成结帐过程的用户权限
【发布时间】:2015-11-13 04:05:30
【问题描述】:

所以,我按照 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

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    如果您不希望非付费客户能够访问应用程序,则需要创建一个额外的 before_filter 方法。这可能需要您在用户表上创建一个额外的布尔字段,例如“付费”并将其用作您的过滤器。示例...

    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
    

    【讨论】:

    • 是的,我已经有了,它是订阅的布尔值。我会尝试 before_filter :authenticate_subscribed ?
    • 不起作用,我遇到了一个错误 :( 我有这个,类 AddSubscriberAndStripeIdToUsers
    • 我试图把 before_filter :authenticate_subscribed 说成无效的方法。尝试了很多其他的,比如@user.subscribed?不起作用。不确定使用哪种方法来确定它是否正确。
    • 设法找出我的出路!感谢您的提示帮助很大,现在唯一的问题是,如果用户未订阅,则会陷入循环和错误页面。
    • 你能发布完整的错误信息吗?如果 subscribed 是用户上的布尔列,并且您的 current_user 方法正在查找用户对象,它应该可以工作吗?另外需要注意的是,你应该只把这个 before_filter 放在你想要阻止非订阅用户查看的控制器上。
    猜你喜欢
    • 2014-10-19
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2021-09-29
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多