2019-08-14 09:48:44 +00:00
|
|
|
class Subscription < ApplicationRecord
|
|
|
|
include Events::Types
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
before_create :set_default_billing_params
|
2019-08-19 08:19:57 +00:00
|
|
|
after_create :notify_creation
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
enum state: [:trial, :active, :cancelled]
|
|
|
|
|
|
|
|
def payment_source_added!
|
|
|
|
self.payment_source_added = true
|
2019-10-20 08:47:26 +00:00
|
|
|
save
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def trial_expired?
|
|
|
|
(trial? && expiry < Date.current) ||
|
2019-10-20 08:47:26 +00:00
|
|
|
(cancelled? && !payment_source_added)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def suspended?
|
|
|
|
cancelled? && payment_source_added
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
{
|
|
|
|
state: state,
|
|
|
|
expiry: expiry.to_i
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_default_billing_params
|
|
|
|
self.expiry = Time.now + Plan.default_trial_period
|
|
|
|
self.pricing_version = Plan.default_pricing_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_creation
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(SUBSCRIPTION_CREATED, Time.zone.now, subscription: self)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|