Class: Payola::Subscription

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AASM, GuidBehavior
Defined in:
app/models/payola/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GuidBehavior

#populate_guid

Instance Attribute Details

#old_planObject

Returns the value of attribute old_plan.



24
25
26
# File 'app/models/payola/subscription.rb', line 24

def old_plan
  @old_plan
end

#old_quantityObject

Returns the value of attribute old_quantity.



24
25
26
# File 'app/models/payola/subscription.rb', line 24

def old_quantity
  @old_quantity
end

Instance Method Details

#conditional_stripe_tokenObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/payola/subscription.rb', line 135

def conditional_stripe_token
  # Don't require a Stripe token if the subscription has an owner - we'll try to reuse the Stripe customer from an existing successful subscription
  return true if owner.present?
  # Don't require a Stripe token if we're creating a subscription for an existing Stripe customer
  return true if stripe_customer_id.present?
  return true if plan.nil?
  if (plan.amount > 0 )
    if plan.respond_to?(:trial_period_days) and (plan.trial_period_days.nil? or ( plan.trial_period_days and !(plan.trial_period_days > 0) ))
      errors.add(:base, 'No Stripe token is present for a paid plan') if stripe_token.nil?
    end
  end
end

#custom_fieldsObject



87
88
89
90
91
92
93
# File 'app/models/payola/subscription.rb', line 87

def custom_fields
  if self.signed_custom_fields.present?
    verifier.verify(self.signed_custom_fields)
  else
    nil
  end
end

#instrument_plan_changed(old_plan) ⇒ Object



119
120
121
122
123
# File 'app/models/payola/subscription.rb', line 119

def instrument_plan_changed(old_plan)
  self.old_plan = old_plan
  Payola.instrument(instrument_key('plan_changed'), self)
  Payola.instrument(instrument_key('plan_changed', false), self)
end

#instrument_quantity_changed(old_quantity) ⇒ Object



125
126
127
128
129
# File 'app/models/payola/subscription.rb', line 125

def instrument_quantity_changed(old_quantity)
  self.old_quantity = old_quantity
  Payola.instrument(instrument_key('quantity_changed'), self)
  Payola.instrument(instrument_key('quantity_changed', false), self)
end

#nameObject



54
55
56
# File 'app/models/payola/subscription.rb', line 54

def name
  self.plan.name
end

#priceObject



58
59
60
# File 'app/models/payola/subscription.rb', line 58

def price
  self.plan.amount
end

#redirect_path(sale) ⇒ Object



62
63
64
# File 'app/models/payola/subscription.rb', line 62

def redirect_path(sale)
  self.plan.redirect_path(self)
end

#redirectorObject



131
132
133
# File 'app/models/payola/subscription.rb', line 131

def redirector
  plan
end

#sync_with!(stripe_sub) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/payola/subscription.rb', line 95

def sync_with!(stripe_sub)
  self.current_period_start = Time.at(stripe_sub.current_period_start)
  self.current_period_end   = Time.at(stripe_sub.current_period_end)
  self.ended_at             = Time.at(stripe_sub.ended_at) if stripe_sub.ended_at
  self.trial_start          = Time.at(stripe_sub.trial_start) if stripe_sub.trial_start
  self.trial_end            = Time.at(stripe_sub.trial_end) if stripe_sub.trial_end
  self.canceled_at          = Time.at(stripe_sub.canceled_at) if stripe_sub.canceled_at
  self.quantity             = stripe_sub.quantity
  self.stripe_status        = stripe_sub.status
  self.amount               = stripe_sub.plan.amount
  self.currency             = stripe_sub.plan.respond_to?(:currency) ? stripe_sub.plan.currency : Payola.default_currency
  self.cancel_at_period_end = stripe_sub.cancel_at_period_end

  # Support for discounts is added to stripe-ruby-mock in v2.2.0, 84f08eb
  self.coupon               = stripe_sub.discount && stripe_sub.discount.coupon.id if stripe_sub.respond_to?(:discount)

  self.save!
  self
end

#to_paramObject



115
116
117
# File 'app/models/payola/subscription.rb', line 115

def to_param
  guid
end

#verifierObject



66
67
68
# File 'app/models/payola/subscription.rb', line 66

def verifier
  @verifier ||= ActiveSupport::MessageVerifier.new(Payola.secret_key_for_sale(self), digest: 'SHA256')
end

#verify_chargeObject



70
71
72
73
74
75
76
77
# File 'app/models/payola/subscription.rb', line 70

def verify_charge
  begin
    self.verify_charge!
  rescue RuntimeError => e
    self.error = e.message
    self.fail!
  end
end

#verify_charge!Object



79
80
81
82
83
84
85
# File 'app/models/payola/subscription.rb', line 79

def verify_charge!
  if Payola.charge_verifier.arity > 1
    Payola.charge_verifier.call(self, custom_fields)
  else
    Payola.charge_verifier.call(self)
  end
end