Class: Ashmont::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/ashmont/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, cached_attributes = {}) ⇒ Subscription

Returns a new instance of Subscription.



10
11
12
13
14
# File 'lib/ashmont/subscription.rb', line 10

def initialize(token = nil, cached_attributes = {})
  @token = token
  @cached_attributes = cached_attributes
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/ashmont/subscription.rb', line 8

def errors
  @errors
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/ashmont/subscription.rb', line 8

def token
  @token
end

Instance Method Details

#most_recent_transactionObject



43
44
45
# File 'lib/ashmont/subscription.rb', line 43

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

#next_billing_dateObject



47
48
49
# File 'lib/ashmont/subscription.rb', line 47

def next_billing_date
  .parse(remote_subscription.next_billing_date) if remote_subscription
end

#past_due?Boolean

Returns:

  • (Boolean)


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

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

#reloadObject



51
52
53
54
55
# File 'lib/ashmont/subscription.rb', line 51

def reload
  @remote_subscription = nil
  @cached_attributes = {}
  self
end

#retry_chargeObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ashmont/subscription.rb', line 31

def retry_charge
  transaction = Braintree::Subscription.retry_charge(token).transaction
  result = Braintree::Transaction.submit_for_settlement(transaction.id)
  reload
  if result.success?
    true
  else
    @errors = Ashmont::Errors.new(transaction, result.errors)
    false
  end
end

#save(attributes) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/ashmont/subscription.rb', line 22

def save(attributes)
  attributes_for_merchant = add_merchant_to_attributes(attributes)
  if token
    update(attributes_for_merchant)
  else
    create(attributes_for_merchant)
  end
end

#statusObject



18
19
20
# File 'lib/ashmont/subscription.rb', line 18

def status
  @cached_attributes[:status] || remote_status
end