Module: Saucy::Subscription::InstanceMethods

Defined in:
lib/saucy/subscription.rb

Instance Method Summary collapse

Instance Method Details

#can_change_plan_to?(new_plan) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/saucy/subscription.rb', line 60

def can_change_plan_to?(new_plan)
  within_limits_for?(new_plan) && !past_trial_for?(new_plan)
end

#credit_cardObject



36
37
38
# File 'lib/saucy/subscription.rb', line 36

def credit_card
  customer.credit_cards[0] if customer && customer.credit_cards.any?
end

#customerObject



32
33
34
# File 'lib/saucy/subscription.rb', line 32

def customer
  Braintree::Customer.find(customer_token) if customer_token
end

#past_due?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/saucy/subscription.rb', line 64

def past_due?
  subscription_status == Braintree::Subscription::Status::PastDue
end

#save_customer_and_subscription!(attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/saucy/subscription.rb', line 44

def save_customer_and_subscription!(attributes)
  successful = true
  self.plan = ::Plan.find(attributes[:plan_id]) if changing_plan?(attributes)
  if changing_customer_attributes?(attributes)
    successful = update_customer(attributes)
  end
  if successful && past_due?
    successful = retry_subscription_charge!
  end
  if successful && changing_plan?(attributes)
    save_subscription
    flush_cache :subscription
  end
  successful && save
end

#subscriptionObject



40
41
42
# File 'lib/saucy/subscription.rb', line 40

def subscription
  Braintree::Subscription.find(subscription_token) if subscription_token
end