【问题标题】:How can I create a premium user to test premium features of my app in production when using Stripe in test mode?在测试模式下使用 Stripe 时,如何创建高级用户来测试我的应用的高级功能?
【发布时间】:2014-11-19 16:46:22
【问题描述】:

我正在开发一个应用程序,该应用程序允许会员升级为高级会员(在使用 Stripe 付款后)以访问其他功能。我仅在生产中以测试模式使用 Stripe,因此我正在寻找一种在 Heroku 控制台中创建高级用户的方法。会是这样吗?:

user = User.find_by(name:’existing user name’)
user.update_attribute(‘premium’).save

我的 schema.rb 文件中有 t.boolean "premium"

我是编程新手,所以如果您需要任何其他文件信息,请告诉我。谢谢!

编辑更新:这是我的收费控制器代码:

class ChargesController < ApplicationController

  def new
    @stripe_btn_data = {
      key: "#{ Rails.configuration.stripe[:publishable_key] }",
      description: 'Premium Membership',
      amount: 1_299

    }
   end


   def create

   @amount = params[:amount]


   customer = Stripe::Customer.create(
     email: current_user.email,
     card: params[:stripeToken]
   )

   charge = Stripe::Charge.create(
     customer: customer.id, 
     amount: @amount,
     description: 'Premium Membership',
     currency: 'usd'
   )

   current_user.update_attribute(:premium, true)

   redirect_to wikis_path, flash: { notice: "Congratulations, #{current_user.email}, on becoming a premium member!"}


 rescue Stripe::CardError => e
   flash[:error] = e.message
   redirect_to new_charge_path
 end
end

【问题讨论】:

    标签: ruby-on-rails heroku stripe-payments


    【解决方案1】:

    我假设在你的控制器中你调用了一个方法来创建一个新的 Stripe 费用,像这样:

    @user.update_with_payment
    

    在用户模型的该方法中,您应该调用 Stripe API 以根据用户的条带令牌向用户收费。您可以做的是对该方法设置一个条件,以便如果收费成功,您可以更新用户以使其高级属性为真。如果收费不成功,您将重新提交付款表格并显示任何适用的错误。

    if @user.update_with_payment
      @user.update_attribute(:premium, true)
      # You can than redirect wherever you want
      redirect_to @user
    else
      render :new
    end
    

    以这种方式设置您的控制器将使处于 Stripe 测试模式的用户能够接收其对象的高级属性,因为测试模式将像实时模式一样运行此方法。在为 Stripe 设置方法和逻辑时,将所有逻辑放入 Rails 应用程序中,测试模式将按照您期望的实时模式工作。不要在控制台中更新用户对象。

    【讨论】:

    • 我理解这个概念,但我的费用控制器或代码中的其他任何地方都没有@user.update_with_payment。既然我已经发布了我的收费控制器,您还有其他建议吗?谢谢!
    • 如果您想在不更改代码的情况下显式更新数据库中的该用户,那么您可以执行顶部fourfourjew 建议的操作——获取一个用户 (user = User.find(1)),然后使用 AR update_attribute 方法,它接受属性,然后将值设置为参数:user.update_attribute(:premium, true)
    • Sasha,fourfourjew 提到不更新控制台中的用户对象是否有原因?它会不会在生产中弄乱我的数据库?
    • 据我所知。看起来他只是在建议一种方法来构建你的控制器来做你想做的事。他的方式不是唯一的方式,您可以随时随地更新用户。他建议将更多的 Stripe 行为封装在您的用户模型中,而不是在控制器中。尽管这是一种稍微 Railsier 的方式(胖模型,瘦控制器),但欢迎您遵循任一路径。
    猜你喜欢
    • 2017-10-12
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2016-07-02
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多