Proper way to deal with multiple subscriptions for a single user? #1140
-
I run a job board where customers can subscribe for promotion of their jobs on a recurring basis. Right now I manage this manually with a cron job, but I'm considering switching to Stripe subscriptions to handle the recurring logic and have Pay handle the Rails implementation of it. What's the best way to implement this with Pay? I'm aware a subscription can have a "name" to differentiate them. Is it okay to use a name such as For example, I might implement something like this: # app/models/job_listing.rb
class JobListing < ApplicationRecord
belongs_to :user
def subscribe!
raise "Already subscribed" if subscribed?
user.payment_processor.subscribe(name: name_for_pay_subscription, plan: "monthly")
end
def subscribed?
user.payment_processor.subscribed?(name: name_for_pay_subscription, plan: "monthly")
end
def cancel!
user.payment_processor.subscription(name: name_for_pay).cancel
end
def name_for_pay_subscription
"job_listing_#{id}"
end
end
# Somewhere in a controller:
@job_listing = @user.job_listings.find(params[:id])
@job_listing.subscribe! Does this seem like a decent approach? Any caveats I should be aware of? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
That would work or you could use metadata for it like |
Beta Was this translation helpful? Give feedback.
That would work or you could use metadata for it like
metadata: {job_listing_id: id}
(or both). You can use whatever you want for the name so you can query against it or use it as a friendly name to show customers.