andrewculver / koudoku

Robust subscription support for Rails with Stripe.
MIT License
1.16k stars 188 forks source link

Modifying the user instance after a successful subscription #132

Open FanaHOVA opened 9 years ago

FanaHOVA commented 9 years ago

Hi

I've koudoku installed in my app; my user model has a "credits" column, credits are obtained on sign up to each subscription level so I'd like to do something like this:

after_subscription_successful :add_credits

def add_credits
    current_user.credits += 10
end

How can it be done? The documentation on that part is really vague, I imagine this on the Subscription model could work?

finalize_new_subscription! :add_credits

Thanks in advance.

yas4891 commented 9 years ago

Yes, I'd go with overwriting finalize_new_subscription! in subscription. However, the syntax for that would be:

def finalize_new_subscription!
  # code goes here
end
dyatlov commented 8 years ago

To those who interested (not strongly related to the topic):

if you want to top up user credits every month (upon successful prolongation of subscription), then you should use this method:

class Subscription < ActiveRecord::Base
  include Koudoku::Subscription

  belongs_to :user
  belongs_to :coupon

  def payment_succeeded(amount)
    user.update_attribute(:paid_subscription, true) unless user.paid_subscription?
    user.credit.increment!(:amount, 10) # add 10 credits
  end

  def finalize_cancelation!
    user.update_attribute(:paid_subscription, false) if user.paid_subscription?
  end
end

maybe it will help someone. P.S.: Don't forget to add webhooks to your stripe account.