Save 36% for Black Friday! Learn more

New Discussion

Notifications

You’re not receiving notifications from this thread.

How do I add multiple subscriptions to one user ?

3
General

I'm able to assign multiple subscriptions to a user, but the problem is that everytime I assign a new subscription to a user, I'm overriding the subscription_id.

I can still show the user all his subscriptions through Stripe::Subscription.list(customer: current_user.stripe_id), but it's not possible to show a specific subscription, just the last one that was created.

How can I overcome this ?

Hey Arnas,

Try changing it so you have a Subscription belongs_to :user and a User has_many :subscriptions. Then use the association to handle the subscriptions in your app.

Hey Chris,
I'm currently working without a Model, therefore it's a bit tricky, but I guess I'll be able to handle this.

Just this last step isn't working fully to my intention...

if current_user.subscription_id?
Stripe::SubscriptionItem.create(
subscription: current_user.subscription.id,
plan: plan.id
)
else
subscription = customer.subscriptions.create(
plan: plan.id
)
current_user.update(
stripe_id: customer.id,
subscription_id: subscription.id
)
end

This works when I add a new plan to the subscription, but it throws an error when I subscribe to the same plan twice. Stripe::InvalidRequestError (Cannot add multiple subscription items with the same plan: XXXXXX

Yeah, you can't subscribe to the same thing twice. You can use quantities if you need something like that.

Join the discussion
Create an account Log in