Module: Saucy::Subscription

Extended by:
ActiveSupport::Concern
Defined in:
lib/saucy/subscription.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#billing_addressObject



53
54
55
# File 'lib/saucy/subscription.rb', line 53

def billing_address
  credit_card.billing_address if credit_card
end

#can_change_plan_to?(new_plan) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/saucy/subscription.rb', line 77

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

#credit_cardObject



49
50
51
# File 'lib/saucy/subscription.rb', line 49

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

#customerObject



45
46
47
# File 'lib/saucy/subscription.rb', line 45

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

#most_recent_transactionObject



85
86
87
# File 'lib/saucy/subscription.rb', line 85

def most_recent_transaction
  subscription.transactions.sort_by(&:created_at).last
end

#past_due?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/saucy/subscription.rb', line 81

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

#save_customer_and_subscription!(attributes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/saucy/subscription.rb', line 61

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



57
58
59
# File 'lib/saucy/subscription.rb', line 57

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