Class: Reji::SubscriptionItem

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
InteractsWithPaymentBehavior, Prorates
Defined in:
lib/reji/subscription_item.rb

Instance Method Summary collapse

Methods included from Prorates

#always_invoice, #no_prorate, #prorate, #prorate_behavior, #set_proration_behavior

Methods included from InteractsWithPaymentBehavior

#allow_payment_failures, #error_if_payment_fails, #payment_behavior, #pending_if_payment_fails

Instance Method Details

#as_stripe_subscription_item(expand = {}) ⇒ Object

Get the subscription as a Stripe subscription item object.



90
91
92
93
94
95
# File 'lib/reji/subscription_item.rb', line 90

def as_stripe_subscription_item(expand = {})
  Stripe::SubscriptionItem.retrieve(
    {:id => self.stripe_id, :expand => expand},
    self.subscription.owner.stripe_options
  )
end

#decrement_quantity(count = 1) ⇒ Object

Decrement the quantity of the subscription item.



27
28
29
30
31
# File 'lib/reji/subscription_item.rb', line 27

def decrement_quantity(count = 1)
  self.update_quantity([1, self.quantity - count].max)

  self
end

#increment_and_invoice(count = 1) ⇒ Object

Increment the quantity of the subscription item, and invoice immediately.



18
19
20
21
22
23
24
# File 'lib/reji/subscription_item.rb', line 18

def increment_and_invoice(count = 1)
  self.always_invoice

  self.increment_quantity(count)

  self
end

#increment_quantity(count = 1) ⇒ Object

Increment the quantity of the subscription item.



11
12
13
14
15
# File 'lib/reji/subscription_item.rb', line 11

def increment_quantity(count = 1)
  self.update_quantity(self.quantity + count)

  self
end

#swap(plan, options = {}) ⇒ Object

Swap the subscription item to a new Stripe plan.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reji/subscription_item.rb', line 51

def swap(plan, options = {})
  self.subscription.guard_against_incomplete

  options = {
    :plan => plan,
    :quantity => self.quantity,
    :payment_behavior => self.payment_behavior,
    :proration_behavior => self.prorate_behavior,
    :tax_rates => self.subscription.get_plan_tax_rates_for_payload(plan)
  }.merge(options)

  item = Stripe::SubscriptionItem::update(
    self.stripe_id,
    options,
    self.subscription.owner.stripe_options
  )

  self.update(stripe_plan: plan, quantity: item.quantity)

  self.subscription.update(stripe_plan: plan, quantity: item.quantity) if self.subscription.has_single_plan

  self
end

#swap_and_invoice(plan, options = {}) ⇒ Object

Swap the subscription item to a new Stripe plan, and invoice immediately.



76
77
78
79
80
# File 'lib/reji/subscription_item.rb', line 76

def swap_and_invoice(plan, options = {})
  self.always_invoice

  self.swap(plan, options)
end

#update_quantity(quantity) ⇒ Object

Update the quantity of the subscription item.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reji/subscription_item.rb', line 34

def update_quantity(quantity)
  self.subscription.guard_against_incomplete

  stripe_subscription_item = self.as_stripe_subscription_item
  stripe_subscription_item.quantity = quantity
  stripe_subscription_item.payment_behavior = self.payment_behavior
  stripe_subscription_item.proration_behavior = self.prorate_behavior
  stripe_subscription_item.save

  self.update(quantity: quantity)

  self.subscription.update(quantity: quantity) if self.subscription.has_single_plan

  self
end

#update_stripe_subscription_item(options = {}) ⇒ Object

Update the underlying Stripe subscription item information for the model.



83
84
85
86
87
# File 'lib/reji/subscription_item.rb', line 83

def update_stripe_subscription_item(options = {})
  Stripe::SubscriptionItem.update(
    self.stripe_id, options, self.subscription.owner.stripe_options
  )
end