lib/braintree/subscription.rb in braintree-2.0.0 vs lib/braintree/subscription.rb in braintree-2.1.0
- old
+ new
@@ -45,11 +45,11 @@
attr_reader :transactions
def self.cancel(subscription_id)
response = Http.put "/subscriptions/#{subscription_id}/cancel"
if response[:subscription]
- SuccessfulResult.new(:subscription => new(response[:subscription]))
+ SuccessfulResult.new(:subscription => _new(response[:subscription]))
elsif response[:api_error_response]
ErrorResult.new(response[:api_error_response])
else
raise UnexpectedError, "expected :subscription or :api_error_response"
end
@@ -64,15 +64,25 @@
# Finds the subscription with the given id. Raises a Braintree::NotFoundError
# if the subscription cannot be found.
def self.find(id)
response = Http.get "/subscriptions/#{id}"
- new(response[:subscription])
+ _new(response[:subscription])
rescue NotFoundError
raise NotFoundError, "subscription with id #{id.inspect} not found"
end
+ def self.retry_charge(subscription_id, amount=nil)
+ attributes = {
+ :amount => amount,
+ :subscription_id => subscription_id,
+ :type => Transaction::Type::Sale
+ }
+
+ Transaction.send(:_do_create, "/transactions", :transaction => attributes)
+ end
+
# Allows searching on subscriptions. There are two types of fields that are searchable: text and
# multiple value fields. Searchable text fields are:
# - plan_id
# - days_past_due
#
@@ -91,19 +101,19 @@
search = SubscriptionSearch.new
block.call(search)
response = Http.post "/subscriptions/advanced_search?page=#{page}", {:search => search.to_hash}
attributes = response[:subscriptions]
- attributes[:items] = Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| new(attrs) }
+ attributes[:items] = Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| _new(attrs) }
ResourceCollection.new(attributes) { |page_number| Subscription.search(page_number, &block) }
end
def self.update(subscription_id, attributes)
Util.verify_keys(_update_signature, attributes)
response = Http.put "/subscriptions/#{subscription_id}", :subscription => attributes
if response[:subscription]
- SuccessfulResult.new(:subscription => new(response[:subscription]))
+ SuccessfulResult.new(:subscription => _new(response[:subscription]))
elsif response[:api_error_response]
ErrorResult.new(response[:api_error_response])
else
raise UnexpectedError, "expected :subscription or :api_error_response"
end
@@ -128,10 +138,18 @@
end
# True if <tt>other</tt> has the same id.
def ==(other)
+ return false unless other.is_a?(Subscription)
id == other.id
+ end
+
+ class << self
+ protected :new
+ def _new(*args) # :nodoc:
+ self.new *args
+ end
end
def self._do_create(url, params) # :nodoc:
response = Http.post url, params
if response[:subscription]