I am trying to implement multiple plan subscription on a rails app with stripe.
My subscription controller
def new
session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
client_reference_id: current_user.id,
customer_email: current_user.email,
subscription_data: {
items: [{
plan: 'beginner'
}]
},
success_url: 'http://localhost:3000/',
cancel_url: 'http://localhost:3000/'
)
@session_id = session.id
end
My Routes
....
resources :subscriptions, only: :new
....
My subscription button is like
<%= link_to 'Subscribe to Beginner', new_subscription_path, %>
<%= link_to 'Subscribe to Pro', #TODO, %>
With this setup, I can subscribe to the beginner plan with no issue.
My question is how to add a pro plan to this setup. What will the routes look like?
I have all the plans created in my stripe dashboard. I have checked the docs but it is not clear for me.
I have seen you can pass params in the link like so
<%= link_to "subscribe", some_path(:params[:value]) %>
How can I achieve that kind of URL?
And after all, I want to have a method on the user model to check if the user is subscribed to the beginner or pro
Thanks
Looks like you're asking about "query parameters".
You can add params to the URL
<%= link_to 'Subscribe to Beginner', new_subscription_path(plan: :beginner), %>
<%= link_to 'Subscribe to Pro', new_subscription_path(plan: :pro), %>
new_subscription_path(plan: :beginner)
will evaluate to subscription/new?plan=beginner
Then you can access this in the model via params.
def new
plan_type = params[:plan]
session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
client_reference_id: current_user.id,
customer_email: current_user.email,
subscription_data: {
items: [{
plan: plan_type
}]
},
success_url: 'http://localhost:3000/',
cancel_url: 'http://localhost:3000/'
)
@session_id = session.id
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments