Class: Ashmont::Subscription
- Inherits:
-
Object
- Object
- Ashmont::Subscription
- Defined in:
- lib/ashmont/subscription.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(token = nil, cached_attributes = {}) ⇒ Subscription
constructor
A new instance of Subscription.
- #most_recent_transaction ⇒ Object
- #next_billing_date ⇒ Object
- #past_due? ⇒ Boolean
- #reload ⇒ Object
- #retry_charge ⇒ Object
- #save(attributes) ⇒ Object
- #status ⇒ Object
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
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/ashmont/subscription.rb', line 8 def errors @errors end |
#token ⇒ Object (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_transaction ⇒ Object
43 44 45 |
# File 'lib/ashmont/subscription.rb', line 43 def most_recent_transaction transactions.sort_by(&:created_at).last end |
#next_billing_date ⇒ Object
47 48 49 |
# File 'lib/ashmont/subscription.rb', line 47 def next_billing_date merchant_account_time_zone.parse(remote_subscription.next_billing_date) if remote_subscription end |
#past_due? ⇒ Boolean
57 58 59 |
# File 'lib/ashmont/subscription.rb', line 57 def past_due? status == Braintree::Subscription::Status::PastDue end |
#reload ⇒ Object
51 52 53 54 55 |
# File 'lib/ashmont/subscription.rb', line 51 def reload @remote_subscription = nil @cached_attributes = {} self end |
#retry_charge ⇒ Object
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 |
#status ⇒ Object
18 19 20 |
# File 'lib/ashmont/subscription.rb', line 18 def status @cached_attributes[:status] || remote_status end |