Class: Braintree::PaymentMethodGateway

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

Overview

:nodoc:

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

Returns a new instance of PaymentMethodGateway.


5
6
7
8
9
# File 'lib/braintree/payment_method_gateway.rb', line 5

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
  @config.assert_has_access_token_or_keys
end

Class Method Details

._create_signatureObject

:nodoc:


135
136
137
# File 'lib/braintree/payment_method_gateway.rb', line 135

def self._create_signature # :nodoc:
  _signature(:create)
end

._delete_signatureObject

:nodoc:


143
144
145
# File 'lib/braintree/payment_method_gateway.rb', line 143

def self._delete_signature # :nodoc:
  [:revoke_all_grants]
end

._signature(type) ⇒ Object

:nodoc:


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/braintree/payment_method_gateway.rb', line 147

def self._signature(type) # :nodoc:
  billing_address_params = AddressGateway._shared_signature
  paypal_options_shipping_signature = AddressGateway._shared_signature
  options = [
    :make_default,
    :skip_advanced_fraud_checking,
    :us_bank_account_verification_method,
    :venmo_sdk_session,
    :verification_account_type,
    :verification_amount,
    :verification_currency_iso_code,
    :verification_merchant_account_id,
    :verify_card,
    :paypal => [
      :payee_email,
      :order_id,
      :custom_field,
      :description,
      :amount,
      {:shipping => paypal_options_shipping_signature}
    ],
  ]
  signature = [
    :billing_address_id, :cardholder_name, :cvv, :expiration_date, :expiration_month,
    :expiration_year, :number, :token, :venmo_sdk_payment_method_code, :device_data,
    :payment_method_nonce,
    {:options => options},
    {:billing_address => billing_address_params}
  ]
  signature << {
    :three_d_secure_pass_thru => [
      :eci_flag,
      :cavv,
      :xid,
      :three_d_secure_version,
      :authentication_response,
      :directory_response,
      :cavv_algorithm,
      :ds_transaction_id,
    ]
  }

  case type
  when :create
    options << :fail_on_duplicate_payment_method
    signature << :customer_id
    signature << :paypal_refresh_token
  when :update
    billing_address_params << {:options => [:update_existing]}
  else
    raise ArgumentError
  end

  return signature
end

._update_signatureObject

:nodoc:


139
140
141
# File 'lib/braintree/payment_method_gateway.rb', line 139

def self._update_signature # :nodoc:
  _signature(:update)
end

Instance Method Details

#_do_create(path, params = nil) ⇒ Object

:nodoc:


20
21
22
23
24
25
26
27
28
29
# File 'lib/braintree/payment_method_gateway.rb', line 20

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

#_do_grant(path, params = nil) ⇒ Object


100
101
102
103
104
105
106
107
108
109
# File 'lib/braintree/payment_method_gateway.rb', line 100

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

#_do_revoke(path, params = nil) ⇒ Object


124
125
126
127
128
129
130
131
132
133
# File 'lib/braintree/payment_method_gateway.rb', line 124

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

#_do_update(http_verb, path, params) ⇒ Object

:nodoc:


69
70
71
72
73
74
75
76
77
78
# File 'lib/braintree/payment_method_gateway.rb', line 69

def _do_update(http_verb, path, params) # :nodoc:
  response = @config.http.send(http_verb, "#{@config.base_merchant_path}#{path}", params)
  if response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  elsif response
    SuccessfulResult.new(:payment_method => PaymentMethodParser.parse_payment_method(@gateway, response))
  else
    raise UnexpectedError, "expected :payment_method or :api_error_response"
  end
end

#create(attributes) ⇒ Object


11
12
13
14
# File 'lib/braintree/payment_method_gateway.rb', line 11

def create(attributes)
  Util.verify_keys(PaymentMethodGateway._create_signature, attributes)
  _do_create("/payment_methods", :payment_method => attributes)
end

#create!(*args) ⇒ Object


16
17
18
# File 'lib/braintree/payment_method_gateway.rb', line 16

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

#delete(token, options = {}) ⇒ Object


31
32
33
34
35
36
# File 'lib/braintree/payment_method_gateway.rb', line 31

def delete(token, options = {})
  Util.verify_keys(PaymentMethodGateway._delete_signature, options)
  query_param = "?" + Util.hash_to_query_string(options) if not options.empty?
  @config.http.delete("#{@config.base_merchant_path}/payment_methods/any/#{token}#{query_param}")
  SuccessfulResult.new
end

#find(token) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/braintree/payment_method_gateway.rb', line 38

def find(token)
  raise ArgumentError if token.nil? || token.to_s.strip == ""
  response = @config.http.get("#{@config.base_merchant_path}/payment_methods/any/#{token}")
  if response.has_key?(:credit_card)
    CreditCard._new(@gateway, response[:credit_card])
  elsif response.has_key?(:paypal_account)
    PayPalAccount._new(@gateway, response[:paypal_account])
  elsif response.has_key?(:us_bank_account)
    UsBankAccount._new(@gateway, response[:us_bank_account])
  elsif response.has_key?(:apple_pay_card)
    ApplePayCard._new(@gateway, response[:apple_pay_card])
  elsif response.has_key?(:android_pay_card)
    GooglePayCard._new(@gateway, response[:android_pay_card])
  elsif response.has_key?(:venmo_account)
    VenmoAccount._new(@gateway, response[:venmo_account])
  else
    UnknownPaymentMethod._new(@gateway, response)
  end
rescue NotFoundError
  raise NotFoundError, "payment method with token #{token.inspect} not found"
end

#grant(token, options = {}) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/braintree/payment_method_gateway.rb', line 80

def grant(token, options = {})
  raise ArgumentError if token.nil? || token.to_s.strip == ""
  if  options.class == Hash
    grant_options = options
  elsif [true, false].include?(options)
    grant_options = {:allow_vaulting => options}
  else
    raise ArgumentError
  end

  _do_grant(
    "/payment_methods/grant",
    :payment_method => {
      :shared_payment_method_token => token,
    }.merge(grant_options),
  )
rescue NotFoundError
  raise NotFoundError, "payment method with token #{token.inspect} not found"
end

#revoke(token) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/braintree/payment_method_gateway.rb', line 111

def revoke(token)
  raise ArgumentError if token.nil? || token.to_s.strip == ""

  _do_revoke(
    "/payment_methods/revoke",
    :payment_method => {
      :shared_payment_method_token => token
    },
  )
rescue NotFoundError
  raise NotFoundError, "payment method with token #{token.inspect} not found"
end

#update(token, attributes) ⇒ Object


60
61
62
63
# File 'lib/braintree/payment_method_gateway.rb', line 60

def update(token, attributes)
  Util.verify_keys(PaymentMethodGateway._update_signature, attributes)
  _do_update(:put, "/payment_methods/any/#{token}", :payment_method => attributes)
end

#update!(*args) ⇒ Object


65
66
67
# File 'lib/braintree/payment_method_gateway.rb', line 65

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