Module: MuckCommerce::Models::MuckSubscription

Extended by:
ActiveSupport::Concern
Defined in:
lib/muck-commerce/models/subscription.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#activate_subscriptionObject



184
185
186
# File 'lib/muck-commerce/models/subscription.rb', line 184

def activate_subscription
  self.update_attributes(:activated_at => Time.now, :suspended_at => nil, :cancelled_at => nil)
end

#amount_priceObject



257
258
259
# File 'lib/muck-commerce/models/subscription.rb', line 257

def amount_price
  self.amount/100.0
end

#cancel_subscriptionObject



188
189
190
# File 'lib/muck-commerce/models/subscription.rb', line 188

def cancel_subscription
  self.update_attribute(:cancelled_at, Time.now)
end

#chargeObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/muck-commerce/models/subscription.rb', line 215

def charge
  return false if self.cancelled? || self.suspended?

  return true if self.user.non_billable?

  if self.user.billing_information.blank?
    success = false
  elsif self.amount == 0
    success = true
  else
    @purchase = OrderTransaction.cim_purchase(self.amount_in_dollars, self.user.billing_information)
    success = @purchase.success?
    self.order_transactions.push(@purchase) if @purchase
  end

  if success
    self.reset_billing_date
    self.activate! if self.trial?
    true
  else
    self.update_attribute(:failed_billing_attempts, self.failed_billing_attempts + 1)
    if self.failed_billing_attempts > MuckCommerce.configuration.max_billing_attempt_failures
      self.suspend!
    end
    false
  end
end

#coupon_codeObject



249
250
251
# File 'lib/muck-commerce/models/subscription.rb', line 249

def coupon_code
  coupon.nil? ? "" : coupon.code
end

#enroll_user_in_program_coursesObject



243
244
245
246
247
# File 'lib/muck-commerce/models/subscription.rb', line 243

def enroll_user_in_program_courses
  self.subscription_plan.programs.each do |program|
    program.enroll_user(self.user)
  end
end

#needs_payment_info?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/muck-commerce/models/subscription.rb', line 253

def needs_payment_info?
  self.user.billing_information.blank?
end

#past_due?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/muck-commerce/models/subscription.rb', line 192

def past_due?
  self.next_renewal_at < DateTime.now.beginning_of_day
end

#reactivate_if_needed!Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/muck-commerce/models/subscription.rb', line 204

def reactivate_if_needed!
  return true if self.active? && !past_due?
  if OrderTransaction.cim_validate_user(self.user.billing_information).success?
    self.update_attributes(:failed_billing_attempts => 0)
    self.activate!
    true
  else
    false
  end
end

#reset_billing_dateObject



196
197
198
199
200
201
202
# File 'lib/muck-commerce/models/subscription.rb', line 196

def reset_billing_date
  if self.renewal_period
    update_attributes(:next_renewal_at => self.next_renewal_at.advance(:days => self.renewal_period), :failed_billing_attempts => 0)
  else
    update_attributes(:next_renewal_at => DateTime.now.advance(:months => 1), :failed_billing_attempts => 0)
  end
end

#suspend_subscriptionObject



179
180
181
182
# File 'lib/muck-commerce/models/subscription.rb', line 179

def suspend_subscription
  self.update_attribute(:suspended_at, Time.now)
  CommerceMailer.cc_declined(self.user).deliver
end