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

Constants included from Fields

Fields::FIELDS

Constants included from TypeConversions

TypeConversions::API_FIELD_PREFIX

Instance Attribute Summary

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) ⇒ 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")


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

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.



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

def authorization_code
  @fields[:auth_code]
end

#avs_responseObject

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



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

def avs_response
  @fields[:avs_code]
end

#customer_idObject

Returns the customer id from the response.



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

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.



36
37
38
39
40
41
42
43
# File 'lib/authorize_net/sim/response.rb', line 36

def direct_post_reply(url, options = {})
  url = direct_post_url(url, options[:include]) if options.key?(:include)
  js_url = url.tr("'", '\'')
  html_url = url.gsub('&', '&').tr('"', "\"")
  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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/authorize_net/sim/response.rb', line 48

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.empty?
      parsed_url.query = parsed_url.query.chomp('&') + '&'
    end
    parsed_url.query += ((fields.select { |k| @fields.key?(k) }).collect { |k| 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)


78
79
80
# File 'lib/authorize_net/sim/response.rb', line 78

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.



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

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)


27
28
29
30
# File 'lib/authorize_net/sim/response.rb', line 27

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