2012-07-04 28 views
0

我正在尝试将条纹集成到我的应用程序中。我正在关注一个Railscast(#288),但我觉得Ryan没有明确提及的几件事。使用条纹付款的Rails应用程序:无法从应用程序传输plan_id到条纹网站

我的问题是我的应用程序的plan_id没有传输到Stripe。一旦我在我的应用程序中生成测试交易并登录到Stripe,它将显示已创建具有有效卡的新客户,但plan_id未传输。我有计划安装条纹。但由于没有提交plan_id,信用卡永远不会被收取费用。所以我有客户,但没有付款。

这是我的订阅/ new.html.haml。我想知道是否需要为隐藏字段“plan_id”赋值。

%p 
    = @plan.name 
    = @plan.price 

= form_for @subscription do |f| 
    - if @subscription.errors.any? 
    .error_messages 
     %h2 
     = pluralize(@subscription.errors.count, "error") 
     prohibited this subscription from being saved: 
     %ul 
     - @subscription.errors.full_messages.each do |msg| 
      %li= msg 

    = f.hidden_field :plan_id 

    = f.hidden_field :stripe_card_token 

    .field 
    = f.label :email 
    = f.text_field :email 

    - if @subscription.stripe_card_token 
    Credit card has been provided 
    - else 
    .field 
     = label_tag :card_number, "Credit Card Number " 
     = text_field_tag :card_number, nil, name: nil 
    .field 
     = label_tag :card_code, "Security Code on Card (CVV)" 
     = text_field_tag :card_code, nil, name: nil 
    .field 
     = label_tag :card_month, "Card Expiration" 
     = select_month nil, {add_month_numbers_true: true}, {name: nil, id: "card_month"} 
     = select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} 

    .stripe_error 
    %noscript JavaScript is not enabled and is required for this form. First enable it in your web browser settings. 
    .actions= f.submit "Subscribe" 

这是我的订阅控制器。 params(:plan_id)使用字符串查询传递。

def new 
    @subscription = Subscription.new 
    @plan = Plan.find_by_id(params[:plan_id]) 
    end 

    def create 
    @subscription = Subscription.new(params[:subscription]) 
    if @subscription.save_with_payment 
     flash[:success] = "Thank you for subscribing!" 
     redirect_to @subscription 
     else 
     render 'new' 
     end 
    end 

这里是我的subscription.rb

class Subscription < ActiveRecord::Base 

    has_many :users 

    belongs_to :plan 

    #validates_presence_of :plan_id, :email 

    attr_accessible :stripe_card_token, :plan_id, :email 
    attr_accessor :stripe_card_token 

    def save_with_payment 
    if valid? 
     customer = Stripe::Customer.create(description:email, plan: plan_id, card: stripe_card_token) 
     self.stripe_customer_token = customer.id 
     save! 
    end 
    rescue Stripe::InvalidRequestError => e 
    logger.error "Stripe error while creating customer: #{e.message}" 
    errors.add :base, "There was a problem with your credit card." 
    end 
end 

回答

2

我结束了传递plan_id的数据类型,以这样的新方法的模型。

@subscription = Subscription.new(plan_id: params[:plan_id]) 

简单的答案。希望我早点实现了它。