Module: AuthorizeNet::TypeConversions

Included in:
Response, Transaction
Defined in:
lib/authorize_net/authorize_net.rb

Overview

Some type conversion routines that will be injected into our Transaction/Response classes.

Constant Summary collapse

API_FIELD_PREFIX =
'x_'.freeze

Instance Method Summary collapse

Instance Method Details

#boolean_to_value(bool) ⇒ Object

Converts a boolean into an Authorize.Net boolean value string. This is designed to handle the wide range of boolean formats that Authorize.Net uses. If bool isn’t a Boolean, its converted to a string and passed along.



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

def boolean_to_value(bool)
  case bool
  when TrueClass, FalseClass
    bool ? 'TRUE' : 'FALSE'
  else
    bool.to_s
  end
end

#date_to_value(date) ⇒ Object

Converts a Date (or DateTime, or Time) into an Authorize.Net date value string. If date isn’t a Date (or DateTime, or Time), its converted to a string and passed along.



65
66
67
68
69
70
71
72
# File 'lib/authorize_net/authorize_net.rb', line 65

def date_to_value(date)
  case date
  when Date, DateTime, Time
    date.strftime('%Y-%m-%d')
  else
    date.to_s
  end
end

#datetime_to_value(datetime) ⇒ Object

Converts a Date (or DateTime, or Time) into an Authorize.Net datetime value string. If date isn’t a Date (or DateTime, or Time), it’s converted to a string and passed along.



82
83
84
85
86
87
88
89
90
91
# File 'lib/authorize_net/authorize_net.rb', line 82

def datetime_to_value(datetime)
  case datetime
  when Date, DateTime
    datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%SZ')
  when Time
    datetime.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  else
    datetime.to_s
  end
end

#decimal_to_value(float) ⇒ Object

Converts a BigDecimal (or Float) into an Authorize.Net float value string. If float isn’t a BigDecimal (or Float), its converted to a string and passed along.



46
47
48
49
50
51
52
53
54
55
# File 'lib/authorize_net/authorize_net.rb', line 46

def decimal_to_value(float)
  case float
  when Float
    format("%0.2f", float)
  when BigDecimal
    float.truncate(2).to_s('F')
  else
    float.to_s
  end
end

#integer_to_value(int) ⇒ Object

Converts an Integer into an Authorize.Net integer string.



99
100
101
# File 'lib/authorize_net/authorize_net.rb', line 99

def integer_to_value(int)
  int.to_s
end

#to_external_field(key) ⇒ Object

Converts an internal field name (Symbol) into an external field name (Symbol) that can be consumed by the Authorize.Net API.



118
119
120
# File 'lib/authorize_net/authorize_net.rb', line 118

def to_external_field(key)
  (API_FIELD_PREFIX + key.to_s).to_sym
end

#to_internal_field(key) ⇒ Object

Converts an external field name (Symbol) into an internal field name (Symbol). This is the exact inverse of to_external_field. Running to_internal_field(to_external_field(:foo)) would return :foo back.



126
127
128
129
# File 'lib/authorize_net/authorize_net.rb', line 126

def to_internal_field(key)
  k_str = key.to_s
  k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
end

#to_param(key, value, key_prefix = API_FIELD_PREFIX) ⇒ Object

Converts a key value pair into a HTTP POST parameter. The key is prefixed with key_prefix when being converted to a parameter name.



105
106
107
108
109
110
111
112
113
114
# File 'lib/authorize_net/authorize_net.rb', line 105

def to_param(key, value, key_prefix = API_FIELD_PREFIX)
  key_str = "#{key_prefix}#{key}="
  if value.is_a?(Array)
    (value.collect do |v|
      key_str + CGI.escape(v.to_s)
    end).join('&')
  else
    key_str + CGI.escape(value.to_s)
  end
end

#value_to_boolean(value) ⇒ Object

Converts a value received from Authorize.Net into a boolean if possible. This is designed to handle the wide range of boolean formats that Authorize.Net uses.



13
14
15
16
17
18
19
20
21
22
# File 'lib/authorize_net/authorize_net.rb', line 13

def value_to_boolean(value)
  case value
  when "TRUE", "T", "YES", "Y", "1", "true"
    true
  when "FALSE", "F", "NO", "N", "0", "false"
    false
  else
    value
  end
end

#value_to_date(value) ⇒ Object

Converts a value received from Authorize.Net into a Date.



58
59
60
# File 'lib/authorize_net/authorize_net.rb', line 58

def value_to_date(value)
  Date.strptime(value, '%Y-%m-%d')
end

#value_to_datetime(value) ⇒ Object

Converts a value received from Authorize.Net into a DateTime.



75
76
77
# File 'lib/authorize_net/authorize_net.rb', line 75

def value_to_datetime(value)
  DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
end

#value_to_decimal(value) ⇒ Object

Converts a value received from Authorize.Net into a BigDecimal.



38
39
40
41
# File 'lib/authorize_net/authorize_net.rb', line 38

def value_to_decimal(value)
  value = 0 if value == '' # Ruby 2.4+ does not accept ""
  BigDecimal(value)
end

#value_to_integer(value) ⇒ Object

Converts a value received from Authorize.Net into an Integer.



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

def value_to_integer(value)
  value.to_s.to_i
end