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


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

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

.pay_processor_for(name) ⇒ Object


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

def self.pay_processor_for(name)
  "Pay::#{name.to_s.classify}::Subscription".constantize
end

.with_metered_itemsObject


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/pay/subscription.rb', line 52

def self.with_metered_items
  case Pay::Adapter.current_adapter
  when "sqlite3"
    where("json_extract(data, '$.\"metered\"') = true")
    # For SQLite 3.38+ we could use the arrows
    # where("data->'metered' = ?", "true")
  when "mysql2"
    where("data->'$.\"metered\"' = true")
  when "postgresql", "postgis"
    # Single quotes are important for json keys apparently
    where("data->>'metered' = 'true'")
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

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

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

#canceled?Boolean

Returns:

  • (Boolean)

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

def canceled?
  ends_at?
end

#cancelled?Boolean

Returns:

  • (Boolean)

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

def cancelled?
  canceled?
end

#change_quantity(quantity) ⇒ Object


126
127
128
129
# File 'app/models/pay/subscription.rb', line 126

def change_quantity(quantity)
  payment_processor.change_quantity(quantity)
  update(quantity: quantity)
end

#generic_trial?Boolean

Returns:

  • (Boolean)

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

def generic_trial?
  fake_processor? && trial_ends_at?
end

#has_incomplete_payment?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'app/models/pay/subscription.rb', line 122

def has_incomplete_payment?
  past_due? || incomplete?
end

#incomplete?Boolean

Returns:

  • (Boolean)

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

def incomplete?
  status == "incomplete"
end

#latest_paymentObject


152
153
154
# File 'app/models/pay/subscription.rb', line 152

def latest_payment
  processor_subscription(expand: ["latest_invoice.payment_intent"]).latest_invoice.payment_intent
end

#metered_items?Boolean

Returns:

  • (Boolean)

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

def metered_items?
  !!metered
end

#no_prorateObject


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

def no_prorate
  self.prorate = false
end

#on_trial?Boolean

Returns:

  • (Boolean)

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

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

#paddle_paused_fromObject


156
157
158
159
160
# File 'app/models/pay/subscription.rb', line 156

def paddle_paused_from
  if (timestamp = super)
    Time.zone.parse(timestamp)
  end
end

#past_due?Boolean

Returns:

  • (Boolean)

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

def past_due?
  status == "past_due"
end

#pause_resumes_atObject


162
163
164
165
166
# File 'app/models/pay/subscription.rb', line 162

def pause_resumes_at
  if (resumes_at = super)
    Time.zone.parse(resumes_at)
  end
end

#payment_processorObject


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

def payment_processor
  @payment_processor ||= self.class.pay_processor_for(customer.processor).new(self)
end

#processor_subscription(**options) ⇒ Object


148
149
150
# File 'app/models/pay/subscription.rb', line 148

def processor_subscription(**options)
  payment_processor.subscription(**options)
end

#resumeObject


131
132
133
134
135
# File 'app/models/pay/subscription.rb', line 131

def resume
  payment_processor.resume
  update(ends_at: nil, status: :active)
  self
end

#skip_trialObject


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

def skip_trial
  self.trial_ends_at = nil
end

#swap(plan) ⇒ Object

Raises:

  • (ArgumentError)

137
138
139
140
141
# File 'app/models/pay/subscription.rb', line 137

def swap(plan)
  raise ArgumentError, "plan must be a string. Got `#{plan.inspect}` instead." unless plan.is_a?(String)
  payment_processor.swap(plan)
  update(processor_plan: plan, ends_at: nil, status: :active)
end

#swap_and_invoice(plan) ⇒ Object


143
144
145
146
# File 'app/models/pay/subscription.rb', line 143

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

#sync!Object


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

def sync!
  self.class.pay_processor_for(customer.processor).sync(processor_id)
end