Module: ActsAsSubscription::Subscription::SubscriptionInstanceMethods

Defined in:
lib/acts_as_subscription/subscription.rb

Overview

Instance methods for an ActsAsSubscription::Subscription instance, which use ActiveMerchant to validate credit card information.

These instance methods should be included in ActiveRecord::Base to provide subscription validation support.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cc_numberObject

Allows entry of a complete credit card number, even though we only store the last 4 digits internally.



145
146
147
# File 'lib/acts_as_subscription/subscription.rb', line 145

def cc_number
  @cc_number
end

#credit_cardObject (readonly)

Instantiates an instance of ActiveMerchant::Billing::CreditCard if one hasn’t already been created.



141
142
143
# File 'lib/acts_as_subscription/subscription.rb', line 141

def credit_card
  @credit_card
end

Instance Method Details

#backend_saveObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/acts_as_subscription/subscription.rb', line 161

def backend_save
  if self.new_record?
    result = ActsAsSubscription::Subscription::Backend.instance.create_subscription(self)
  else
    result = ActsAsSubscription::Subscription::Backend.instance.update_subscription(self)
  end
  
  # Decide whether payment has been recorded.
  self.payment_details_recorded = (result and !self.is_free_plan?) unless self.payment_details_recorded
    
  # No way of knowing what fields the errors are for for some backends - just add a top-level error.
  unless result == true
    self.errors.add :base, result
    return false
  end
  
  return true
end

#is_free_plan?Boolean

Returns true if the current plan is free, false otherwise.

Returns:

  • (Boolean)


148
149
150
151
152
153
# File 'lib/acts_as_subscription/subscription.rb', line 148

def is_free_plan?
  # If no plan-code is given, assume this is a paid plan to be on the safe side.
  return false unless self.plan_code
  
  return (self.plan_code =~ /free/i) != nil
end

#require_verification_value?Boolean

This can be overridden in the implementing model, to turn off verification-value validation.

Returns:

  • (Boolean)


186
187
188
# File 'lib/acts_as_subscription/subscription.rb', line 186

def require_verification_value?
  return (self.is_free_plan? == false)
end

#require_zip_code?Boolean

This can be overridden in the implementing model, to turn off zip-code validation.

Returns:

  • (Boolean)


181
182
183
# File 'lib/acts_as_subscription/subscription.rb', line 181

def require_zip_code?
  return (self.is_free_plan? == false)
end