Class: AuthorizeNet::AIM::Response

Inherits:
KeyValueResponse show all
Includes:
Fields
Defined in:
lib/authorize_net/aim/response.rb

Overview

The AIM response class. Handles parsing the response from the gateway.

Constant Summary collapse

@@digest =

Our MD5 digest generator.

OpenSSL::Digest::Digest.new('md5')
@@boolean_fields =

Fields to convert to/from booleans.

[:tax_exempt]
@@decimal_fields =

Fields to convert to/from BigDecimal.

[:amount, :tax, :freight, :duty, :requested, :balance_on_card]

Constants included from Fields

Fields::CP_FIELDS, Fields::FIELDS

Constants included from TypeConversions

TypeConversions::API_FIELD_PREFIX

Instance Method Summary collapse

Methods inherited from KeyValueResponse

#approved?, #custom_fields, #declined?, #error?, #fields, #held?, #response_code, #response_reason_code, #response_reason_text

Methods included from TypeConversions

#boolean_to_value, #date_to_value, #datetime_to_value, #decimal_to_value, #integer_to_value, #to_external_field, #to_internal_field, #to_param, #value_to_boolean, #value_to_date, #value_to_datetime, #value_to_decimal, #value_to_integer

Constructor Details

#initialize(raw_response, transaction) ⇒ Response

Constructs a new response object from a raw_response and the transaction that generated the raw_response. You don’t typically construct this object yourself, as AuthorizeNet::AIM::Transaction will build one for you when it makes the request to the gateway.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/authorize_net/aim/response.rb', line 21

def initialize(raw_response, transaction)
  @version = transaction.version
  raise "AuthorizeNet gem only supports AIM version 3.1" unless @version.to_s == '3.1'
  @raw_response = raw_response
  @fields = {}
  @transaction = transaction
  custom_field_names = transaction.custom_fields.keys.collect(&:to_s).sort.collect(&:to_sym)
  @custom_fields = {}
  split_on = transaction.delimiter
  if @raw_response.kind_of?(Net::HTTPOK) || @raw_response.kind_of?(Nokogiri::XML::Element)
    if @raw_response.kind_of?(Net::HTTPOK)
      raw_data = @raw_response.body 
    else
      raw_data = @raw_response.text
    end
    unless transaction.encapsulation_character.nil?
      split_on = transaction.encapsulation_character + split_on + transaction.encapsulation_character
      raw_data = raw_data[1..raw_data.length - 2]
    end
    raw_data.split(split_on).each_with_index  do |field, index|
      if transaction.cp_version.nil?
        field_desc = FIELDS
      else
        field_desc = CP_FIELDS
      end
      if index < field_desc.length
        @fields[field_desc[index]] = field
      else
        @custom_fields[custom_field_names[index - field_desc.length]] = field
      end
    end
    @fields.delete(nil)
    @fields.each do |k, v|
      if @@boolean_fields.include?(k)
        @fields[k] = value_to_boolean(v)
      elsif @@decimal_fields.include?(k)
        @fields[k] = value_to_decimal(v)
      end
    end
  end
end

Instance Method Details

#authorization_codeObject

Returns the transaction’s authorization code. This should be shown to the end user.



103
104
105
# File 'lib/authorize_net/aim/response.rb', line 103

def authorization_code
  @fields[:authorization_code]
end

#avs_responseObject

Returns a response code (from AVSResponseCode) indicating the result of any Address Verification Service checks.



120
121
122
# File 'lib/authorize_net/aim/response.rb', line 120

def avs_response
  @fields[:avs_response]
end

#card_typeObject

Returns the credit card type used in the transaction. The values returned can be found in CardType.



125
126
127
# File 'lib/authorize_net/aim/response.rb', line 125

def card_type
  @fields[:card_type]
end

#connection_failure?Boolean

Returns true if we failed to open a connection to the gateway or got back a non-200 OK HTTP response.

Returns:

  • (Boolean)


84
85
86
# File 'lib/authorize_net/aim/response.rb', line 84

def connection_failure?
  !@raw_response.kind_of?(Net::HTTPOK) && !@raw_response.kind_of?(Nokogiri::XML::Element)
end

#customer_idObject

Returns the customer id from the response.



114
115
116
# File 'lib/authorize_net/aim/response.rb', line 114

def customer_id
  @fields[:customer_id]
end

#rawObject

Returns the underlying Net::HTTPResponse object. This has the original response body along with headers and such. Note that if an exception is generated while making the request (which happens if there is no internet connection for example), you will get the exception object here instead of a Net::HTTPResponse object.



92
93
94
# File 'lib/authorize_net/aim/response.rb', line 92

def raw
  @raw_response
end

#success?Boolean

Check to see if the response indicated success. Success is defined as a 200 OK response indicating that the transaction was approved.

Returns:

  • (Boolean)


79
80
81
# File 'lib/authorize_net/aim/response.rb', line 79

def success?
  !connection_failure? && approved?
end

#transactionObject

Returns the AuthorizeNet::Transaction instance that owns this response.



97
98
99
# File 'lib/authorize_net/aim/response.rb', line 97

def transaction
  @transaction
end

#transaction_idObject

Returns the transaction’s authorization id. You will need this for future void, refund and prior authorization capture requests.



109
110
111
# File 'lib/authorize_net/aim/response.rb', line 109

def transaction_id
  @fields[:transaction_id]
end

#valid_md5?(api_login, merchant_value) ⇒ Boolean

Returns True if the MD5 hash found in the response payload validates using the supplied api_login and secret merchant_value (THIS IS NOT YOUR API KEY).

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/authorize_net/aim/response.rb', line 65

def valid_md5?(, merchant_value)
  if @fields[:md5_hash].nil?
    return false
  end
  @@digest.hexdigest("#{merchant_value}#{}#{@fields[:transaction_id]}#{@transaction.fields[:amount]}").downcase == @fields[:md5_hash].downcase
end

#versionObject

Returns the current API version that we are adhering to.



73
74
75
# File 'lib/authorize_net/aim/response.rb', line 73

def version
  @version
end