Class: SubscriptionFu::Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SubscriptionFu::Subscription
- Defined in:
- app/models/subscription_fu/subscription.rb
Constant Summary collapse
- AVAILABLE_CANCEL_REASONS =
%w( update cancel gwcancel timeout admin )
Class Method Summary collapse
Instance Method Summary collapse
- #activated? ⇒ Boolean
- #activated_paid_subscription? ⇒ Boolean
- #canceled? ⇒ Boolean
- #end_date_when_canceled ⇒ Object
- #estimated_next_billing_date ⇒ Object
- #human_description ⇒ Object
-
#initiate_activation(initiator) ⇒ Object
billing API.
- #initiate_cancellation(initiator, activation_transaction) ⇒ Object
- #last_billing_date ⇒ Object
-
#next_billing_date ⇒ Object
billing time data about the subscription.
- #paid_subscription? ⇒ Boolean
- #plan ⇒ Object
- #successor_start_date(new_plan_name) ⇒ Object
- #sync_from_gateway! ⇒ Object
Class Method Details
.build_for_initializing(plan_key, prev_sub = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'app/models/subscription_fu/subscription.rb', line 31 def self.build_for_initializing(plan_key, prev_sub = nil) if prev_sub start_time = prev_sub.successor_start_date(plan_key) billing_start_time = prev_sub.end_date_when_canceled else billing_start_time = start_time = Time.now end new(:plan_key => plan_key, :starts_at => start_time, :billing_starts_at => billing_start_time, :prev_subscription => prev_sub) end |
.sync_all_from_gateway ⇒ Object
25 26 27 28 29 |
# File 'app/models/subscription_fu/subscription.rb', line 25 def self.sync_all_from_gateway SubscriptionFu::Subscription.using_paypal.not_canceled.each do |s| s.sync_from_gateway! end end |
Instance Method Details
#activated? ⇒ Boolean
45 46 47 |
# File 'app/models/subscription_fu/subscription.rb', line 45 def activated? ! activated_at.blank? end |
#activated_paid_subscription? ⇒ Boolean
49 50 51 |
# File 'app/models/subscription_fu/subscription.rb', line 49 def activated_paid_subscription? activated? && paid_subscription? end |
#canceled? ⇒ Boolean
53 54 55 |
# File 'app/models/subscription_fu/subscription.rb', line 53 def canceled? ! canceled_at.blank? end |
#end_date_when_canceled ⇒ Object
94 95 96 97 98 |
# File 'app/models/subscription_fu/subscription.rb', line 94 def end_date_when_canceled # in case this plan was already canceled, this date takes # precedence (there won't be a next billing time anymore). canceled_at || next_billing_date || estimated_next_billing_date || Time.now end |
#estimated_next_billing_date ⇒ Object
74 75 76 77 |
# File 'app/models/subscription_fu/subscription.rb', line 74 def estimated_next_billing_date p = last_billing_date p.next_month unless p.nil? end |
#human_description ⇒ Object
61 62 63 64 65 66 |
# File 'app/models/subscription_fu/subscription.rb', line 61 def human_description I18n.t(:description, :scope => [:subscription_fu, :subscription]) % { :plan_name => plan.human_name, :subject_desc => subject.human_description_for_subscription, :price => plan.human_price } end |
#initiate_activation(initiator) ⇒ Object
billing API
102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/subscription_fu/subscription.rb', line 102 def initiate_activation(initiator) gateway = (plan.free_plan? || sponsored?) ? 'nogw' : 'paypal' transactions.create_activation(gateway, initiator).tap do |t| if prev_subscription to_cancel = [prev_subscription] to_cancel.push(*prev_subscription.next_subscriptions.where("subscriptions.id <> ?", self).all) to_cancel.each {|s| s.initiate_cancellation(initiator, t) } end end end |
#initiate_cancellation(initiator, activation_transaction) ⇒ Object
113 114 115 |
# File 'app/models/subscription_fu/subscription.rb', line 113 def initiate_cancellation(initiator, activation_transaction) transactions.create_cancellation(initiator, activation_transaction, self) end |
#last_billing_date ⇒ Object
79 80 81 |
# File 'app/models/subscription_fu/subscription.rb', line 79 def last_billing_date paypal_recurring_details[:last_payment_date] if paypal? end |
#next_billing_date ⇒ Object
billing time data about the subscription
70 71 72 |
# File 'app/models/subscription_fu/subscription.rb', line 70 def next_billing_date paypal_recurring_details[:next_billing_date] if paypal? end |
#paid_subscription? ⇒ Boolean
41 42 43 |
# File 'app/models/subscription_fu/subscription.rb', line 41 def paid_subscription? ! plan.free_plan? && ! sponsored? end |
#plan ⇒ Object
57 58 59 |
# File 'app/models/subscription_fu/subscription.rb', line 57 def plan SubscriptionFu.config.available_plans[self.plan_key] end |
#successor_start_date(new_plan_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/subscription_fu/subscription.rb', line 83 def successor_start_date(new_plan_name) new_plan = SubscriptionFu.config.available_plans[new_plan_name] if new_plan > self.plan # higher plans always start immediately Time.now else # otherwise they start with the next billing cycle end_date_when_canceled end end |
#sync_from_gateway! ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'app/models/subscription_fu/subscription.rb', line 117 def sync_from_gateway! if paypal? if paypal_recurring_details[:status] == SubscriptionFu::Paypal::CANCELED_STATE t = initiate_cancellation(SubscriptionFu::SystemInitiator.paypal_sync_initiator, nil) t.complete(:effective => end_date_when_canceled, :reason => :gwcancel) end end end |