Class: AuthorizeNet::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/authorize_net/error_handler.rb

Constant Summary collapse

ERROR_FIELD_REGEXES =
[
  /'AnetApi\/xml\/v1\/schema\/AnetApiSchema\.xsd:([a-zA-Z]*)'/,
  /The element '([a-zA-Z]*)' in namespace 'AnetApi\/xml\/v1\/schema\/AnetApiSchema.xsd'/
]
MESSAGE_CODES =
{
  "E00003" => :invalid_field,
  "E00015" => :invalid_field_length,
  "E00027" => :missing_required_field,
  "E00039" => :duplicate_record_exists,
  "E00041" => :customer_profile_info_required,
}
ERROR_CODES =
{
  "210" => :transaction_declined,
  "6" => :invalid_card_number,
  "7" => :invalid_expiration_date,
  "8" => :expired_credit_card,
}
ERROR_FIELDS =
{
  :invalid_card_number => :card_number,
  :invalid_expiration_date => :card_expiration,
  :expired_credit_card => :card_expiration,

  "cardNumber" => :card_number,
  "expirationDate" => :card_expiration,
  "cardCode" => :card_security_code,
}

Class Method Summary collapse

Class Method Details

.buildError(error) ⇒ Object

Attempts to determine the error type and field for an error hash

Parameters:

  • Hash

    error

Returns:

  • Hash error



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/authorize_net/error_handler.rb', line 75

def buildError(error)
  code = error[:code]
  text = error[:text]
  type = getTypeFromCode(code)
  field = nil

  if !type.nil? and ERROR_FIELDS.has_key? type
    field = ERROR_FIELDS[type]
  else
    field = getFieldFromText(text)
  end

  return {
    :code => code,
    :text => text,
    :type => type,
    :field => field,
  }
end

.getFieldFromText(text) ⇒ Object

Attempts to determine the error field given an error message

Parameters:

  • String

    text

Returns:

  • Symbol|nil field



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/authorize_net/error_handler.rb', line 116

def getFieldFromText(text)
  if text.nil?
    return nil
  end

  ERROR_FIELD_REGEXES.each do |regex|
    field_match = text.match(regex)
    if !field_match.nil?
      field = field_match[1]

      if ERROR_FIELDS.keys.include? field
        return ERROR_FIELDS[field]
      end
      return field
    end
  end

  return nil
end

.getTypeFromCode(code) ⇒ Object

Attempts to determine the error type given an error code

Parameters:

  • String

    code

Returns:

  • Symbol|nil type



101
102
103
104
105
106
107
108
# File 'lib/authorize_net/error_handler.rb', line 101

def getTypeFromCode(code)
  if ERROR_CODES.has_key? code
    return ERROR_CODES[code]
  elsif MESSAGE_CODES.has_key? code
    return MESSAGE_CODES[code]
  end
  return nil
end

.handle(response) ⇒ Object

Creates an exception, populates it as well as possible, and then raises it

Parameters:

  • AuthorizeNet::Response


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/authorize_net/error_handler.rb', line 40

def handle(response)
  exception = AuthorizeNet::Exception.new

  if !response.errors.nil?
    first_error = response.errors.first
    exception.message = first_error[:text]

    # Add errors to exception
    response.errors.each do |error|
      exception.errors << buildError(error)
    end

    raise exception

  # If there are no errors, then the "messages" are probably errors... *sigh*
  elsif !response.messages.nil? and response.result == AuthorizeNet::RESULT_ERROR
    first_msg = response.messages.first
    exception.message = first_msg[:text]

    # Add messages (that are sometimes actually errors) to exception
    response.messages.each do |msg|
      exception.errors << buildError(msg)
    end

    raise exception
  end

end