Class: Braintree::PaymentMethodGateway

Inherits:
Object
  • Object
show all
Includes:
BaseModule
Defined in:
lib/braintree/payment_method_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) ⇒ 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



145
146
147
# File 'lib/braintree/payment_method_gateway.rb', line 145

def self._create_signature
  _signature(:create)
end

._delete_signatureObject



153
154
155
# File 'lib/braintree/payment_method_gateway.rb', line 153

def self._delete_signature
  [:revoke_all_grants]
end

._signature(type) ⇒ Object



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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/braintree/payment_method_gateway.rb', line 157

def self._signature(type)
  billing_address_params = AddressGateway._shared_signature
  paypal_options_shipping_signature = AddressGateway._shared_signature
  # NEXT_MAJOR_VERSION Remove venmo_sdk_session
  # The old venmo SDK class has been deprecated
  options = [
    :make_default,
    :skip_advanced_fraud_checking,
    :us_bank_account_verification_method,
    :venmo_sdk_session, # Deprecated
    :verification_account_type,
    :verification_add_ons,
    :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}
    ],
  ]
  # NEXT_MAJOR_VERSION Remove venmo_sdk_payment_method_code
  # The old venmo SDK class has been deprecated
  signature = [
    :billing_address_id, :cardholder_name, :cvv, :expiration_date, :expiration_month,
    :expiration_year, :number, :token, :venmo_sdk_payment_method_code, # Deprecated
    :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



149
150
151
# File 'lib/braintree/payment_method_gateway.rb', line 149

def self._update_signature
  _signature(:update)
end

Instance Method Details

#_do_create(path, params = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/braintree/payment_method_gateway.rb', line 24

def _do_create(path, params=nil)
  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



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

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



134
135
136
137
138
139
140
141
142
143
# File 'lib/braintree/payment_method_gateway.rb', line 134

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



79
80
81
82
83
84
85
86
87
88
# File 'lib/braintree/payment_method_gateway.rb', line 79

def _do_update(http_verb, path, params)
  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
15
16
17
18
# File 'lib/braintree/payment_method_gateway.rb', line 11

def create(attributes)
  # NEXT_MAJOR_VERSION remove this check
  if attributes.has_key?(:venmo_sdk_payment_method_code) || attributes.has_key?(:venmo_sdk_session)
    warn "[DEPRECATED] The Venmo SDK integration is Unsupported. Please update your integration to use Pay with Venmo instead."
  end
  Util.verify_keys(PaymentMethodGateway._create_signature, attributes)
  _do_create("/payment_methods", :payment_method => attributes)
end

#create!(*args) ⇒ Object



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

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

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



35
36
37
38
39
40
# File 'lib/braintree/payment_method_gateway.rb', line 35

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/braintree/payment_method_gateway.rb', line 42

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])
  elsif response.has_key?(:sepa_debit_account)
    SepaDirectDebitAccount._new(@gateway, response[:sepa_debit_account])
  else
    UnknownPaymentMethod._new(@gateway, response)
  end
rescue NotFoundError
  raise NotFoundError, "payment method with token #{token.inspect} not found"
end

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/braintree/payment_method_gateway.rb', line 90

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



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

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



66
67
68
69
70
71
72
73
# File 'lib/braintree/payment_method_gateway.rb', line 66

def update(token, attributes)
  # NEXT_MAJOR_VERSION remove this check
  if attributes.has_key?(:venmo_sdk_payment_method_code) || attributes.has_key?(:venmo_sdk_session)
    warn "[DEPRECATED] The Venmo SDK integration is Unsupported. Please update your integration to use Pay with Venmo instead."
  end
  Util.verify_keys(PaymentMethodGateway._update_signature, attributes)
  _do_update(:put, "/payment_methods/any/#{token}", :payment_method => attributes)
end

#update!(*args) ⇒ Object



75
76
77
# File 'lib/braintree/payment_method_gateway.rb', line 75

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