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.



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

def old_plan
  @old_plan
end

#old_quantityObject

Returns the value of attribute old_quantity.



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

def old_quantity
  @old_quantity
end

Instance Method Details

#custom_fieldsObject



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

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



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

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



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

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



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

def name
  self.plan.name
end

#priceObject



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

def price
  self.plan.amount
end

#redirect_path(sale) ⇒ Object



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

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

#redirectorObject



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

def redirector
  plan
end

#sync_with!(stripe_sub) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/payola/subscription.rb', line 94

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.cancel_at_period_end = stripe_sub.cancel_at_period_end

  self.save!
  self
end

#to_paramObject



109
110
111
# File 'app/models/payola/subscription.rb', line 109

def to_param
  guid
end

#verifierObject



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

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

#verify_chargeObject



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

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

#verify_charge!Object



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

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