Module: Reji::ManagesSubscriptions

Extended by:
ActiveSupport::Concern
Included in:
Billable
Defined in:
lib/reji/concerns/manages_subscriptions.rb

Instance Method Summary collapse

Instance Method Details

#incomplete_payment?(name = 'default') ⇒ Boolean

Determine if the customer’s subscription has an incomplete payment.

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/reji/concerns/manages_subscriptions.rb', line 50

def incomplete_payment?(name = 'default')
  subscription = self.subscription(name)

  subscription ? subscription.incomplete_payment? : false
end

#new_subscription(name, plans) ⇒ Object

Begin creating a new subscription.



12
13
14
# File 'lib/reji/concerns/manages_subscriptions.rb', line 12

def new_subscription(name, plans)
  SubscriptionBuilder.new(self, name, plans)
end

#on_generic_trialObject

Determine if the Stripe model is on a “generic” trial at the model level.



28
29
30
# File 'lib/reji/concerns/manages_subscriptions.rb', line 28

def on_generic_trial
  !!trial_ends_at && trial_ends_at.future?
end

#on_plan(plan) ⇒ Object

Determine if the entity has a valid subscription on the given plan.



72
73
74
# File 'lib/reji/concerns/manages_subscriptions.rb', line 72

def on_plan(plan)
  subscriptions.any? { |subscription| subscription.valid && subscription.plan?(plan) }
end

#on_trial(name = 'default', plan = nil) ⇒ Object

Determine if the Stripe model is on trial.



17
18
19
20
21
22
23
24
25
# File 'lib/reji/concerns/manages_subscriptions.rb', line 17

def on_trial(name = 'default', plan = nil)
  return true if name == 'default' && plan.nil? && on_generic_trial

  subscription = self.subscription(name)

  return false unless subscription&.on_trial

  plan ? subscription.plan?(plan) : true
end

#plan_tax_ratesObject

Get the tax rates to apply to individual subscription items.



87
88
89
# File 'lib/reji/concerns/manages_subscriptions.rb', line 87

def plan_tax_rates
  {}
end

#subscribed(name = 'default', plan = nil) ⇒ Object

Determine if the Stripe model has a given subscription.



33
34
35
36
37
38
39
# File 'lib/reji/concerns/manages_subscriptions.rb', line 33

def subscribed(name = 'default', plan = nil)
  subscription = self.subscription(name)

  return false unless subscription&.valid?

  plan ? subscription.plan?(plan) : true
end

#subscribed_to_plan(plans, name = 'default') ⇒ Object

Determine if the Stripe model is actively subscribed to one of the given plans.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/reji/concerns/manages_subscriptions.rb', line 57

def subscribed_to_plan(plans, name = 'default')
  subscription = self.subscription(name)

  return false unless subscription&.valid?

  plans = [plans] unless plans.instance_of? Array

  plans.each do |plan|
    return true if subscription.plan?(plan)
  end

  false
end

#subscription(name = 'default') ⇒ Object

Get a subscription instance by name.



42
43
44
45
46
47
# File 'lib/reji/concerns/manages_subscriptions.rb', line 42

def subscription(name = 'default')
  subscriptions
    .sort_by { |subscription| subscription.created_at.to_i }
    .reverse
    .find { |subscription| subscription.name == name }
end

#tax_percentageObject

Get the tax percentage to apply to the subscription.



77
78
79
# File 'lib/reji/concerns/manages_subscriptions.rb', line 77

def tax_percentage
  0
end

#tax_ratesObject

Get the tax rates to apply to the subscription.



82
83
84
# File 'lib/reji/concerns/manages_subscriptions.rb', line 82

def tax_rates
  []
end