Reputation: 2665
I'm trying to integrate stripe into my app. I'm following a Railscast (#288) but I'm getting the impression there a are few things that Ryan isn't mentioning explicitly.
My problem is that the plan_id on my app doesn't get transmitted over to Stripe. Once I generate a test transaction in my app and I log on to Stripe, it will show that a new customer with a valid card was created, but the plan_id isn't transmitted. I do have plans setup on stripe. But since no plan_id is submitted, the credit card never gets charged. So I've got customers, but no payments.
Here's my subscription/new.html.haml. I'm wondering if I need to assign a value to the hidden field "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"
Here's my subscription controller. The params(:plan_id) is passed using a string query.
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
Here's my model for 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
Upvotes: 0
Views: 453
Reputation: 2665
I ended up passing the plan_id to the new method like this.
@subscription = Subscription.new(plan_id: params[:plan_id])
Easy answer. Wish I had realized it earlier.
Upvotes: 2