Class: Payola::Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Payola::Subscription
- Includes:
- AASM, GuidBehavior
- Defined in:
- app/models/payola/subscription.rb
Instance Attribute Summary collapse
-
#old_plan ⇒ Object
Returns the value of attribute old_plan.
-
#old_quantity ⇒ Object
Returns the value of attribute old_quantity.
Instance Method Summary collapse
- #conditional_stripe_token ⇒ Object
- #custom_fields ⇒ Object
- #instrument_plan_changed(old_plan) ⇒ Object
- #instrument_quantity_changed(old_quantity) ⇒ Object
- #name ⇒ Object
- #price ⇒ Object
- #redirect_path(sale) ⇒ Object
- #redirector ⇒ Object
- #sync_with!(stripe_sub) ⇒ Object
- #to_param ⇒ Object
- #verifier ⇒ Object
- #verify_charge ⇒ Object
- #verify_charge! ⇒ Object
Methods included from GuidBehavior
Instance Attribute Details
#old_plan ⇒ Object
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_quantity ⇒ Object
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_token ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/payola/subscription.rb', line 130 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_fields ⇒ Object
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
114 115 116 117 118 |
# File 'app/models/payola/subscription.rb', line 114 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
120 121 122 123 124 |
# File 'app/models/payola/subscription.rb', line 120 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 |
#name ⇒ Object
54 55 56 |
# File 'app/models/payola/subscription.rb', line 54 def name self.plan.name end |
#price ⇒ Object
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 |
#redirector ⇒ Object
126 127 128 |
# File 'app/models/payola/subscription.rb', line 126 def redirector plan end |
#sync_with!(stripe_sub) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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.cancel_at_period_end = stripe_sub.cancel_at_period_end self.save! self end |
#to_param ⇒ Object
110 111 112 |
# File 'app/models/payola/subscription.rb', line 110 def to_param guid end |
#verifier ⇒ Object
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_charge ⇒ Object
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. 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 |