【问题标题】:How to implement multiple plan in rails with stripe checkout如何使用条带结帐在 Rails 中实施多个计划
【发布时间】:2021-05-04 13:23:02
【问题描述】:

我正在尝试在带有条纹的 Rails 应用上实现多个计划订阅。

我的订阅控制器

def new
    session = Stripe::Checkout::Session.create(
      payment_method_types: ['card'],
      client_reference_id: current_user.id,
      customer_email: current_user.email,
      subscription_data: {
        items: [{
          plan: 'beginner'
        }]
      },
      success_url: 'http://localhost:3000/',
      cancel_url: 'http://localhost:3000/'
    )
    @session_id = session.id
  end

我的路线

....
resources :subscriptions, only: :new
....

我的订阅按钮是这样的

<%= link_to 'Subscribe to Beginner', new_subscription_path,  %>
<%= link_to 'Subscribe to Pro', #TODO,  %>

通过此设置,我可以毫无问题地订阅初学者计划。

我的问题是如何在此设置中添加专业计划。路线会是什么样子?

我在我的条带仪表板中创建了所有计划。 我检查了文档,但我不清楚。

我看到你可以像这样在链接中传递参数

<%= link_to "subscribe", some_path(:params[:value]) %>

我怎样才能实现那种 URL?

毕竟,我想在用户模型上有一个方法来检查用户是否订阅了初学者或专业

谢谢

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    您似乎在询问“查询参数”。

    您可以在 URL 中添加参数

    <%= link_to 'Subscribe to Beginner', new_subscription_path(plan: :beginner),  %>
    <%= link_to 'Subscribe to Pro', new_subscription_path(plan: :pro),  %>
    

    new_subscription_path(plan: :beginner) 将评估为 subscription/new?plan=beginner

    然后你可以通过参数在模型中访问它。

      def new
        plan_type = params[:plan]
    
        session = Stripe::Checkout::Session.create(
          payment_method_types: ['card'],
          client_reference_id: current_user.id,
          customer_email: current_user.email,
          subscription_data: {
            items: [{
              plan: plan_type
            }]
          },
          success_url: 'http://localhost:3000/',
          cancel_url: 'http://localhost:3000/'
        )
        @session_id = session.id
      end
    

    【讨论】:

    • 非常感谢您的回答。我的下一个挑战是在用户模型上创建一个可能的方法,以根据用户的订阅类型来确定用户范围。我正在考虑类似def pro_user and user_starter
    猜你喜欢
    • 2016-04-06
    • 2019-10-24
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2011-01-31
    • 2013-12-01
    相关资源
    最近更新 更多