Class: Pay::Subscription

Inherits:
ApplicationRecord show all
Defined in:
app/models/pay/subscription.rb

Constant Summary collapse

STATUSES =
%w[incomplete incomplete_expired trialing active past_due canceled unpaid]

Instance Method Summary collapse

Instance Method Details

#active? ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/pay/subscription.rb', line 53

def active?
  ["trialing", "active"].include?(status) && (ends_at.nil? || on_grace_period? || on_trial?)
end

#cancel ⇒ Object



69
70
71
# File 'app/models/pay/subscription.rb', line 69

def cancel
  send("#{processor}_cancel")
end

#cancel_now! ⇒ Object



73
74
75
# File 'app/models/pay/subscription.rb', line 73

def cancel_now!
  send("#{processor}_cancel_now!")
end

#canceled? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/pay/subscription.rb', line 41

def canceled?
  ends_at?
end

#cancelled? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/pay/subscription.rb', line 45

def cancelled?
  canceled?
end

#has_incomplete_payment? ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/pay/subscription.rb', line 65

def has_incomplete_payment?
  past_due? || incomplete?
end

#incomplete? ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/pay/subscription.rb', line 61

def incomplete?
  status == "incomplete"
end

#latest_payment ⇒ Object



103
104
105
106
# File 'app/models/pay/subscription.rb', line 103

def latest_payment
  return unless stripe?
  processor_subscription(expand: ["latest_invoice.payment_intent"]).latest_invoice.payment_intent
end

#no_prorate ⇒ Object



29
30
31
# File 'app/models/pay/subscription.rb', line 29

def no_prorate
  self.prorate = false
end

#on_grace_period? ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/pay/subscription.rb', line 49

def on_grace_period?
  canceled? && Time.zone.now < ends_at
end

#on_trial? ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/pay/subscription.rb', line 37

def on_trial?
  trial_ends_at? && Time.zone.now < trial_ends_at
end

#past_due? ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/pay/subscription.rb', line 57

def past_due?
  status == "past_due"
end

#processor_subscription(options = {}) ⇒ Object



99
100
101
# File 'app/models/pay/subscription.rb', line 99

def processor_subscription(options = {})
  owner.processor_subscription(processor_id, options)
end

#resume ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/pay/subscription.rb', line 77

def resume
  unless on_grace_period?
    raise StandardError,
      "You can only resume subscriptions within their grace period."
  end

  send("#{processor}_resume")

  update(ends_at: nil, status: "active")
  self
end

#skip_trial ⇒ Object



33
34
35
# File 'app/models/pay/subscription.rb', line 33

def skip_trial
  self.trial_ends_at = nil
end

#swap(plan) ⇒ Object



89
90
91
92
# File 'app/models/pay/subscription.rb', line 89

def swap(plan)
  send("#{processor}_swap", plan)
  update(processor_plan: plan, ends_at: nil)
end

#swap_and_invoice(plan) ⇒ Object



94
95
96
97
# File 'app/models/pay/subscription.rb', line 94

def swap_and_invoice(plan)
  swap(plan)
  owner.invoice!(subscription_id: processor_id)
end