Class: Tang::Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tang::Subscription
- Includes:
- AASM
- Defined in:
- app/models/tang/subscription.rb
Constant Summary collapse
- STATUSES =
['trialing', 'active', 'past_due', 'canceled', 'unpaid']
Class Method Summary collapse
Instance Method Summary collapse
- #discount_for_plan(plan) ⇒ Object
- #end_trial_now ⇒ Object
- #end_trial_now=(val) ⇒ Object
- #period_end ⇒ Object
- #period_start ⇒ Object
- #quantity ⇒ Object
- #stripe_trial_end ⇒ Object
- #upgraded ⇒ Object
- #upgraded=(val) ⇒ Object
Class Method Details
.from_stripe(stripe_subscription, customer, plan) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/tang/subscription.rb', line 51 def self.from_stripe(stripe_subscription, customer, plan) subscription = Subscription.find_or_create_by(stripe_id: stripe_subscription.id) do |s| s.customer = customer s.plan = plan s.application_fee_percent = stripe_subscription.application_fee_percent s.quantity = stripe_subscription.quantity # s.tax_percent = stripe_subscription.tax_percent # removed from api in favor of tax rates s.trial_end = stripe_subscription.trial_end s.coupon = Coupon.find_by(stripe_id: stripe_subscription.discount.coupon.id) if stripe_subscription.discount.present? s.status = stripe_subscription.status end return subscription end |
Instance Method Details
#discount_for_plan(plan) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'app/models/tang/subscription.rb', line 107 def discount_for_plan(plan) amount_off = 0 if self.coupon.percent_off.present? amount_off = (self.coupon.percent_off.to_f / 100.0) * plan.amount.to_f elsif self.coupon.amount_off.present? amount_off = self.coupon.amount_off end return amount_off end |
#end_trial_now ⇒ Object
91 92 93 |
# File 'app/models/tang/subscription.rb', line 91 def end_trial_now @end_trial_now || false end |
#end_trial_now=(val) ⇒ Object
95 96 97 |
# File 'app/models/tang/subscription.rb', line 95 def end_trial_now=(val) @end_trial_now = val end |
#period_end ⇒ Object
73 74 75 76 77 78 79 |
# File 'app/models/tang/subscription.rb', line 73 def period_end invoice = invoices.last if invoice.present? return invoice.period_end end return plan.period_days_from(period_start) end |
#period_start ⇒ Object
65 66 67 68 69 70 71 |
# File 'app/models/tang/subscription.rb', line 65 def period_start invoice = invoices.last if invoice.present? return invoice.period_start end return created_at end |
#quantity ⇒ Object
81 82 83 |
# File 'app/models/tang/subscription.rb', line 81 def quantity self[:quantity] || 1 end |
#stripe_trial_end ⇒ Object
85 86 87 88 89 |
# File 'app/models/tang/subscription.rb', line 85 def stripe_trial_end return 'now' if self.end_trial_now return self.trial_end.to_i if self.trial_end.present? return nil end |
#upgraded ⇒ Object
99 100 101 |
# File 'app/models/tang/subscription.rb', line 99 def upgraded @upgraded || false end |
#upgraded=(val) ⇒ Object
103 104 105 |
# File 'app/models/tang/subscription.rb', line 103 def upgraded=(val) @upgraded = val end |