Class: Braintree::SubscriptionGateway
- Inherits:
-
Object
- Object
- Braintree::SubscriptionGateway
- Includes:
- BaseModule
- Defined in:
- lib/braintree/subscription_gateway.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_do_create(path, params) ⇒ Object
- #_fetch_subscriptions(search, ids) ⇒ Object
- #cancel(subscription_id) ⇒ Object
- #cancel!(*args) ⇒ Object
- #create(attributes) ⇒ Object
- #create!(*args) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(gateway) ⇒ SubscriptionGateway
constructor
A new instance of SubscriptionGateway.
- #retry_charge(subscription_id, amount = nil, submit_for_settlement = false) ⇒ Object
- #search(&block) ⇒ Object
- #update(subscription_id, attributes) ⇒ Object
- #update!(*args) ⇒ Object
Methods included from BaseModule
Methods included from BaseModule::Methods
#copy_instance_variables_from_object, #return_object_or_raise, #set_instance_variables_from_hash, #singleton_class
Constructor Details
#initialize(gateway) ⇒ SubscriptionGateway
Returns a new instance of SubscriptionGateway.
5 6 7 8 9 |
# File 'lib/braintree/subscription_gateway.rb', line 5 def initialize(gateway) @gateway = gateway @config = gateway.config @config.assert_has_access_token_or_keys end |
Class Method Details
._add_on_discount_signature ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/braintree/subscription_gateway.rb', line 117 def self._add_on_discount_signature [ { :add_ons => [ {:add => [:amount, :inherited_from_id, :never_expires, :number_of_billing_cycles, :quantity]}, {:update => [:amount, :existing_id, :never_expires, :number_of_billing_cycles, :quantity]}, {:remove => [:_any_key_]} ] }, { :discounts => [ {:add => [:amount, :inherited_from_id, :never_expires, :number_of_billing_cycles, :quantity]}, {:update => [:amount, :existing_id, :never_expires, :number_of_billing_cycles, :quantity]}, {:remove => [:_any_key_]} ] } ] end |
._create_signature ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/braintree/subscription_gateway.rb', line 73 def self._create_signature [ :billing_day_of_month, :first_billing_date, :id, :merchant_account_id, :never_expires, :number_of_billing_cycles, :payment_method_token, :payment_method_nonce, :plan_id, :price, :trial_duration, :trial_duration_unit, :trial_period, {:options => [ :do_not_inherit_add_ons_or_discounts, :start_immediately, {:paypal => [:description]}, ]}, {:descriptor => [:name, :phone, :url]} ] + _add_on_discount_signature end |
._update_signature ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/braintree/subscription_gateway.rb', line 97 def self._update_signature [ :id, :merchant_account_id, :never_expires, :number_of_billing_cycles, :payment_method_token, :payment_method_nonce, :plan_id, :price, {:options => [ :prorate_charges, :replace_all_add_ons_and_discounts, :revert_subscription_on_proration_failure, {:paypal => [:description]}, ]}, {:descriptor => [:name, :phone, :url]} ] + _add_on_discount_signature end |
Instance Method Details
#_do_create(path, params) ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/braintree/subscription_gateway.rb', line 136 def _do_create(path, params) response = @config.http.post("#{@config.base_merchant_path}#{path}", params) if response[:subscription] SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription])) elsif response[:api_error_response] ErrorResult.new(@gateway, response[:api_error_response]) else raise UnexpectedError, "expected :subscription or :api_error_response" end end |
#_fetch_subscriptions(search, ids) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/braintree/subscription_gateway.rb', line 147 def _fetch_subscriptions(search, ids) search.ids.in ids response = @config.http.post("#{@config.base_merchant_path}/subscriptions/advanced_search", {:search => search.to_hash}) attributes = response[:subscriptions] Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| Subscription._new(@gateway, attrs) } end |
#cancel(subscription_id) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/braintree/subscription_gateway.rb', line 11 def cancel(subscription_id) response = @config.http.put("#{@config.base_merchant_path}/subscriptions/#{subscription_id}/cancel") if response[:subscription] SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription])) elsif response[:api_error_response] ErrorResult.new(@gateway, response[:api_error_response]) else raise UnexpectedError, "expected :subscription or :api_error_response" end rescue NotFoundError raise NotFoundError, "subscription with id #{subscription_id.inspect} not found" end |
#cancel!(*args) ⇒ Object
24 25 26 |
# File 'lib/braintree/subscription_gateway.rb', line 24 def cancel!(*args) return_object_or_raise(:subscription) { cancel(*args) } end |
#create(attributes) ⇒ Object
28 29 30 31 |
# File 'lib/braintree/subscription_gateway.rb', line 28 def create(attributes) Util.verify_keys(SubscriptionGateway._create_signature, attributes) _do_create "/subscriptions", :subscription => attributes end |
#create!(*args) ⇒ Object
33 34 35 |
# File 'lib/braintree/subscription_gateway.rb', line 33 def create!(*args) return_object_or_raise(:subscription) { create(*args) } end |
#find(id) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/braintree/subscription_gateway.rb', line 37 def find(id) raise ArgumentError if id.nil? || id.to_s.strip == "" response = @config.http.get("#{@config.base_merchant_path}/subscriptions/#{id}") Subscription._new(@gateway, response[:subscription]) rescue NotFoundError raise NotFoundError, "subscription with id #{id.inspect} not found" end |
#retry_charge(subscription_id, amount = nil, submit_for_settlement = false) ⇒ Object
45 46 47 |
# File 'lib/braintree/subscription_gateway.rb', line 45 def retry_charge(subscription_id, amount=nil, submit_for_settlement=false) @gateway.transaction.retry_subscription_charge(subscription_id, amount, submit_for_settlement) end |
#search(&block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/braintree/subscription_gateway.rb', line 49 def search(&block) search = SubscriptionSearch.new block.call(search) if block response = @config.http.post("#{@config.base_merchant_path}/subscriptions/advanced_search_ids", {:search => search.to_hash}) ResourceCollection.new(response) { |ids| _fetch_subscriptions(search, ids) } end |
#update(subscription_id, attributes) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/braintree/subscription_gateway.rb', line 57 def update(subscription_id, attributes) Util.verify_keys(SubscriptionGateway._update_signature, attributes) response = @config.http.put("#{@config.base_merchant_path}/subscriptions/#{subscription_id}", :subscription => attributes) if response[:subscription] SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription])) elsif response[:api_error_response] ErrorResult.new(@gateway, response[:api_error_response]) else raise UnexpectedError, "expected :subscription or :api_error_response" end end |
#update!(*args) ⇒ Object
69 70 71 |
# File 'lib/braintree/subscription_gateway.rb', line 69 def update!(*args) return_object_or_raise(:subscription) { update(*args) } end |