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, #proration_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: stripe_id, expand: expand },
    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)
  update_quantity([1, 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)
  always_invoice

  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)
  update_quantity(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 = {})
  subscription.guard_against_incomplete

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

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

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

  subscription.update(stripe_plan: plan, quantity: item.quantity) if subscription.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 = {})
  always_invoice

  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)
  subscription.guard_against_incomplete

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

  update(quantity: quantity)

  subscription.update(quantity: quantity) if subscription.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(
    stripe_id, options, subscription.owner.stripe_options
  )
end