如何在测试模式下使用Stripe时创建一个高级用户来测试我的应用程序的高级功能?

问题描述:

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

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

我有t.boolean "premium"在我schema.rb文件。

我是编程新手,所以请让我知道你是否需要任何额外的文件信息。谢谢!

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

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 

我认为在你的控制器,你调用创建一个新的条纹费,像这样的方法:

@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设置方法和逻辑时,将所有逻辑放入Rails应用程序,并且测试模式将按照您希望实时模式工作的方式工作。不要更新控制台中的用户对象。

+0

我理解这个概念,但我没有@ user.update_with_payment在我的收费控制器或我的代码中的任何其他地方。现在我已经发布了我的收费控制器了吗?谢谢! – 2014-09-25 14:38:02

+0

如果你想在不更改代码的情况下显式更新数据库中的用户,那么你可以只做'fourfourjew'在顶部建议的内容 - 抓住用户('user = User.find(1)'),然后使用AR 'update_attribute'方法,该方法接受属性,然后将值设置为参数:'user.update_attribute(:premium,true)' – Sasha 2014-09-25 18:24:10

+0

Sasha,是否有一个原因,即fourfourjew提到不更新控制台中的用户对象?它可能会混淆我的生产数据库吗? – 2014-09-25 20:18:46