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.new('md5')
@@boolean_fields =

Fields to convert to/from booleans.

[:tax_exempt]
@@decimal_fields =

Fields to convert to/from BigDecimal.

%i[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 Attribute Summary collapse

Attributes inherited from KeyValueResponse

#custom_fields, #fields

Instance Method Summary collapse

Methods inherited from KeyValueResponse

#approved?, #declined?, #error?, #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.



18
19
20
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
# File 'lib/authorize_net/aim/response.rb', line 18

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.is_a?(Net::HTTPOK) || @raw_response.is_a?(Nokogiri::XML::Element)
    if @raw_response.is_a?(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 Attribute Details

#transactionObject (readonly)

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



90
91
92
# File 'lib/authorize_net/aim/response.rb', line 90

def transaction
  @transaction
end

#versionObject (readonly)

Returns the current API version that we are adhering to.



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

def version
  @version
end

Instance Method Details

#authorization_codeObject

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



94
95
96
# File 'lib/authorize_net/aim/response.rb', line 94

def authorization_code
  @fields[:authorization_code]
end

#avs_responseObject

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



111
112
113
# File 'lib/authorize_net/aim/response.rb', line 111

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.



116
117
118
# File 'lib/authorize_net/aim/response.rb', line 116

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)


77
78
79
# File 'lib/authorize_net/aim/response.rb', line 77

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

#customer_idObject

Returns the customer id from the response.



105
106
107
# File 'lib/authorize_net/aim/response.rb', line 105

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.



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

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)


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

def success?
  !connection_failure? && approved?
end

#transaction_idObject

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



100
101
102
# File 'lib/authorize_net/aim/response.rb', line 100

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)


62
63
64
65
# File 'lib/authorize_net/aim/response.rb', line 62

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