Class: Braintree::CreditCardGateway

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

Returns a new instance of CreditCardGateway.


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


72
73
74
# File 'lib/braintree/credit_card_gateway.rb', line 72

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

._signature(type) ⇒ Object

:nodoc:


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/braintree/credit_card_gateway.rb', line 80

def self._signature(type) # :nodoc:
  billing_address_params = AddressGateway._shared_signature
  options = [
    :fail_on_duplicate_payment_method,
    :make_default,
    :skip_advanced_fraud_checking,
    :venmo_sdk_session,
    :verification_account_type,
    :verification_amount,
    :verification_currency_iso_code,
    :verification_merchant_account_id,
    :verify_card
  ]
  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,
    {:external_vault => [:network_transaction_id]},
    {: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
    signature << :customer_id
  when :update
    billing_address_params << {:options => [:update_existing]}
  else
    raise ArgumentError
  end

  return signature
end

._update_signatureObject

:nodoc:


76
77
78
# File 'lib/braintree/credit_card_gateway.rb', line 76

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

Instance Method Details

#_do_create(path, params = nil) ⇒ Object

:nodoc:


127
128
129
130
131
132
133
134
135
136
# File 'lib/braintree/credit_card_gateway.rb', line 127

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

#_do_update(http_verb, path, params) ⇒ Object

:nodoc:


138
139
140
141
142
143
144
145
146
147
# File 'lib/braintree/credit_card_gateway.rb', line 138

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

#_fetch_expired(ids) ⇒ Object

:nodoc:


149
150
151
152
153
# File 'lib/braintree/credit_card_gateway.rb', line 149

def _fetch_expired(ids) # :nodoc:
  response = @config.http.post("#{@config.base_merchant_path}/payment_methods/all/expired", :search => {:ids => ids})
  attributes = response[:payment_methods]
  Util.extract_attribute_as_array(attributes, :credit_card).map { |attrs| CreditCard._new(@gateway, attrs) }
end

#_fetch_expiring_between(formatted_start_date, formatted_end_date, ids) ⇒ Object

:nodoc:


155
156
157
158
159
160
161
162
# File 'lib/braintree/credit_card_gateway.rb', line 155

def _fetch_expiring_between(formatted_start_date, formatted_end_date, ids) # :nodoc:
  response = @config.http.post(
    "#{@config.base_merchant_path}/payment_methods/all/expiring?start=#{formatted_start_date}&end=#{formatted_end_date}",
    :search => {:ids => ids},
  )
  attributes = response[:payment_methods]
  Util.extract_attribute_as_array(attributes, :credit_card).map { |attrs| CreditCard._new(@gateway, attrs) }
end

#create(attributes) ⇒ Object


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

def create(attributes)
  if attributes.has_key?(:expiration_date) && (attributes.has_key?(:expiration_month) || attributes.has_key?(:expiration_year))
    raise ArgumentError.new("create with both expiration_month and expiration_year or only expiration_date")
  end
  Util.verify_keys(CreditCardGateway._create_signature, attributes)
  _do_create("/payment_methods", :credit_card => attributes)
end

#create!(*args) ⇒ Object


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

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

#credit(token, transaction_attributes) ⇒ Object


23
24
25
# File 'lib/braintree/credit_card_gateway.rb', line 23

def credit(token, transaction_attributes)
  @gateway.transaction.credit(transaction_attributes.merge(:payment_method_token => token))
end

#credit!(*args) ⇒ Object


27
28
29
# File 'lib/braintree/credit_card_gateway.rb', line 27

def credit!(*args)
  return_object_or_raise(:transaction) { credit(*args) }
end

#delete(token) ⇒ Object


31
32
33
# File 'lib/braintree/credit_card_gateway.rb', line 31

def delete(token)
  @config.http.delete("#{@config.base_merchant_path}/payment_methods/credit_card/#{token}")
end

#expired(options = {}) ⇒ Object


35
36
37
38
# File 'lib/braintree/credit_card_gateway.rb', line 35

def expired(options = {})
  response = @config.http.post("#{@config.base_merchant_path}/payment_methods/all/expired_ids")
  ResourceCollection.new(response) { |ids| _fetch_expired(ids) }
end

#expiring_between(start_date, end_date, options = {}) ⇒ Object


40
41
42
43
44
45
# File 'lib/braintree/credit_card_gateway.rb', line 40

def expiring_between(start_date, end_date, options = {})
  formatted_start_date = start_date.strftime("%m%Y")
  formatted_end_date = end_date.strftime("%m%Y")
  response = @config.http.post("#{@config.base_merchant_path}/payment_methods/all/expiring_ids?start=#{formatted_start_date}&end=#{formatted_end_date}")
  ResourceCollection.new(response) { |ids| _fetch_expiring_between(formatted_start_date, formatted_end_date, ids) }
end

#find(token) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/braintree/credit_card_gateway.rb', line 47

def find(token)
  raise ArgumentError if token.nil? || token.to_s.strip == ""
  response = @config.http.get("#{@config.base_merchant_path}/payment_methods/credit_card/#{token}")
  CreditCard._new(@gateway, response[:credit_card])
rescue NotFoundError
  raise NotFoundError, "payment method with token #{token.inspect} not found"
end

#from_nonce(nonce) ⇒ Object


55
56
57
58
59
60
61
# File 'lib/braintree/credit_card_gateway.rb', line 55

def from_nonce(nonce)
  raise ArgumentError if nonce.nil? || nonce.to_s.strip == ""
  response = @config.http.get("#{@config.base_merchant_path}/payment_methods/from_nonce/#{nonce}")
  CreditCard._new(@gateway, response[:credit_card])
rescue NotFoundError
  raise NotFoundError, "nonce #{nonce.inspect} locked, consumed, or not found"
end

#update(token, attributes) ⇒ Object


63
64
65
66
# File 'lib/braintree/credit_card_gateway.rb', line 63

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

#update!(*args) ⇒ Object


68
69
70
# File 'lib/braintree/credit_card_gateway.rb', line 68

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