Class: AuthorizeNet::SIM::Response

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

Overview

The SIM response class. Handles parsing the response from the gateway. Also provides a few relay response helpers used to implement Direct Post Method.

Constant Summary collapse

@@digest =

Our MD5 digest generator.

OpenSSL::Digest::Digest.new('md5')

Constants included from 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) ⇒ Response

Constructs a new response object from a raw_response. Provides utility methods for validating the response as authentic, and for handling the Direct Post Method relay response.

raw_response

The raw response, either a string in POST body or GET query string format, or a hash of key/value pairs.

Typical usage:

response = AuthorizeNet::SIM::Response("x_first_name=John&x_last_name=Doe")


20
21
22
23
24
25
# File 'lib/authorize_net/sim/response.rb', line 20

def initialize(raw_response)
  @raw_response = raw_response
  @custom_fields = {}
  @fields = {}
  parse_response(@raw_response)
end

Instance Method Details

#authorization_codeObject

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



89
90
91
# File 'lib/authorize_net/sim/response.rb', line 89

def authorization_code
  @fields[:auth_code]
end

#avs_responseObject

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



106
107
108
# File 'lib/authorize_net/sim/response.rb', line 106

def avs_response
  @fields[:avs_code]
end

#customer_idObject

Returns the customer id from the response.



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

def customer_id
  @fields[:cust_id]
end

#direct_post_reply(url, options = {}) ⇒ Object

Returns an HTML string that can be returned to the gateway during the Relay Response, and will send the user on to URL you specify. Takes a hash of options, currently the only option is :include, which can be True to include all fields returned in the response as query string parameters, or it can be an array of fields to include.



41
42
43
44
45
46
47
48
# File 'lib/authorize_net/sim/response.rb', line 41

def direct_post_reply(url, options = {})
  url = direct_post_url(url, options[:include]) if options.has_key?(:include)
  js_url = url.gsub("'", '\'')
  html_url = url.gsub('&', '&').gsub('"', "\"")
  html = <<-HTML
<html><head><script type="text/javascript" charset="utf-8">window.location='#{js_url}';</script><noscript><meta http-equiv="refresh" content="1;url=#{html_url}"></noscript></head><body></body></html>
HTML
end

#direct_post_url(base_url, include_fields = true) ⇒ Object

Returns an URL with the fields found in the response and specified in include_fields attached as part of the URL’s query string. If you pass true instead of an array of fields, all fields will be attached.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/authorize_net/sim/response.rb', line 53

def direct_post_url(base_url, include_fields = true)
  url = base_url
  if include_fields
    fields = []
    case include_fields
    when TrueClass
      fields = FIELDS.collect do |k|
        k_str = k.to_s
        k_str[2..k_str.length].to_sym
      end
    when Array
      fields = include_fields
    else
      fields = include_fields.to_a
    end
    parsed_url = URI.parse(url)
    if parsed_url.query.nil?
      parsed_url.query = ''
    elsif parsed_url.query.length != 0
      parsed_url.query = parsed_url.query.chomp('&') + '&'
    end
    parsed_url.query += ((fields.select { |k| @fields.has_key?(k) }).collect { |k| self.to_param(k, @fields[k]) }).join('&')
    parsed_url.query.chomp('&')
    url = parsed_url.to_s
  end
  url
end

#success?(api_login, merchant_value) ⇒ Boolean

Check to see if the response indicated success. Success is defined as a valid MD5 hash and an response code of AuthorizeNet::Response::ResponseCode::APPROVED.

Returns:

  • (Boolean)


83
84
85
# File 'lib/authorize_net/sim/response.rb', line 83

def success?(, merchant_value)
  valid_md5?(, merchant_value) && approved?
end

#transaction_idObject

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



95
96
97
# File 'lib/authorize_net/sim/response.rb', line 95

def transaction_id
  @fields[:trans_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)


30
31
32
33
34
35
# File 'lib/authorize_net/sim/response.rb', line 30

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