Module: PaidUp::Mixins::Subscriber::InstanceMethods

Defined in:
lib/paid_up/mixins/subscriber.rb

Overview

Included by subscriber mixin

Instance Method Summary collapse

Instance Method Details

#can_downgrade_to?(plan_to_check) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/paid_up/mixins/subscriber.rb', line 87

def can_downgrade_to?(plan_to_check)
  plan.present? && (
    !subscribed_to?(plan_to_check) &&
    (plan_to_check.sort_order.to_i < plan.sort_order.to_i)
  )
end

#can_upgrade_to?(plan_to_check) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/paid_up/mixins/subscriber.rb', line 80

def can_upgrade_to?(plan_to_check)
  plan.nil? || (
    !subscribed_to?(plan_to_check) &&
    (plan_to_check.sort_order.to_i > plan.sort_order.to_i)
  )
end

#cardsObject



38
39
40
# File 'lib/paid_up/mixins/subscriber.rb', line 38

def cards
  stripe_data.present? && stripe_data.sources.list(object: 'card')
end


71
72
73
74
# File 'lib/paid_up/mixins/subscriber.rb', line 71

def paid_up_subscription
  stripe_data.nil? && subscribe_to_free_plan
  @subscription
end

#plan_stripe_idObject



67
68
69
# File 'lib/paid_up/mixins/subscriber.rb', line 67

def plan_stripe_id
  paid_up_subscription&.stripe_data&.plan&.id
end

#reload(*args, &blk) ⇒ Object



28
29
30
31
32
# File 'lib/paid_up/mixins/subscriber.rb', line 28

def reload(*args, &blk)
  super(*args, &blk)
  load_stripe_data
  self
end

#rolify_setting(table_name) ⇒ Object



63
64
65
# File 'lib/paid_up/mixins/subscriber.rb', line 63

def rolify_setting(table_name)
  PaidUp::RolifyFeatureSettingType.new(name: table_name, user: self)
end

#stripe_dataObject



34
35
36
# File 'lib/paid_up/mixins/subscriber.rb', line 34

def stripe_data
  (stripe_id.present? || new_record?) && @customer_stripe_data
end

#subscribe_to_free_planObject



55
56
57
# File 'lib/paid_up/mixins/subscriber.rb', line 55

def subscribe_to_free_plan
  subscribe_to_plan PaidUp::Plan.free
end

#subscribe_to_plan(new_plan, stripe_token = nil, coupon = nil, trial_end = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paid_up/mixins/subscriber.rb', line 42

def subscribe_to_plan(
  new_plan, stripe_token = nil, coupon = nil, trial_end = nil
)
  # If there is an existing subscription
  if stripe_id.present? && paid_up_subscription.present?
    paid_up_subscription.update(new_plan, stripe_token, coupon, trial_end)
  else # Totally new subscription
    paid_up_subscription.create(new_plan, stripe_token, coupon, trial_end)
  end
  Rails.cache.delete("#{stripe_id}/stripe_data")
  reload
end

#subscribed_to?(plan_to_check) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/paid_up/mixins/subscriber.rb', line 76

def subscribed_to?(plan_to_check)
  plan.present? && plan.id == plan_to_check.id
end

#table_setting(table_name) ⇒ Object



59
60
61
# File 'lib/paid_up/mixins/subscriber.rb', line 59

def table_setting(table_name)
  PaidUp::TableFeatureSettingType.new(name: table_name, user: self)
end

#using_free_plan?Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/paid_up/mixins/subscriber.rb', line 94

def using_free_plan?
  plan.nil? || stripe_data.delinquent ||
    plan.stripe_id == PaidUp.configuration.free_plan_stripe_id
end