How to send different Plan ID's to Stripe?
I worked through the Subscriptions with Stripe video and got everything working fine, with a single Plan ID.
Now I'm trying to modify it to work with multiple Plan ID's to give my users payment options.
I created a model to hold all of my plans, with each plan ID matching the plan ID at Stripe, so that I can pull the ID from params to send to Stripe.
The wall I've hit is that I can't seem to actually send the param ID to Stripe.
What am I doing wrong?
subscriptions_controller.rb
def create
@company = Company.friendly.find(params[:company_id])
@plan = Plan.find(params[:id]) ### This ID works on #show but can't be found in #create
customer = Stripe::Customer.create(
email: current_user.email,
source: params[:stripeToken],
)
subscription = customer.subscriptions.create(
# plan: '001' ### This works
plan: @plan ### This doesn't
)
@company.users.each do |user|
user.update(
subscribed_at: Time.now,
canceled_at: nil
)
end
current_user.update(
stripe_id: customer.id,
stripe_subscription_id: subscription.id,
card_last4: params[:card_last4],
card_exp_month: params[:card_exp_month],
card_exp_year: params[:card_exp_year],
card_type: params[:card_brand],
stripe_plan_id: @plan, ### Need this to work
invitation_limit: @plan.users ### Need this to work
)
redirect_to company_path(@company)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to company_upgrades_path(@company)
end
Just rewatched the video and realized Chris suggested hidden fields as the best solution for this. So I got it working by adding these hidden fields to my Stripe form:
<%= hidden_field_tag(:plan_id, @plan.id) %>
<%= hidden_field_tag(:plan_seats, @plan.seats) %>
And modifying my controller as such:
def create
@company = Company.friendly.find(params[:company_id])
@plan_id = params[:plan_id] # <<<<
@plan_seats = params[:plan_seats] # <<<<
customer = Stripe::Customer.create(
email: current_user.email,
source: params[:stripeToken],
)
subscription = customer.subscriptions.create(
plan: @plan_id # <<<<
)
@company.users.each do |user|
user.update(
subscribed_at: Time.now,
canceled_at: nil
)
end
current_user.update(
stripe_id: customer.id,
stripe_subscription_id: subscription.id,
card_last4: params[:card_last4],
card_exp_month: params[:card_exp_month],
card_exp_year: params[:card_exp_year],
card_type: params[:card_brand],
stripe_plan_id: @plan_id, # <<<<
invitation_limit: @plan_seats # <<<<
)
redirect_to company_path(@company)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to company_upgrades_path(@company)
end