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 paused]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_processor_and_id(processor, processor_id) ⇒ Object



47
48
49
# File 'app/models/pay/subscription.rb', line 47

def self.find_by_processor_and_id(processor, processor_id)
  joins(:customer).find_by(processor_id: processor_id, pay_customers: {processor: processor})
end

Instance Method Details

#active?Boolean

If you cancel during a trial, you should still retain access until the end of the trial Otherwise a subscription is active unless it has ended or is currently paused Check the subscription status so we don’t accidentally consider “incomplete”, “unpaid”, or other statuses as active

Returns:

  • (Boolean)


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

def active?
  ["trialing", "active"].include?(status) &&
    (!(canceled? || paused?) || on_trial? || on_grace_period?)
end

#canceled?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/pay/subscription.rb', line 79

def canceled?
  ends_at?
end

#cancelled?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/pay/subscription.rb', line 83

def cancelled?
  canceled?
end

#ended?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/pay/subscription.rb', line 87

def ended?
  ends_at? && ends_at <= Time.current
end

#generic_trial?Boolean

Returns:

  • (Boolean)


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

def generic_trial?
  fake_processor? && trial_ends_at?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/pay/subscription.rb', line 115

def has_incomplete_payment?
  past_due? || incomplete?
end

#has_trial?Boolean

Returns:

  • (Boolean)


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

def has_trial?
  trial_ends_at?
end

#incomplete?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/pay/subscription.rb', line 111

def incomplete?
  status == "incomplete"
end

#on_grace_period?Boolean

Returns:

  • (Boolean)


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

def on_grace_period?
  ends_at? && ends_at > Time.current
end

#on_trial?Boolean

Does not include the last second of the trial

Returns:

  • (Boolean)


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

def on_trial?
  return false if ended?
  trial_ends_at? && trial_ends_at > Time.current
end

#past_due?Boolean

Returns:

  • (Boolean)


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

def past_due?
  status == "past_due"
end

#skip_trialObject



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

def skip_trial
  self.trial_ends_at = nil
end

#swap_and_invoice(plan) ⇒ Object



119
120
121
122
# File 'app/models/pay/subscription.rb', line 119

def swap_and_invoice(plan)
  swap(plan)
  customer.invoice!(subscription: processor_id)
end

#sync!(**options) ⇒ Object



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

def sync!(**options)
  self.class.sync(processor_id, **options)
  reload
end

#trial_ended?Boolean

Returns:

  • (Boolean)


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

def trial_ended?
  return true if ended?
  trial_ends_at? && trial_ends_at <= Time.current
end

#unpaid?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/pay/subscription.rb', line 107

def unpaid?
  status == "unpaid"
end