class AuthorizeNet::AIM::Response
The AIM
response class. Handles parsing the response from the gateway.
Public Class Methods
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.
# 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
Public Instance Methods
Returns a response code (from AVSResponseCode) indicating the result of any Address
Verification Service checks.
# File lib/authorize_net/aim/response.rb, line 120 def avs_response @fields[:avs_response] end
Returns the credit card type used in the transaction. The values returned can be found in CardType.
# File lib/authorize_net/aim/response.rb, line 125 def card_type @fields[:card_type] end
Returns true if we failed to open a connection to the gateway or got back a non-200 OK HTTP response.
# 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
Returns the customer id from the response.
# File lib/authorize_net/aim/response.rb, line 114 def customer_id @fields[:customer_id] end
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.
# File lib/authorize_net/aim/response.rb, line 92 def raw @raw_response end
Check to see if the response indicated success. Success is defined as a 200 OK response indicating that the transaction was approved.
# File lib/authorize_net/aim/response.rb, line 79 def success? !connection_failure? && approved? end
Returns the AuthorizeNet::Transaction
instance that owns this response.
# File lib/authorize_net/aim/response.rb, line 97 def transaction @transaction end
Returns the transaction’s authorization id. You will need this for future void, refund and prior authorization capture requests.
# File lib/authorize_net/aim/response.rb, line 109 def transaction_id @fields[:transaction_id] end
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).
# File lib/authorize_net/aim/response.rb, line 65 def valid_md5?(api_login, merchant_value) if @fields[:md5_hash].nil? return false end @@digest.hexdigest("#{merchant_value}#{api_login}#{@fields[:transaction_id]}#{@transaction.fields[:amount]}").downcase == @fields[:md5_hash].downcase end
Returns the current API version that we are adhering to.
# File lib/authorize_net/aim/response.rb, line 73 def version @version end