authorize-net 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/License.pdf +0 -0
  2. data/README.rdoc +124 -0
  3. data/Rakefile +74 -0
  4. data/generators/authorize_net_direct_post/USAGE +20 -0
  5. data/generators/authorize_net_direct_post/authorize_net_direct_post_generator.rb +21 -0
  6. data/generators/authorize_net_direct_post/templates/README-AuthorizeNet +49 -0
  7. data/generators/authorize_net_direct_post/templates/config.yml.erb +8 -0
  8. data/generators/authorize_net_direct_post/templates/config.yml.rails3.erb +8 -0
  9. data/generators/authorize_net_direct_post/templates/controller.rb.erb +31 -0
  10. data/generators/authorize_net_direct_post/templates/initializer.rb +4 -0
  11. data/generators/authorize_net_direct_post/templates/layout.erb +18 -0
  12. data/generators/authorize_net_direct_post/templates/payment.erb +10 -0
  13. data/generators/authorize_net_direct_post/templates/payment.rails3.erb +10 -0
  14. data/generators/authorize_net_direct_post/templates/receipt.erb +1 -0
  15. data/generators/authorize_net_direct_post/templates/relay_response.erb +1 -0
  16. data/generators/authorize_net_sim/USAGE +20 -0
  17. data/generators/authorize_net_sim/authorize_net_sim_generator.rb +19 -0
  18. data/generators/authorize_net_sim/templates/README-AuthorizeNet +52 -0
  19. data/generators/authorize_net_sim/templates/config.yml.erb +8 -0
  20. data/generators/authorize_net_sim/templates/config.yml.rails3.erb +8 -0
  21. data/generators/authorize_net_sim/templates/controller.rb.erb +21 -0
  22. data/generators/authorize_net_sim/templates/initializer.rb +4 -0
  23. data/generators/authorize_net_sim/templates/layout.erb +18 -0
  24. data/generators/authorize_net_sim/templates/payment.erb +6 -0
  25. data/generators/authorize_net_sim/templates/payment.rails3.erb +6 -0
  26. data/generators/authorize_net_sim/templates/thank_you.erb +1 -0
  27. data/generators/generator_extensions.rb +75 -0
  28. data/init.rb +2 -0
  29. data/install.rb +1 -0
  30. data/lib/app/helpers/authorize_net_helper.rb +24 -0
  31. data/lib/authorize-net.rb +4 -0
  32. data/lib/authorize_net.rb +92 -0
  33. data/lib/authorize_net/addresses/address.rb +29 -0
  34. data/lib/authorize_net/addresses/shipping_address.rb +26 -0
  35. data/lib/authorize_net/aim/response.rb +131 -0
  36. data/lib/authorize_net/aim/transaction.rb +184 -0
  37. data/lib/authorize_net/arb/response.rb +34 -0
  38. data/lib/authorize_net/arb/subscription.rb +72 -0
  39. data/lib/authorize_net/arb/transaction.rb +146 -0
  40. data/lib/authorize_net/authorize_net.rb +154 -0
  41. data/lib/authorize_net/cim/customer_profile.rb +19 -0
  42. data/lib/authorize_net/cim/payment_profile.rb +37 -0
  43. data/lib/authorize_net/cim/response.rb +110 -0
  44. data/lib/authorize_net/cim/transaction.rb +678 -0
  45. data/lib/authorize_net/customer.rb +27 -0
  46. data/lib/authorize_net/email_receipt.rb +24 -0
  47. data/lib/authorize_net/fields.rb +736 -0
  48. data/lib/authorize_net/key_value_response.rb +117 -0
  49. data/lib/authorize_net/key_value_transaction.rb +291 -0
  50. data/lib/authorize_net/line_item.rb +25 -0
  51. data/lib/authorize_net/order.rb +42 -0
  52. data/lib/authorize_net/payment_methods/credit_card.rb +74 -0
  53. data/lib/authorize_net/payment_methods/echeck.rb +72 -0
  54. data/lib/authorize_net/reporting/batch.rb +19 -0
  55. data/lib/authorize_net/reporting/batch_statistics.rb +19 -0
  56. data/lib/authorize_net/reporting/fds_filter.rb +11 -0
  57. data/lib/authorize_net/reporting/response.rb +127 -0
  58. data/lib/authorize_net/reporting/transaction.rb +116 -0
  59. data/lib/authorize_net/reporting/transaction_details.rb +25 -0
  60. data/lib/authorize_net/response.rb +27 -0
  61. data/lib/authorize_net/sim/hosted_payment_form.rb +38 -0
  62. data/lib/authorize_net/sim/hosted_receipt_page.rb +37 -0
  63. data/lib/authorize_net/sim/response.rb +142 -0
  64. data/lib/authorize_net/sim/transaction.rb +138 -0
  65. data/lib/authorize_net/transaction.rb +66 -0
  66. data/lib/authorize_net/xml_response.rb +172 -0
  67. data/lib/authorize_net/xml_transaction.rb +275 -0
  68. data/lib/generators/authorize_net/direct_post_generator.rb +51 -0
  69. data/lib/generators/authorize_net/sim_generator.rb +47 -0
  70. data/spec/aim_spec.rb +310 -0
  71. data/spec/arb_spec.rb +191 -0
  72. data/spec/authorize_net_spec.rb +200 -0
  73. data/spec/cim_spec.rb +450 -0
  74. data/spec/reporting_spec.rb +431 -0
  75. data/spec/sim_spec.rb +97 -0
  76. data/spec/spec.opts +5 -0
  77. data/spec/spec_helper.rb +2 -0
  78. data/uninstall.rb +1 -0
  79. metadata +223 -0
@@ -0,0 +1,184 @@
1
+ module AuthorizeNet::AIM
2
+
3
+ # The AIM transaction class. Handles building the transaction payload and
4
+ # transmitting it to the gateway.
5
+ class Transaction < AuthorizeNet::KeyValueTransaction
6
+
7
+ # The default options for the constructor.
8
+ @@option_defaults = {
9
+ :transaction_type => Type::AUTHORIZE_AND_CAPTURE,
10
+ :gateway => :production,
11
+ :test => false,
12
+ :allow_split => false,
13
+ :delimiter => ',',
14
+ :encapsulation_character => nil,
15
+ :verify_ssl => false,
16
+ :device_type => DeviceType::UNKNOWN,
17
+ :market_type => MarketType::RETAIL
18
+ }
19
+
20
+ # Fields to convert to/from booleans.
21
+ @@boolean_fields = [:tax_exempt, :test_request, :recurring_billing, :allow_partial_auth, :delim_data, :email_customer, :relay_response]
22
+
23
+ # Fields to convert to/from BigDecimal.
24
+ @@decimal_fields = [:amount]
25
+
26
+ # Constructs an AIM transaction. You can use the new AIM transaction object
27
+ # to issue a request to the payment gateway and parse the response into a new
28
+ # AuthorizeNet::AIM::Response object.
29
+ #
30
+ # +api_login_id+:: Your API login ID, as a string.
31
+ # +api_transaction_key+:: Your API transaction key, as a string.
32
+ # +options+:: A hash of options. See below for values.
33
+ #
34
+ # Options
35
+ # +transaction_type+:: The type of transaction to perform. Defaults to AuthorizeNet::Type::AUTHORIZE_AND_CAPTURE. This value is only used if run is called directly.
36
+ # +gateway+:: The gateway to submit the transaction to. Can be a URL string, an AuthorizeNet::AIM::Transaction::Gateway constant, or one of the convenience symbols :sandbox, :test, :card_present_test, :card_present_live, :card_present_sandbox, :card_present_production, :production, or :live (:test is an alias for :sandbox, :card_present_test is an alias for :card_present_sandbox, :card_present_production is an alias for :card_present_live, and :live is an alias for :production).
37
+ # +test+:: A boolean indicating if the transaction should be run in test mode or not (defaults to false).
38
+ # +allow_split+:: A boolean indicating if split transactions should be allowed (defaults to false).
39
+ # +delimiter+:: A single character (as a string) that will be used to delimit the response from the gateway. Defaults to ','.
40
+ # +encapsulation_character+:: A single character (as a string) that will be used to encapsulate each field in the response from the gateway. Defaults to nil.
41
+ # +verify_ssl+:: A boolean indicating if the SSL certificate of the +gateway+ should be verified. Defaults to false.
42
+ # +device_type+:: A constant from DeviceType indicating the type of POS device used in a card present transaction. Defaults to DeviceType::UNKNOWN.
43
+ # +market_type+:: A constant from MarketType indicating your industry. Used for card present transactions. Defaults to MarketType::RETAIL.
44
+ #
45
+ def initialize(api_login_id, api_transaction_key, options = {})
46
+ super()
47
+ options = @@option_defaults.merge(options)
48
+ @api_login_id = api_login_id
49
+ @api_transaction_key = api_transaction_key
50
+ @test_mode = options[:test]
51
+ @response ||= nil
52
+ @delimiter = options[:delimiter]
53
+ @type = options[:transaction_type]
54
+ @cp_version = nil
55
+ case options[:gateway]
56
+ when :sandbox, :test
57
+ @gateway = Gateway::TEST
58
+ when :production, :live
59
+ @gateway = Gateway::LIVE
60
+ when :card_present_live, :card_present_production
61
+ @gateway = Gateway::CARD_PRESENT_LIVE
62
+ @cp_version = '1.0'
63
+ when :card_present_test, :card_present_sandbox
64
+ @gateway = Gateway::CARD_PRESENT_TEST
65
+ @cp_version = '1.0'
66
+ else
67
+ @gateway = options[:gateway]
68
+ end
69
+ @allow_split_transaction = options[:allow_split]
70
+ @encapsulation_character = options[:encapsulation_character]
71
+ @verify_ssl = options[:verify_ssl]
72
+ @market_type = options[:market_type]
73
+ @device_type = options[:device_type]
74
+ end
75
+
76
+ # Checks if the transaction has been configured for test mode or not. Return TRUE if the
77
+ # transaction is a test transaction, FALSE otherwise. All transactions run against the sandbox
78
+ # are considered test transactions.
79
+ def test?
80
+ super || @gateway == Gateway::TEST
81
+ end
82
+
83
+ # Returns TRUE if split transactions are allowed, FALSE otherwise.
84
+ def split_transaction_allowed?
85
+ !!@allow_split_transaction
86
+ end
87
+
88
+ # Returns the current encapsulation character unless there is none, in which case Nil is returned.
89
+ def encapsulation_character
90
+ @encapsulation_character
91
+ end
92
+
93
+ # Returns the gateway to be used for this transaction.
94
+ def gateway
95
+ @gateway
96
+ end
97
+
98
+ # Checks to see if the transaction has a response (meaning it has been submitted to the gateway).
99
+ # Returns TRUE if a response is present, FALSE otherwise.
100
+ def has_response?
101
+ !@response.nil?
102
+ end
103
+
104
+ # Retrieve the response object (or Nil if transaction hasn't been sent to the gateway).
105
+ def response
106
+ @response
107
+ end
108
+
109
+ # Returns the current delimiter we are using to parse the fields returned by the
110
+ # gateway.
111
+ def delimiter
112
+ @delimiter
113
+ end
114
+
115
+ # Sets the delimiter used to parse the response from the gateway.
116
+ def delimiter=(delimiter)
117
+ @delimiter = delimiter
118
+ end
119
+
120
+ # Submits the transaction to the gateway for processing. Returns a response object. If the transaction
121
+ # has already been run, it will return nil.
122
+ def run
123
+ make_request
124
+ end
125
+
126
+ # Returns the current card present API version that we are adhering to.
127
+ def cp_version
128
+ @cp_version
129
+ end
130
+
131
+ #:enddoc:
132
+ protected
133
+
134
+ # An internal method that builds the POST body, submits it to the gateway, and constructs a Response object with the response.
135
+ def make_request
136
+ if has_response?
137
+ return nil
138
+ end
139
+ url = URI.parse(@gateway)
140
+ fields = @fields.merge(:type => @type, :delim_char => @delimiter, :delim_data => "TRUE", :login => @api_login_id, :tran_key => @api_transaction_key, :relay_response => "FALSE")
141
+
142
+ if @cp_version.nil?
143
+ fields.merge!(:version => @version)
144
+ else
145
+ fields.merge!(:cp_version => @cp_version, :market_type => @market_type, :device_type => @device_type, :response_format => "1")
146
+ end
147
+ fields[:test_request] = boolean_to_value(@test_mode)
148
+ fields[:allow_partial_auth] = 'TRUE' if @allow_split_transaction
149
+ fields[:encap_char] = @encapsulation_character unless @encapsulation_character.nil?
150
+ fields.each do |k, v|
151
+ if @@boolean_fields.include?(k)
152
+ fields[k] = boolean_to_value(v)
153
+ elsif @@decimal_fields.include?(k)
154
+ fields[k] = decimal_to_value(v)
155
+ end
156
+ end
157
+ data = fields.collect do |key, val|
158
+ self.to_param(key, val)
159
+ end
160
+ custom_field_keys = @custom_fields.keys.collect(&:to_s).sort.collect(&:to_sym)
161
+ for key in custom_field_keys
162
+ data += [self.to_param(key, @custom_fields[key.to_sym], '')]
163
+ end
164
+ data.flatten!
165
+ request = Net::HTTP::Post.new(url.path)
166
+ request.content_type = 'application/x-www-form-urlencoded'
167
+ request.body = data.join("&")
168
+ connection = Net::HTTP.new(url.host, url.port)
169
+ connection.use_ssl = true
170
+ if @verify_ssl
171
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
172
+ else
173
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
174
+ end
175
+ begin
176
+ @response = AuthorizeNet::AIM::Response.new((connection.start {|http| http.request(request)}), self)
177
+ rescue
178
+ @response = AuthorizeNet::AIM::Response.new($!, self)
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ end
@@ -0,0 +1,34 @@
1
+ module AuthorizeNet::ARB
2
+
3
+ # The ARB response class.
4
+ class Response < AuthorizeNet::XmlResponse
5
+
6
+ # Constructs a new response object from a +raw_response. You don't typically
7
+ # construct this object yourself, as AuthorizeNet::ARB::Transaction will
8
+ # build one for you when it makes the request to the gateway.
9
+ def initialize(raw_response, transaction)
10
+ super
11
+ unless connection_failure?
12
+ begin
13
+ @subscription_id = node_content_unless_nil(@root.at_css('subscriptionId'))
14
+ @subscription_status = node_content_unless_nil(@root.at_css('Status'))
15
+ rescue
16
+ @raw_response = $!
17
+ end
18
+ end
19
+ end
20
+
21
+ # Returns the subscriptionId from the response if there is one. Otherwise returns nil.
22
+ def subscription_id
23
+ @subscription_id
24
+ end
25
+
26
+ # Returns the status of the Subscription from the response if there is one. Otherwise returns nil. This value
27
+ # is only returned in response to a get_status transaction.
28
+ def subscription_status
29
+ @subscription_status
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,72 @@
1
+ module AuthorizeNet::ARB
2
+
3
+ # Models an ARB subscription.
4
+ class Subscription
5
+
6
+ # Use this constant for the value of total_occurrences to get a subscription with no end.
7
+ UNLIMITED_OCCURRENCES = 9999
8
+
9
+ # Constants for the various interval units supported by the ARB API.
10
+ module IntervalUnits
11
+ MONTH = 'months'
12
+ DAY = 'days'
13
+ end
14
+
15
+ # Constants for the various statuses a subscription can have. These are returned by the get_status call.
16
+ module Status
17
+ ACTIVE = 'active'
18
+ EXPIRED = 'expired'
19
+ SUSPENDED = 'suspended'
20
+ CANCELED = 'canceled'
21
+ TERMINATED = 'terminated'
22
+ end
23
+
24
+ include AuthorizeNet::Model
25
+
26
+ attr_accessor :name, :length, :unit, :start_date, :total_occurrences, :trial_occurrences, :amount, :trial_amount, :invoice_number, :description, :subscription_id, :credit_card, :billing_address, :shipping_address, :customer
27
+
28
+ # Override the length setter to provide support for :unlimited shortcut. Do not document this method in rdoc.
29
+ def length=(new_length) #:nodoc:
30
+ if new_length == :unlimited
31
+ @length = UNLIMITED_OCCURRENCES
32
+ else
33
+ @length = new_length
34
+ end
35
+ end
36
+
37
+ # Override the unit setter to provide support for :day, :days, :month, :months shortcut. Do not document this method in rdoc.
38
+ def unit=(new_unit) #:nodoc:
39
+ case new_unit
40
+ when :day, :days
41
+ @unit = IntervalUnits::DAY
42
+ when :month, :months
43
+ @unit = IntervalUnits::MONTH
44
+ else
45
+ @unit = new_unit
46
+ end
47
+ end
48
+
49
+ def to_hash
50
+ hash = {
51
+ :subscription_name => @name,
52
+ :subscription_length => @length,
53
+ :subscription_unit => @unit,
54
+ :subscription_start_date => @start_date,
55
+ :subscription_total_occurrences => @total_occurrences,
56
+ :subscription_trial_occurrences => @trial_occurrences,
57
+ :subscription_amount => @amount,
58
+ :subscription_trial_amount => @trial_amount,
59
+ :invoice_num => @invoice_number,
60
+ :description => @description,
61
+ :subscription_id => @subscription_id
62
+ }
63
+ hash.merge!(@credit_card.to_hash) unless @credit_card.nil?
64
+ hash.merge!(@billing_address.to_hash) unless @billing_address.nil?
65
+ hash.merge!(@shipping_address.to_hash) unless @shipping_address.nil?
66
+ hash.merge!(@customer.to_hash) unless @customer.nil?
67
+ hash.delete_if {|k, v| v.nil?}
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,146 @@
1
+ module AuthorizeNet::ARB
2
+
3
+ # The ARB transaction class.
4
+ class Transaction < AuthorizeNet::XmlTransaction
5
+
6
+ include AuthorizeNet::ARB::Fields
7
+
8
+ # The default options for the constructor.
9
+ @@option_defaults = {
10
+ :gateway => :production,
11
+ :verify_ssl => false,
12
+ :reference_id => nil
13
+ }
14
+
15
+ # Fields to convert to/from booleans.
16
+ @@boolean_fields = []
17
+
18
+ # Fields to convert to/from BigDecimal.
19
+ @@decimal_fields = [:amount, :trial_amount]
20
+
21
+ # Fields to convert to/from Date.
22
+ @@date_fields = [:subscription_start_date]
23
+
24
+ # The class to wrap our response in.
25
+ @response_class = AuthorizeNet::ARB::Response
26
+
27
+
28
+ # Constructs an ARB transaction. You can use the new ARB transaction object
29
+ # to issue a request to the payment gateway and parse the response into a new
30
+ # AuthorizeNet::ARB::Response object.
31
+ #
32
+ # +api_login_id+:: Your API login ID, as a string.
33
+ # +api_transaction_key+:: Your API transaction key, as a string.
34
+ # +options+:: A hash of options. See below for values.
35
+ #
36
+ # Options
37
+ # +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).
38
+ # +verify_ssl+:: A boolean indicating if the SSL certificate of the +gateway+ should be verified. Defaults to false.
39
+ # +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.
40
+ #
41
+ def initialize(api_login_id, api_transaction_key, options = {})
42
+ super
43
+ end
44
+
45
+ # Sets up and submits a start of subscription (ARBCreateSubscriptionRequest) transaction. Returns a response object. If the transaction
46
+ # has already been run, it will return nil.
47
+ #
48
+ # +subscription+:: An instance of AuthorizeNet::ARB::Subscription describing the recurring payment you would like to create.
49
+ #
50
+ #
51
+ # Typical usage:
52
+ #
53
+ # subscription = AuthorizeNet::ARB::Subscription.new(
54
+ # :name => "Monthly Gift Basket",
55
+ # :length => 1,
56
+ # :unit => :month,
57
+ # :start_date => Date.today,
58
+ # :total_occurrences => :unlimited,
59
+ # :amount => 100.00,
60
+ # :invoice_number => '1234567',
61
+ # :description => "John Doe's Monthly Gift Basket",
62
+ # :credit_card => AuthorizeNet::CreditCard.new('4111111111111111', '1120'),
63
+ # :billing_address => AuthorizeNet::Address.new(:first_name => 'John', :last_name => 'Doe')
64
+ # )
65
+ # response = transaction.create(subscription)
66
+ #
67
+ def create(subscription)
68
+ @type = Type::ARB_CREATE
69
+ set_fields(subscription.to_hash)
70
+ run
71
+ end
72
+
73
+ # Sets up and submits a subscription update (ARBUpdateSubscriptionRequest) transaction. Returns a response object. If the transaction
74
+ # has already been run, it will return nil.
75
+ #
76
+ # +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.
77
+ #
78
+ #
79
+ # Typical usage:
80
+ #
81
+ # subscription = AuthorizeNet::ARB::Subscription.new(
82
+ # :billing_address => AuthorizeNet::Address.new(:first_name => 'Jane', :last_name => 'Doe'),
83
+ # :subscription_id => '123456'
84
+ # )
85
+ # response = transaction.update(subscription)
86
+ #
87
+ def update(subscription)
88
+ @type = Type::ARB_UPDATE
89
+ set_fields(subscription.to_hash)
90
+ run
91
+ end
92
+
93
+ # Sets up and submits a subscription status query (ARBGetSubscriptionStatusRequest) transaction. Returns a response object (which contains the subscription status). If the transaction
94
+ # has already been run, it will return nil.
95
+ #
96
+ # +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.
97
+ #
98
+ #
99
+ # Typical usage:
100
+ #
101
+ # response = transaction.get_status('123456')
102
+ # response.subscription_status # A value from AuthorizeNet::ARB::Subscription::Status
103
+ #
104
+ def get_status(subscription_id)
105
+ @type = Type::ARB_GET_STATUS
106
+ handle_subscription_id(subscription_id)
107
+ run
108
+ end
109
+
110
+ # Sets up and submits a subscription cancelation (ARBCancelSubscriptionRequest) transaction. Returns a response object. If the transaction
111
+ # has already been run, it will return nil.
112
+ #
113
+ # +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.
114
+ #
115
+ #
116
+ # Typical usage:
117
+ #
118
+ # response = transaction.cancel('123456')
119
+ #
120
+ def cancel(subscription_id)
121
+ @type = Type::ARB_CANCEL
122
+ handle_subscription_id(subscription_id)
123
+ run
124
+ end
125
+
126
+ #:enddoc:
127
+ protected
128
+
129
+ # Internal method to handle multiple types of subscription id arguments.
130
+ def handle_subscription_id(subscription_id)
131
+ case subscription_id
132
+ when Subscription
133
+ set_fields(:subscription_id => subscription_id.subscription_id.to_s)
134
+ else
135
+ set_fields(:subscription_id => subscription_id.to_s)
136
+ end
137
+ end
138
+
139
+ # An internal method that builds the POST body, submits it to the gateway, and constructs a Response object with the response.
140
+ def make_request
141
+ set_fields(:reference_id => @reference_id)
142
+ super
143
+ end
144
+
145
+ end
146
+ end
@@ -0,0 +1,154 @@
1
+ # :title: Authorize.Net Ruby SDK
2
+ # The core AuthoizeNet module. The entire SDK is name-spaced inside of this module.
3
+ module AuthorizeNet
4
+
5
+ # Some type conversion routines that will be injected into our Transaction/Response
6
+ # classes.
7
+ module TypeConversions
8
+
9
+ API_FIELD_PREFIX = 'x_'
10
+
11
+ # Coverts a value received from Authorize.Net into a boolean if possible. This
12
+ # is designed to handle the wide range of boolean 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. This
25
+ # is designed to handle the wide range of boolean formats that Authorize.Net
26
+ # uses. If bool isn't a Boolean, its converted to a string and passed along.
27
+ def boolean_to_value(bool)
28
+ case bool
29
+ when TrueClass, FalseClass
30
+ bool ? 'TRUE' : 'FALSE'
31
+ else
32
+ bool.to_s
33
+ end
34
+ end
35
+
36
+ # Coverts a value received from Authorize.Net into a BigDecimal.
37
+ def value_to_decimal(value)
38
+ BigDecimal.new(value)
39
+ end
40
+
41
+ # Converts a BigDecimal (or Float) into an Authorize.Net float value string. If float isn't
42
+ # a BigDecimal (or Float), its converted to a string and passed along.
43
+ def decimal_to_value(float)
44
+ case float
45
+ when Float
46
+ "%0.2f" % float
47
+ when BigDecimal
48
+ float.truncate(2).to_s('F')
49
+ else
50
+ float.to_s
51
+ end
52
+ end
53
+
54
+ # Coverts a value received from Authorize.Net into a Date.
55
+ def value_to_date(value)
56
+ Date.strptime(value, '%Y-%m-%d')
57
+ end
58
+
59
+ # Converts a Date (or DateTime, or Time) into an Authorize.Net date value string. If date isn't
60
+ # a Date (or DateTime, or Time), its converted to a string and passed along.
61
+ def date_to_value(date)
62
+ case date
63
+ when Date, DateTime, Time
64
+ date.strftime('%Y-%m-%d')
65
+ else
66
+ date.to_s
67
+ end
68
+ end
69
+
70
+ # Coverts a value received from Authorize.Net into a DateTime.
71
+ def value_to_datetime(value)
72
+ DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
73
+ end
74
+
75
+ # Converts a Date (or DateTime, or Time) into an Authorize.Net datetime value string. If date isn't
76
+ # a Date (or DateTime, or Time), its converted to a string and passed along.
77
+ def datetime_to_value(datetime)
78
+ case datetime
79
+ when Date, DateTime
80
+ datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%SZ')
81
+ when Time
82
+ datetime.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
83
+ else
84
+ datetime.to_s
85
+ end
86
+ end
87
+
88
+ # Coverts a value received from Authorize.Net into an Integer.
89
+ def value_to_integer(value)
90
+ value.to_s.to_i
91
+ end
92
+
93
+ # Coverts an Integer into an Authorize.Net integer string.
94
+ def integer_to_value(int)
95
+ int.to_s
96
+ end
97
+
98
+ # Converts a key value pair into a HTTP POST parameter. The key is prefixed
99
+ # with key_prefix when being converted to a parameter name.
100
+ def to_param(key, value, key_prefix = API_FIELD_PREFIX)
101
+ key_str = "#{key_prefix}#{key}="
102
+ if value.kind_of?(Array)
103
+ (value.collect do |v|
104
+ key_str + CGI::escape(v.to_s)
105
+ end).join('&')
106
+ else
107
+ key_str + CGI::escape(value.to_s)
108
+ end
109
+ end
110
+
111
+
112
+ # Converts an internal field name (Symbol) into an external field name (Symbol)
113
+ # that can be consumed by the Authorize.Net API.
114
+ def to_external_field(key)
115
+ (API_FIELD_PREFIX + key.to_s).to_sym
116
+ end
117
+
118
+ # Converts an external field name (Symbol) into an internal field name (Symbol). This
119
+ # is the exact inverse of to_external_field. Running to_internal_field(to_external_field(:foo))
120
+ # would return :foo back.
121
+ def to_internal_field(key)
122
+ k_str = key.to_s
123
+ k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
124
+ end
125
+ end
126
+
127
+ # Provides some basic methods used by the various model classes.
128
+ module Model
129
+
130
+ # The constructor for models. Takes any of the supported attributes
131
+ # as key/value pairs.
132
+ def initialize(fields = {})
133
+ fields.each do |k, v|
134
+ method_name = (k.to_s + '=').to_sym
135
+ if self.respond_to?(method_name)
136
+ self.send(method_name, v)
137
+ end
138
+ end
139
+ end
140
+
141
+ def to_a
142
+ [self]
143
+ end
144
+
145
+ #:enddoc:
146
+ protected
147
+
148
+ def handle_multivalue_hashing(obj)
149
+ obj.to_a.collect(&:to_hash)
150
+ end
151
+
152
+ end
153
+
154
+ end