authorizenet 1.9.5 → 1.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/lib/app/helpers/authorize_net_helper.rb +23 -23
  3. data/lib/authorize_net.rb +107 -107
  4. data/lib/authorize_net/addresses/address.rb +25 -25
  5. data/lib/authorize_net/addresses/shipping_address.rb +22 -22
  6. data/lib/authorize_net/aim/response.rb +120 -120
  7. data/lib/authorize_net/aim/transaction.rb +171 -171
  8. data/lib/authorize_net/api/api_transaction.rb +119 -119
  9. data/lib/authorize_net/api/constants.yml +1 -1
  10. data/lib/authorize_net/api/schema.rb +5165 -5153
  11. data/lib/authorize_net/api/transaction.rb +261 -261
  12. data/lib/authorize_net/arb/fields.rb +24 -24
  13. data/lib/authorize_net/arb/paging.rb +29 -29
  14. data/lib/authorize_net/arb/response.rb +26 -26
  15. data/lib/authorize_net/arb/sorting.rb +39 -39
  16. data/lib/authorize_net/arb/subscription.rb +68 -68
  17. data/lib/authorize_net/arb/subscription_detail.rb +10 -10
  18. data/lib/authorize_net/arb/subscription_list_response.rb +36 -36
  19. data/lib/authorize_net/arb/transaction.rb +171 -171
  20. data/lib/authorize_net/authorize_net.rb +154 -154
  21. data/lib/authorize_net/cim/customer_profile.rb +15 -15
  22. data/lib/authorize_net/cim/payment_profile.rb +35 -35
  23. data/lib/authorize_net/cim/response.rb +111 -111
  24. data/lib/authorize_net/cim/transaction.rb +721 -721
  25. data/lib/authorize_net/customer.rb +24 -24
  26. data/lib/authorize_net/email_receipt.rb +20 -20
  27. data/lib/authorize_net/fields.rb +760 -760
  28. data/lib/authorize_net/key_value_response.rb +109 -109
  29. data/lib/authorize_net/key_value_transaction.rb +281 -281
  30. data/lib/authorize_net/line_item.rb +21 -21
  31. data/lib/authorize_net/order.rb +38 -38
  32. data/lib/authorize_net/payment_methods/credit_card.rb +61 -61
  33. data/lib/authorize_net/payment_methods/echeck.rb +70 -70
  34. data/lib/authorize_net/reporting/batch.rb +16 -16
  35. data/lib/authorize_net/reporting/batch_statistics.rb +15 -15
  36. data/lib/authorize_net/reporting/fds_filter.rb +8 -8
  37. data/lib/authorize_net/reporting/response.rb +157 -157
  38. data/lib/authorize_net/reporting/returned_item.rb +45 -45
  39. data/lib/authorize_net/reporting/transaction.rb +131 -131
  40. data/lib/authorize_net/reporting/transaction_details.rb +22 -22
  41. data/lib/authorize_net/response.rb +25 -25
  42. data/lib/authorize_net/sim/hosted_payment_form.rb +34 -34
  43. data/lib/authorize_net/sim/hosted_receipt_page.rb +32 -32
  44. data/lib/authorize_net/sim/response.rb +133 -133
  45. data/lib/authorize_net/sim/transaction.rb +128 -128
  46. data/lib/authorize_net/transaction.rb +66 -66
  47. data/lib/authorize_net/xml_response.rb +154 -154
  48. data/lib/authorize_net/xml_transaction.rb +279 -279
  49. data/lib/authorizenet.rb +4 -4
  50. data/lib/generators/authorize_net/direct_post/direct_post_generator.rb +52 -52
  51. data/lib/generators/authorize_net/direct_post/templates/README-AuthorizeNet +48 -48
  52. data/lib/generators/authorize_net/direct_post/templates/config.yml.erb +8 -8
  53. data/lib/generators/authorize_net/direct_post/templates/config.yml.rails3.erb +8 -8
  54. data/lib/generators/authorize_net/direct_post/templates/controller.rb.erb +30 -30
  55. data/lib/generators/authorize_net/direct_post/templates/initializer.rb +4 -4
  56. data/lib/generators/authorize_net/direct_post/templates/layout.erb +17 -17
  57. data/lib/generators/authorize_net/direct_post/templates/payment.erb +9 -9
  58. data/lib/generators/authorize_net/direct_post/templates/payment.rails3.erb +9 -9
  59. data/lib/generators/authorize_net/sim/sim_generator.rb +46 -46
  60. data/lib/generators/authorize_net/sim/templates/README-AuthorizeNet +51 -51
  61. data/lib/generators/authorize_net/sim/templates/config.yml.erb +8 -8
  62. data/lib/generators/authorize_net/sim/templates/config.yml.rails3.erb +8 -8
  63. data/lib/generators/authorize_net/sim/templates/controller.rb.erb +20 -20
  64. data/lib/generators/authorize_net/sim/templates/initializer.rb +4 -4
  65. data/lib/generators/authorize_net/sim/templates/layout.erb +17 -17
  66. data/lib/generators/authorize_net/sim/templates/payment.erb +5 -5
  67. data/lib/generators/authorize_net/sim/templates/payment.rails3.erb +5 -5
  68. data/lib/generators/generator_extensions.rb +73 -73
  69. metadata +84 -102
@@ -1,171 +1,171 @@
1
- module AuthorizeNet::ARB
2
- # The ARB transaction class.
3
- class Transaction < AuthorizeNet::XmlTransaction
4
- include AuthorizeNet::ARB::Fields
5
-
6
- # The default options for the constructor.
7
- @@option_defaults = {
8
- gateway: :production,
9
- verify_ssl: true,
10
- reference_id: nil
11
- }
12
-
13
- # Fields to convert to/from booleans.
14
- @@boolean_fields = []
15
-
16
- # Fields to convert to/from BigDecimal.
17
- @@decimal_fields = %i[amount trial_amount]
18
-
19
- # Fields to convert to/from Date.
20
- @@date_fields = [:subscription_start_date]
21
-
22
- # The class to wrap our response in.
23
- @response_class = AuthorizeNet::ARB::Response
24
-
25
- # Constructs an ARB transaction. You can use the new ARB transaction object
26
- # to issue a request to the payment gateway and parse the response into a new
27
- # AuthorizeNet::ARB::Response object.
28
- #
29
- # +api_login_id+:: Your API login ID, as a string.
30
- # +api_transaction_key+:: Your API transaction key, as a string.
31
- # +options+:: A hash of options. See below for values.
32
- #
33
- # Options
34
- # +gateway+:: The gateway to submit the transaction to. Can be a URL string, an AuthorizeNet::ARB::Transaction::Gateway constant, or one of the convenience symbols :sandbox, :test, :production, or :live (:test is an alias for :sandbox, and :live is an alias for :production).
35
- # +verify_ssl+:: A boolean indicating if the SSL certificate of the +gateway+ should be verified. Defaults to true.
36
- # +reference_id+:: A string that can be used to identify a particular transaction with its response. Will be echo'd in the response, only if it was provided in the transaction. Defaults to nil.
37
- #
38
- def initialize(api_login_id, api_transaction_key, options = {})
39
- ActiveSupport::Deprecation.warn "use AuthorizeNet::API::Transaction"
40
- super
41
- end
42
-
43
- # Sets up and submits a start of subscription (ARBCreateSubscriptionRequest) transaction. Returns a response object. If the transaction
44
- # has already been run, it will return nil.
45
- #
46
- # +subscription+:: An instance of AuthorizeNet::ARB::Subscription describing the recurring payment you would like to create.
47
- #
48
- #
49
- # Typical usage:
50
- #
51
- # subscription = AuthorizeNet::ARB::Subscription.new(
52
- # :name => "Monthly Gift Basket",
53
- # :length => 1,
54
- # :unit => :month,
55
- # :start_date => Date.today,
56
- # :total_occurrences => :unlimited,
57
- # :amount => 100.00,
58
- # :invoice_number => '1234567',
59
- # :description => "John Doe's Monthly Gift Basket",
60
- # :credit_card => AuthorizeNet::CreditCard.new('4111111111111111', '1120'),
61
- # :billing_address => AuthorizeNet::Address.new(:first_name => 'John', :last_name => 'Doe')
62
- # )
63
- # response = transaction.create(subscription)
64
- #
65
- def create(subscription)
66
- @type = Type::ARB_CREATE
67
- set_fields(subscription.to_hash)
68
- run
69
- end
70
-
71
- # Sets up and submits a subscription update (ARBUpdateSubscriptionRequest) transaction. Returns a response object. If the transaction
72
- # has already been run, it will return nil.
73
- #
74
- # +subscription+:: An instance of AuthorizeNet::ARB::Subscription describing the changes to make. It must have a value for subscription_id so that the API knows what subscription to update. Note that some information (intervals, start dates, etc) can't be changed. See the ARB guide for more details.
75
- #
76
- #
77
- # Typical usage:
78
- #
79
- # subscription = AuthorizeNet::ARB::Subscription.new(
80
- # :billing_address => AuthorizeNet::Address.new(:first_name => 'Jane', :last_name => 'Doe'),
81
- # :subscription_id => '123456'
82
- # )
83
- # response = transaction.update(subscription)
84
- #
85
- def update(subscription)
86
- @type = Type::ARB_UPDATE
87
- set_fields(subscription.to_hash)
88
- run
89
- end
90
-
91
- # Sets up and submits a subscription status query (ARBGetSubscriptionStatusRequest) transaction. Returns a response object (which contains the subscription status). If the transaction
92
- # has already been run, it will return nil.
93
- #
94
- # +subscription_id+:: Either the subscription id of the subscription to get the status of as a string, or a Subscription instance with a value for subscription_id set on it.
95
- #
96
- #
97
- # Typical usage:
98
- #
99
- # response = transaction.get_status('123456')
100
- # response.subscription_status # A value from AuthorizeNet::ARB::Subscription::Status
101
- #
102
- def get_status(subscription_id)
103
- @type = Type::ARB_GET_STATUS
104
- handle_subscription_id(subscription_id)
105
- run
106
- end
107
-
108
- # Sets up and submits a subscription list query (ARBGetSubscriptionListRequest). Returns a response object
109
- # which contains the list of subscription details and the total number of subscriptions matching the search
110
- # criteria. Sorting and Paging are optional parameters.
111
- #
112
- # Valid values for search type parameter:
113
- # cardExpiringThisMonth
114
- # subscriptionActive
115
- # subscriptionExpiringThisMonth
116
- # subscriptionInactive
117
- #
118
- # Typical usage:
119
- #
120
- # sorting = AuthorizeNet::ARB::Sorting.new('name',true)
121
- # paging = AuthorizeNet::ARB::Paging.new(1,1000)
122
- # response = transaction.get_subscription_list('subscriptionActive',sorting,paging)
123
- #
124
- def get_subscription_list(search_type, sorting = nil, paging = nil)
125
- unless search_type.nil?
126
- self.class.instance_variable_set(:@response_class, SubscriptionListResponse)
127
- @type = Type::ARB_GET_SUBSCRIPTION_LIST
128
- set_fields(search_type: search_type.to_s)
129
- set_fields(sorting.to_hash) unless sorting.nil?
130
- set_fields(paging.to_hash) unless paging.nil?
131
- run
132
- end
133
- response
134
- end
135
-
136
- # Sets up and submits a subscription cancelation (ARBCancelSubscriptionRequest) transaction. Returns a response object. If the transaction
137
- # has already been run, it will return nil.
138
- #
139
- # +subscription_id+:: Either the subscription id of the subscription to get the status of as a string, or a Subscription instance with a value for subscription_id set on it.
140
- #
141
- #
142
- # Typical usage:
143
- #
144
- # response = transaction.cancel('123456')
145
- #
146
- def cancel(subscription_id)
147
- @type = Type::ARB_CANCEL
148
- handle_subscription_id(subscription_id)
149
- run
150
- end
151
-
152
- #:enddoc:
153
- protected
154
-
155
- # Internal method to handle multiple types of subscription id arguments.
156
- def handle_subscription_id(subscription_id)
157
- case subscription_id
158
- when Subscription
159
- set_fields(subscription_id: subscription_id.subscription_id.to_s)
160
- else
161
- set_fields(subscription_id: subscription_id.to_s)
162
- end
163
- end
164
-
165
- # An internal method that builds the POST body, submits it to the gateway, and constructs a Response object with the response.
166
- def make_request
167
- set_fields(reference_id: @reference_id)
168
- super
169
- end
170
- end
171
- end
1
+ module AuthorizeNet::ARB
2
+ # The ARB transaction class.
3
+ class Transaction < AuthorizeNet::XmlTransaction
4
+ include AuthorizeNet::ARB::Fields
5
+
6
+ # The default options for the constructor.
7
+ @@option_defaults = {
8
+ gateway: :production,
9
+ verify_ssl: true,
10
+ reference_id: nil
11
+ }
12
+
13
+ # Fields to convert to/from booleans.
14
+ @@boolean_fields = []
15
+
16
+ # Fields to convert to/from BigDecimal.
17
+ @@decimal_fields = %i[amount trial_amount]
18
+
19
+ # Fields to convert to/from Date.
20
+ @@date_fields = [:subscription_start_date]
21
+
22
+ # The class to wrap our response in.
23
+ @response_class = AuthorizeNet::ARB::Response
24
+
25
+ # Constructs an ARB transaction. You can use the new ARB transaction object
26
+ # to issue a request to the payment gateway and parse the response into a new
27
+ # AuthorizeNet::ARB::Response object.
28
+ #
29
+ # +api_login_id+:: Your API login ID, as a string.
30
+ # +api_transaction_key+:: Your API transaction key, as a string.
31
+ # +options+:: A hash of options. See below for values.
32
+ #
33
+ # Options
34
+ # +gateway+:: The gateway to submit the transaction to. Can be a URL string, an AuthorizeNet::ARB::Transaction::Gateway constant, or one of the convenience symbols :sandbox, :test, :production, or :live (:test is an alias for :sandbox, and :live is an alias for :production).
35
+ # +verify_ssl+:: A boolean indicating if the SSL certificate of the +gateway+ should be verified. Defaults to true.
36
+ # +reference_id+:: A string that can be used to identify a particular transaction with its response. Will be echo'd in the response, only if it was provided in the transaction. Defaults to nil.
37
+ #
38
+ def initialize(api_login_id, api_transaction_key, options = {})
39
+ ActiveSupport::Deprecation.warn "use AuthorizeNet::API::Transaction"
40
+ super
41
+ end
42
+
43
+ # Sets up and submits a start of subscription (ARBCreateSubscriptionRequest) transaction. Returns a response object. If the transaction
44
+ # has already been run, it will return nil.
45
+ #
46
+ # +subscription+:: An instance of AuthorizeNet::ARB::Subscription describing the recurring payment you would like to create.
47
+ #
48
+ #
49
+ # Typical usage:
50
+ #
51
+ # subscription = AuthorizeNet::ARB::Subscription.new(
52
+ # :name => "Monthly Gift Basket",
53
+ # :length => 1,
54
+ # :unit => :month,
55
+ # :start_date => Date.today,
56
+ # :total_occurrences => :unlimited,
57
+ # :amount => 100.00,
58
+ # :invoice_number => '1234567',
59
+ # :description => "John Doe's Monthly Gift Basket",
60
+ # :credit_card => AuthorizeNet::CreditCard.new('4111111111111111', '1120'),
61
+ # :billing_address => AuthorizeNet::Address.new(:first_name => 'John', :last_name => 'Doe')
62
+ # )
63
+ # response = transaction.create(subscription)
64
+ #
65
+ def create(subscription)
66
+ @type = Type::ARB_CREATE
67
+ set_fields(subscription.to_hash)
68
+ run
69
+ end
70
+
71
+ # Sets up and submits a subscription update (ARBUpdateSubscriptionRequest) transaction. Returns a response object. If the transaction
72
+ # has already been run, it will return nil.
73
+ #
74
+ # +subscription+:: An instance of AuthorizeNet::ARB::Subscription describing the changes to make. It must have a value for subscription_id so that the API knows what subscription to update. Note that some information (intervals, start dates, etc) can't be changed. See the ARB guide for more details.
75
+ #
76
+ #
77
+ # Typical usage:
78
+ #
79
+ # subscription = AuthorizeNet::ARB::Subscription.new(
80
+ # :billing_address => AuthorizeNet::Address.new(:first_name => 'Jane', :last_name => 'Doe'),
81
+ # :subscription_id => '123456'
82
+ # )
83
+ # response = transaction.update(subscription)
84
+ #
85
+ def update(subscription)
86
+ @type = Type::ARB_UPDATE
87
+ set_fields(subscription.to_hash)
88
+ run
89
+ end
90
+
91
+ # Sets up and submits a subscription status query (ARBGetSubscriptionStatusRequest) transaction. Returns a response object (which contains the subscription status). If the transaction
92
+ # has already been run, it will return nil.
93
+ #
94
+ # +subscription_id+:: Either the subscription id of the subscription to get the status of as a string, or a Subscription instance with a value for subscription_id set on it.
95
+ #
96
+ #
97
+ # Typical usage:
98
+ #
99
+ # response = transaction.get_status('123456')
100
+ # response.subscription_status # A value from AuthorizeNet::ARB::Subscription::Status
101
+ #
102
+ def get_status(subscription_id)
103
+ @type = Type::ARB_GET_STATUS
104
+ handle_subscription_id(subscription_id)
105
+ run
106
+ end
107
+
108
+ # Sets up and submits a subscription list query (ARBGetSubscriptionListRequest). Returns a response object
109
+ # which contains the list of subscription details and the total number of subscriptions matching the search
110
+ # criteria. Sorting and Paging are optional parameters.
111
+ #
112
+ # Valid values for search type parameter:
113
+ # cardExpiringThisMonth
114
+ # subscriptionActive
115
+ # subscriptionExpiringThisMonth
116
+ # subscriptionInactive
117
+ #
118
+ # Typical usage:
119
+ #
120
+ # sorting = AuthorizeNet::ARB::Sorting.new('name',true)
121
+ # paging = AuthorizeNet::ARB::Paging.new(1,1000)
122
+ # response = transaction.get_subscription_list('subscriptionActive',sorting,paging)
123
+ #
124
+ def get_subscription_list(search_type, sorting = nil, paging = nil)
125
+ unless search_type.nil?
126
+ self.class.instance_variable_set(:@response_class, SubscriptionListResponse)
127
+ @type = Type::ARB_GET_SUBSCRIPTION_LIST
128
+ set_fields(search_type: search_type.to_s)
129
+ set_fields(sorting.to_hash) unless sorting.nil?
130
+ set_fields(paging.to_hash) unless paging.nil?
131
+ run
132
+ end
133
+ response
134
+ end
135
+
136
+ # Sets up and submits a subscription cancelation (ARBCancelSubscriptionRequest) transaction. Returns a response object. If the transaction
137
+ # has already been run, it will return nil.
138
+ #
139
+ # +subscription_id+:: Either the subscription id of the subscription to get the status of as a string, or a Subscription instance with a value for subscription_id set on it.
140
+ #
141
+ #
142
+ # Typical usage:
143
+ #
144
+ # response = transaction.cancel('123456')
145
+ #
146
+ def cancel(subscription_id)
147
+ @type = Type::ARB_CANCEL
148
+ handle_subscription_id(subscription_id)
149
+ run
150
+ end
151
+
152
+ #:enddoc:
153
+ protected
154
+
155
+ # Internal method to handle multiple types of subscription id arguments.
156
+ def handle_subscription_id(subscription_id)
157
+ case subscription_id
158
+ when Subscription
159
+ set_fields(subscription_id: subscription_id.subscription_id.to_s)
160
+ else
161
+ set_fields(subscription_id: subscription_id.to_s)
162
+ end
163
+ end
164
+
165
+ # An internal method that builds the POST body, submits it to the gateway, and constructs a Response object with the response.
166
+ def make_request
167
+ set_fields(reference_id: @reference_id)
168
+ super
169
+ end
170
+ end
171
+ end
@@ -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