authorizenet 1.8.7 → 1.8.8

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