authorizenet 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,154 +1,154 @@
1
- # :title: Authorize.Net Ruby SDK
2
- # The core AuthoizeNet module.
3
- # The entire SDK is name-spaced inside of this module.
4
- module AuthorizeNet
5
- # Some type conversion routines that will be injected into our
6
- # Transaction/Response classes.
7
- module TypeConversions
8
- API_FIELD_PREFIX = 'x_'.freeze
9
-
10
- # Converts a value received from Authorize.Net into a boolean if
11
- # possible. This is designed to handle the wide range of boolean
12
- # formats that Authorize.Net uses.
13
- def value_to_boolean(value)
14
- case value
15
- when "TRUE", "T", "YES", "Y", "1", "true"
16
- true
17
- when "FALSE", "F", "NO", "N", "0", "false"
18
- false
19
- else
20
- value
21
- end
22
- end
23
-
24
- # Converts a boolean into an Authorize.Net boolean value string.
25
- # This is designed to handle the wide range of boolean formats that
26
- # Authorize.Net uses. If bool isn't a Boolean, its converted to a
27
- # string and passed along.
28
- def boolean_to_value(bool)
29
- case bool
30
- when TrueClass, FalseClass
31
- bool ? 'TRUE' : 'FALSE'
32
- else
33
- bool.to_s
34
- end
35
- end
36
-
37
- # Converts a value received from Authorize.Net into a BigDecimal.
38
- def value_to_decimal(value)
39
- value = 0 if value == '' # Ruby 2.4+ does not accept ""
40
- BigDecimal(value)
41
- end
42
-
43
- # Converts a BigDecimal (or Float) into an Authorize.Net float value
44
- # string. If float isn't a BigDecimal (or Float), its converted to a
45
- # string and passed along.
46
- def decimal_to_value(float)
47
- case float
48
- when Float
49
- format("%0.2f", float)
50
- when BigDecimal
51
- float.truncate(2).to_s('F')
52
- else
53
- float.to_s
54
- end
55
- end
56
-
57
- # Converts a value received from Authorize.Net into a Date.
58
- def value_to_date(value)
59
- Date.strptime(value, '%Y-%m-%d')
60
- end
61
-
62
- # Converts a Date (or DateTime, or Time) into an Authorize.Net date
63
- # value string. If date isn't a Date (or DateTime, or Time), its
64
- # converted to a string and passed along.
65
- def date_to_value(date)
66
- case date
67
- when Date, DateTime, Time
68
- date.strftime('%Y-%m-%d')
69
- else
70
- date.to_s
71
- end
72
- end
73
-
74
- # Converts a value received from Authorize.Net into a DateTime.
75
- def value_to_datetime(value)
76
- DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
77
- end
78
-
79
- # Converts a Date (or DateTime, or Time) into an Authorize.Net datetime
80
- # value string. If date isn't a Date (or DateTime, or Time), it's
81
- # converted to a string and passed along.
82
- def datetime_to_value(datetime)
83
- case datetime
84
- when Date, DateTime
85
- datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%SZ')
86
- when Time
87
- datetime.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
88
- else
89
- datetime.to_s
90
- end
91
- end
92
-
93
- # Converts a value received from Authorize.Net into an Integer.
94
- def value_to_integer(value)
95
- value.to_s.to_i
96
- end
97
-
98
- # Converts an Integer into an Authorize.Net integer string.
99
- def integer_to_value(int)
100
- int.to_s
101
- end
102
-
103
- # Converts a key value pair into a HTTP POST parameter. The key is
104
- # prefixed with key_prefix when being converted to a parameter name.
105
- def to_param(key, value, key_prefix = API_FIELD_PREFIX)
106
- key_str = "#{key_prefix}#{key}="
107
- if value.is_a?(Array)
108
- (value.collect do |v|
109
- key_str + CGI.escape(v.to_s)
110
- end).join('&')
111
- else
112
- key_str + CGI.escape(value.to_s)
113
- end
114
- end
115
-
116
- # Converts an internal field name (Symbol) into an external field
117
- # name (Symbol) that can be consumed by the Authorize.Net API.
118
- def to_external_field(key)
119
- (API_FIELD_PREFIX + key.to_s).to_sym
120
- end
121
-
122
- # Converts an external field name (Symbol) into an internal field
123
- # name (Symbol). This is the exact inverse of to_external_field.
124
- # Running to_internal_field(to_external_field(:foo)) would return
125
- # :foo back.
126
- def to_internal_field(key)
127
- k_str = key.to_s
128
- k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
129
- end
130
- end
131
-
132
- # Provides some basic methods used by the various model classes.
133
- module Model
134
- # The constructor for models. Takes any of the supported attributes
135
- # as key/value pairs.
136
- def initialize(fields = {})
137
- fields.each do |k, v|
138
- method_name = (k.to_s + '=').to_sym
139
- send(method_name, v) if respond_to?(method_name)
140
- end
141
- end
142
-
143
- def to_a
144
- [self]
145
- end
146
-
147
- #:enddoc:
148
- protected
149
-
150
- def handle_multivalue_hashing(obj)
151
- obj.to_a.collect(&:to_hash)
152
- end
153
- end
154
- end
1
+ # :title: Authorize.Net Ruby SDK
2
+ # The core AuthoizeNet module.
3
+ # The entire SDK is name-spaced inside of this module.
4
+ module AuthorizeNet
5
+ # Some type conversion routines that will be injected into our
6
+ # Transaction/Response classes.
7
+ module TypeConversions
8
+ API_FIELD_PREFIX = 'x_'.freeze
9
+
10
+ # Converts a value received from Authorize.Net into a boolean if
11
+ # possible. This is designed to handle the wide range of boolean
12
+ # formats that Authorize.Net uses.
13
+ def value_to_boolean(value)
14
+ case value
15
+ when "TRUE", "T", "YES", "Y", "1", "true"
16
+ true
17
+ when "FALSE", "F", "NO", "N", "0", "false"
18
+ false
19
+ else
20
+ value
21
+ end
22
+ end
23
+
24
+ # Converts a boolean into an Authorize.Net boolean value string.
25
+ # This is designed to handle the wide range of boolean formats that
26
+ # Authorize.Net uses. If bool isn't a Boolean, its converted to a
27
+ # string and passed along.
28
+ def boolean_to_value(bool)
29
+ case bool
30
+ when TrueClass, FalseClass
31
+ bool ? 'TRUE' : 'FALSE'
32
+ else
33
+ bool.to_s
34
+ end
35
+ end
36
+
37
+ # Converts a value received from Authorize.Net into a BigDecimal.
38
+ def value_to_decimal(value)
39
+ value = 0 if value == '' # Ruby 2.4+ does not accept ""
40
+ BigDecimal(value)
41
+ end
42
+
43
+ # Converts a BigDecimal (or Float) into an Authorize.Net float value
44
+ # string. If float isn't a BigDecimal (or Float), its converted to a
45
+ # string and passed along.
46
+ def decimal_to_value(float)
47
+ case float
48
+ when Float
49
+ format("%0.2f", float)
50
+ when BigDecimal
51
+ float.truncate(2).to_s('F')
52
+ else
53
+ float.to_s
54
+ end
55
+ end
56
+
57
+ # Converts a value received from Authorize.Net into a Date.
58
+ def value_to_date(value)
59
+ Date.strptime(value, '%Y-%m-%d')
60
+ end
61
+
62
+ # Converts a Date (or DateTime, or Time) into an Authorize.Net date
63
+ # value string. If date isn't a Date (or DateTime, or Time), its
64
+ # converted to a string and passed along.
65
+ def date_to_value(date)
66
+ case date
67
+ when Date, DateTime, Time
68
+ date.strftime('%Y-%m-%d')
69
+ else
70
+ date.to_s
71
+ end
72
+ end
73
+
74
+ # Converts a value received from Authorize.Net into a DateTime.
75
+ def value_to_datetime(value)
76
+ DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
77
+ end
78
+
79
+ # Converts a Date (or DateTime, or Time) into an Authorize.Net datetime
80
+ # value string. If date isn't a Date (or DateTime, or Time), it's
81
+ # converted to a string and passed along.
82
+ def datetime_to_value(datetime)
83
+ case datetime
84
+ when Date, DateTime
85
+ datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%SZ')
86
+ when Time
87
+ datetime.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
88
+ else
89
+ datetime.to_s
90
+ end
91
+ end
92
+
93
+ # Converts a value received from Authorize.Net into an Integer.
94
+ def value_to_integer(value)
95
+ value.to_s.to_i
96
+ end
97
+
98
+ # Converts an Integer into an Authorize.Net integer string.
99
+ def integer_to_value(int)
100
+ int.to_s
101
+ end
102
+
103
+ # Converts a key value pair into a HTTP POST parameter. The key is
104
+ # prefixed with key_prefix when being converted to a parameter name.
105
+ def to_param(key, value, key_prefix = API_FIELD_PREFIX)
106
+ key_str = "#{key_prefix}#{key}="
107
+ if value.is_a?(Array)
108
+ (value.collect do |v|
109
+ key_str + CGI.escape(v.to_s)
110
+ end).join('&')
111
+ else
112
+ key_str + CGI.escape(value.to_s)
113
+ end
114
+ end
115
+
116
+ # Converts an internal field name (Symbol) into an external field
117
+ # name (Symbol) that can be consumed by the Authorize.Net API.
118
+ def to_external_field(key)
119
+ (API_FIELD_PREFIX + key.to_s).to_sym
120
+ end
121
+
122
+ # Converts an external field name (Symbol) into an internal field
123
+ # name (Symbol). This is the exact inverse of to_external_field.
124
+ # Running to_internal_field(to_external_field(:foo)) would return
125
+ # :foo back.
126
+ def to_internal_field(key)
127
+ k_str = key.to_s
128
+ k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
129
+ end
130
+ end
131
+
132
+ # Provides some basic methods used by the various model classes.
133
+ module Model
134
+ # The constructor for models. Takes any of the supported attributes
135
+ # as key/value pairs.
136
+ def initialize(fields = {})
137
+ fields.each do |k, v|
138
+ method_name = (k.to_s + '=').to_sym
139
+ send(method_name, v) if respond_to?(method_name)
140
+ end
141
+ end
142
+
143
+ def to_a
144
+ [self]
145
+ end
146
+
147
+ #:enddoc:
148
+ protected
149
+
150
+ def handle_multivalue_hashing(obj)
151
+ obj.to_a.collect(&:to_hash)
152
+ end
153
+ end
154
+ end
@@ -1,23 +1,23 @@
1
- module AuthorizeNet
2
- # The core, API agnostic response class.
3
- # You shouldn't instantiate this one.
4
- class Response
5
- include AuthorizeNet::TypeConversions
6
-
7
- # Fields to convert to/from booleans.
8
- @@boolean_fields = []
9
-
10
- # Fields to convert to/from BigDecimal.
11
- @@decimal_fields = []
12
-
13
- # DO NOT USE.
14
- def initialize
15
- raise "#{self.class} should not be instantiated directly."
16
- end
17
-
18
- # Check to see if the response indicated success.
19
- def success?
20
- false
21
- end
22
- end
23
- end
1
+ module AuthorizeNet
2
+ # The core, API agnostic response class.
3
+ # You shouldn't instantiate this one.
4
+ class Response
5
+ include AuthorizeNet::TypeConversions
6
+
7
+ # Fields to convert to/from booleans.
8
+ @@boolean_fields = []
9
+
10
+ # Fields to convert to/from BigDecimal.
11
+ @@decimal_fields = []
12
+
13
+ # DO NOT USE.
14
+ def initialize
15
+ raise "#{self.class} should not be instantiated directly."
16
+ end
17
+
18
+ # Check to see if the response indicated success.
19
+ def success?
20
+ false
21
+ end
22
+ end
23
+ end
@@ -1,64 +1,64 @@
1
- module AuthorizeNet
2
- # The core, API agnostic transaction class.
3
- # You shouldn't instantiate this one.
4
- class Transaction
5
- include AuthorizeNet::TypeConversions
6
-
7
- # Fields to convert to/from booleans.
8
- @@boolean_fields = []
9
-
10
- # Fields to convert to/from BigDecimal.
11
- @@decimal_fields = []
12
-
13
- # DO NOT USE.
14
- def initialize
15
- @fields ||= {}
16
- end
17
-
18
- # Sets arbitrary API fields, overwriting existing values if they exist.
19
- # Takes a hash of key/value pairs, where the keys are the field names
20
- # without the "x_" prefix. You can set a field to Nil to unset it. If the
21
- # value is an array, each value in the array will be added. For example,
22
- # set_fields({:line_item => ["item1<|>golf balls<|><|>2<|>18.95<|>Y",
23
- # "item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>"]})
24
- # would generate two x_line_item fields in the transaction, one for
25
- # each value in the array.
26
- def set_fields(fields = {})
27
- @fields.merge!(fields)
28
- @fields.reject! { |_k, v| v.nil? }
29
- @fields
30
- end
31
-
32
- # Returns the current hash of API fields.
33
- attr_reader :fields
34
-
35
- # Takes an instance of AuthorizeNet::Address and adds it to the transaction.
36
- def set_address(address)
37
- @fields.merge!(address.to_hash)
38
- end
39
-
40
- # Takes an instance of AuthorizeNet::ShippingAddress and adds it to the
41
- # transaction.
42
- def set_shipping_address(address)
43
- @fields.merge!(address.to_hash)
44
- end
45
-
46
- # Takes an instance of AuthorizeNet::Customer and adds it to the transaction.
47
- def set_customer(customer)
48
- @fields.merge!(customer.to_hash)
49
- end
50
-
51
- #:enddoc:
52
- protected
53
-
54
- # Internal method to handle multiple types of payment arguments.
55
- def handle_payment_argument(payment)
56
- case payment
57
- when AuthorizeNet::CreditCard, AuthorizeNet::ECheck
58
- set_fields(payment.to_hash)
59
- else
60
- set_fields(card_num: payment)
61
- end
62
- end
63
- end
64
- end
1
+ module AuthorizeNet
2
+ # The core, API agnostic transaction class.
3
+ # You shouldn't instantiate this one.
4
+ class Transaction
5
+ include AuthorizeNet::TypeConversions
6
+
7
+ # Fields to convert to/from booleans.
8
+ @@boolean_fields = []
9
+
10
+ # Fields to convert to/from BigDecimal.
11
+ @@decimal_fields = []
12
+
13
+ # DO NOT USE.
14
+ def initialize
15
+ @fields ||= {}
16
+ end
17
+
18
+ # Sets arbitrary API fields, overwriting existing values if they exist.
19
+ # Takes a hash of key/value pairs, where the keys are the field names
20
+ # without the "x_" prefix. You can set a field to Nil to unset it. If the
21
+ # value is an array, each value in the array will be added. For example,
22
+ # set_fields({:line_item => ["item1<|>golf balls<|><|>2<|>18.95<|>Y",
23
+ # "item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>"]})
24
+ # would generate two x_line_item fields in the transaction, one for
25
+ # each value in the array.
26
+ def set_fields(fields = {})
27
+ @fields.merge!(fields)
28
+ @fields.reject! { |_k, v| v.nil? }
29
+ @fields
30
+ end
31
+
32
+ # Returns the current hash of API fields.
33
+ attr_reader :fields
34
+
35
+ # Takes an instance of AuthorizeNet::Address and adds it to the transaction.
36
+ def set_address(address)
37
+ @fields.merge!(address.to_hash)
38
+ end
39
+
40
+ # Takes an instance of AuthorizeNet::ShippingAddress and adds it to the
41
+ # transaction.
42
+ def set_shipping_address(address)
43
+ @fields.merge!(address.to_hash)
44
+ end
45
+
46
+ # Takes an instance of AuthorizeNet::Customer and adds it to the transaction.
47
+ def set_customer(customer)
48
+ @fields.merge!(customer.to_hash)
49
+ end
50
+
51
+ #:enddoc:
52
+ protected
53
+
54
+ # Internal method to handle multiple types of payment arguments.
55
+ def handle_payment_argument(payment)
56
+ case payment
57
+ when AuthorizeNet::CreditCard, AuthorizeNet::ECheck
58
+ set_fields(payment.to_hash)
59
+ else
60
+ set_fields(card_num: payment)
61
+ end
62
+ end
63
+ end
64
+ end