Class: PaidUp::Subscription

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/paid_up/subscription.rb

Overview

Subscription Class: Not an ActiveRecord object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Subscription

Returns a new instance of Subscription.



15
16
17
18
19
# File 'lib/paid_up/subscription.rb', line 15

def initialize(args)
  super args
  return unless user.stripe_data.respond_to? :subscriptions
  self.stripe_data = user.stripe_data.subscriptions.first
end

Instance Attribute Details

#stripe_dataObject

Returns the value of attribute stripe_data.



10
11
12
# File 'lib/paid_up/subscription.rb', line 10

def stripe_data
  @stripe_data
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/paid_up/subscription.rb', line 10

def user
  @user
end

Instance Method Details

#create(plan, stripe_token = nil, coupon = nil, trial_end = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/paid_up/subscription.rb', line 38

def create(plan, stripe_token = nil, coupon = nil, trial_end = nil)
  customer = Stripe::Customer.create(source: stripe_token, email: email,
                                     plan: plan.stripe_id, coupon: coupon,
                                     trial_end: trial_end) ||
             raise(:could_not_create_subscription.l)

  # If there is an update to be made, we go ahead
  return true if stripe_id == customer.id
  user.update_attribute(:stripe_id, customer.id) ||
    raise(:could_not_associate_subscription.l)
end

#planObject



21
22
23
24
25
26
27
# File 'lib/paid_up/subscription.rb', line 21

def plan
  if stripe_data&.plan&.id.present?
    PaidUp::Plan.find_by_stripe_id(stripe_data.plan.id)
  else
    PaidUp::Plan.free
  end
end

#update(plan, stripe_token = nil, coupon = nil, trial_end = nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/paid_up/subscription.rb', line 29

def update(plan, stripe_token = nil, coupon = nil, trial_end = nil)
  return if stripe_data.nil?
  update_stripe_token(stripe_token)
  update_coupon(coupon)
  update_trial_end(trial_end)
  update_plan(plan)
  stripe_data.save || raise(:could_not_update_plan.l)
end