Class: Braintree::PlanGateway

Inherits:
Object
  • Object
show all
Includes:
BaseModule
Defined in:
lib/braintree/plan_gateway.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModule

included

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) ⇒ PlanGateway

Returns a new instance of PlanGateway.



5
6
7
8
9
# File 'lib/braintree/plan_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_signatureObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/braintree/plan_gateway.rb', line 86

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_signatureObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/braintree/plan_gateway.rb', line 52

def self._create_signature
  [
    :billing_day_of_month,
    :billing_frequency,
    :currency_iso_code,
    :description,
    :id,
    :merchant_id,
    :name,
    :number_of_billing_cycles,
    :price,
    :trial_duration,
    :trial_duration_unit,
    :trial_period
  ] + _add_on_discount_signature
end

._update_signatureObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/braintree/plan_gateway.rb', line 69

def self._update_signature
  [
    :billing_day_of_month,
    :billing_frequency,
    :currency_iso_code,
    :description,
    :id,
    :merchant_id,
    :name,
    :number_of_billing_cycles,
    :price,
    :trial_duration,
    :trial_duration_unit,
    :trial_period
  ] + _add_on_discount_signature
end

Instance Method Details

#_do_create(path, params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/braintree/plan_gateway.rb', line 105

def _do_create(path, params)
  response = @config.http.post("#{@config.base_merchant_path}#{path}", params)
  if response[:plan]
    SuccessfulResult.new(:plan => Plan._new(@gateway, response[:plan]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise UnexpectedError, "expected :plan or :api_error_response"
  end
end

#allObject



11
12
13
14
15
16
17
# File 'lib/braintree/plan_gateway.rb', line 11

def all
  response = @config.http.get("#{@config.base_merchant_path}/plans")
  attributes_collection = response[:plans] || []
  attributes_collection.map do |attributes|
    Plan._new(@gateway, attributes)
  end
end

#create(attributes) ⇒ Object



19
20
21
22
# File 'lib/braintree/plan_gateway.rb', line 19

def create(attributes)
  Util.verify_keys(PlanGateway._create_signature, attributes)
  _do_create "/plans", :plan => attributes
end

#create!(*args) ⇒ Object



24
25
26
# File 'lib/braintree/plan_gateway.rb', line 24

def create!(*args)
  return_object_or_raise(:plan) { create(*args) }
end

#find(id) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/braintree/plan_gateway.rb', line 28

def find(id)
  raise ArgumentError if id.nil? || id.to_s.strip == ""
  response = @config.http.get("#{@config.base_merchant_path}/plans/#{id}")
  Plan._new(@gateway, response[:plan])
rescue NotFoundError
  raise NotFoundError, "plan with id #{id.inspect} not found"
end

#update(plan_id, attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/braintree/plan_gateway.rb', line 36

def update(plan_id, attributes)
  Util.verify_keys(PlanGateway._update_signature, attributes)
  response = @config.http.put("#{@config.base_merchant_path}/plans/#{plan_id}", :plan => attributes)
  if response[:plan]
    SuccessfulResult.new(:plan => Plan._new(@gateway, response[:plan]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise UnexpectedError, "expected :plan or :api_error_response"
  end
end

#update!(*args) ⇒ Object



48
49
50
# File 'lib/braintree/plan_gateway.rb', line 48

def update!(*args)
  return_object_or_raise(:plan) { update(*args) }
end