我正在尝试将条带集成到我的应用程序中。我正在关注 Railscast (#288),但我的印象是 Ryan 没有明确提及一些事情。
我的问题是我的应用程序上的 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
这是我的订阅模型.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