2012-07-04 58 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]) 

簡單的答案。希望我早點實現了它。