Avatax_TaxService 1.0.14 → 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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YjAwNjNhNDYxNmY2NDk4MTIwNDNjOWU5NzNiZDk0OGY3ZTRmOWIzYQ==
5
- data.tar.gz: !binary |-
6
- NmIyODBhY2JiNGI4MDY1OTVkZjY2MGE3ZjJlZWU0OWYyZDVjZjE5Yw==
2
+ SHA1:
3
+ metadata.gz: dda65b810670689ee2629eca1585245fcb36ed93
4
+ data.tar.gz: a1fcc662b18825625ca427210f495ba519c7d46d
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NmVhOGFjNWI2NWU0NWJkOWYxOGZkYTUzZDMxMmY0N2IyOGMyZjk1ODRhN2U1
10
- Y2YxZTI0NDE2NTJkMmVhZTI1ZGExYjY1ZjNlMDlhNDA1MTAxYTJlM2M2ODBm
11
- YmEzMzhiMTQwMjFmMDExNTFkMWRhMDI5YzNkNTM3NWIwNjkxZGE=
12
- data.tar.gz: !binary |-
13
- NzA5ZmIxMjVmMzY3ODNiZTVlNDI0MTJjMGUxOTkwMjEzMGMxMzA5ZWY5ODI2
14
- MTdjMDMyMWY4NjU5NTRkYWRiOGM4M2RlMmVkNzc3ZGExODM1MGRmYjUwMzNl
15
- M2ZkZGI5NWRiZGIxZmMzNmI3MDBmMjNmOTEwNGRlOGNmYzk4YjY=
6
+ metadata.gz: e30884fe83ad03f7ee8819f6004be5a3ae6a4ecc25a74bcc603838155a6527a915143a4d10f0793f271f6fc608bd83d728d776fb67f422bdcc9d30b68f2913ac
7
+ data.tar.gz: 00f3190776f769af80fc2cc4c7c1f3af84e0d43013b001f8ca36ca6f4101235afe7e2453054c0c359963f48e6323087f87e0025b49eae1ab702101cde0e5abfe
@@ -1,23 +1,23 @@
1
- Gem::Specification.new do |s|
2
- s.name = "Avatax_TaxService"
3
- s.version = "1.0.14"
4
- s.date = "2012-10-28"
5
- s.author = "Graham S Wilson"
6
- s.email = "support@Avalara.com"
7
- s.summary = "Avatax SDK for Ruby "
8
- s.homepage = "http://www.avalara.com/"
9
- s.description = "Ruby SDK provides means of communication with Avatax Web Services."
10
- s.license = 'MIT'
11
- s.files = ["lib/tax_log.txt", "lib/taxservice_dev.wsdl", "lib/taxservice_prd.wsdl", "lib/avatax_taxservice.rb",
12
- "lib/template_adjusttax.erb", "lib/template_canceltax.erb", "lib/template_committax.erb","lib/template_gettax.erb",
13
- "lib/template_gettaxhistory.erb","lib/template_isauthorized.erb","lib/template_ping.erb","lib/template_posttax.erb",
14
- "lib/template_reconciletaxhistory.erb",
15
- "samples/CancelTaxTest.rb","samples/GetTaxTest.rb","samples/GetTaxHistoryTest.rb","samples/PingTest.rb","samples/PostTaxTest.rb","samples/ValidateAddressTest.rb",
16
- "spec/adjusttax_spec.rb","spec/canceltax_spec.rb","spec/committax_spec.rb","spec/gettax_spec.rb",
17
- "spec/gettaxhistory_spec.rb","spec/isauthorized_spec.rb","spec/ping_spec.rb","spec/posttax_spec.rb","spec/reconciletaxhistory_spec.rb",
18
- "spec/spec_helper.rb","spec/taxservice_spec.rb","Avatax_TaxService.gemspec","Avatax Ruby SDK Guide.docx", "LICENSE.txt"]
19
- s.add_dependency "savon", ">= 2.3.0"
20
- s.required_ruby_version = '>= 1.9.1'
21
- s.post_install_message = 'Thanks for installing the Avalara TaxService Ruby SDK. Refer to "Avatax Ruby SDK User Guide.docx" to get started.'
22
- end
1
+ Gem::Specification.new do |s|
2
+ s.name = "Avatax_TaxService"
3
+ s.version = "2.0.1"
4
+ s.date = "2015-06-12"
5
+ s.author = "Graham S Wilson"
6
+ s.email = "support@Avalara.com"
7
+ s.summary = "Avatax SDK for Ruby "
8
+ s.homepage = "http://www.avalara.com/"
9
+ s.description = "Ruby SDK provides means of communication with Avatax Web Services."
10
+ s.license = 'MIT'
11
+ s.files = ["lib/tax_log.txt", "lib/taxservice.wsdl", "lib/avatax_taxservice.rb",
12
+ "lib/template_adjusttax.erb", "lib/template_canceltax.erb", "lib/template_committax.erb","lib/template_gettax.erb",
13
+ "lib/template_gettaxhistory.erb","lib/template_isauthorized.erb","lib/template_ping.erb","lib/template_posttax.erb",
14
+ "lib/template_reconciletaxhistory.erb",
15
+ "samples/CancelTaxTest.rb","samples/GetTaxTest.rb","samples/GetTaxHistoryTest.rb","samples/PingTest.rb","samples/PostTaxTest.rb",
16
+ "spec/adjusttax_spec.rb","spec/canceltax_spec.rb","spec/committax_spec.rb","spec/gettax_spec.rb",
17
+ "spec/gettaxhistory_spec.rb","spec/isauthorized_spec.rb","spec/ping_spec.rb","spec/posttax_spec.rb","spec/reconciletaxhistory_spec.rb",
18
+ "spec/spec_helper.rb","spec/taxservice_spec.rb","Avatax_TaxService.gemspec","LICENSE.txt"]
19
+ s.add_dependency "savon", ">= 2.3.0"
20
+ s.required_ruby_version = '>= 1.9.1'
21
+ s.post_install_message = 'Thanks for installing the Avalara TaxService Ruby SDK. For more information and to sign up for a free test account, visit http://developer.avalara.com'
22
+ end
23
23
 
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- Ruby SDK for AvaTax [License]
2
- Copyright (c) 2013 Avalara Inc.
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining
5
- a copy of this software and associated documentation files (the
6
- "Software"), to deal in the Software without restriction, including
7
- without limitation the rights to use, copy, modify, merge, publish,
8
- distribute, sublicense, and/or sell copies of the Software, and to
9
- permit persons to whom the Software is furnished to do so, subject to
10
- the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Ruby SDK for AvaTax [License]
2
+ Copyright (c) 2013 Avalara Inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,649 +1,644 @@
1
- require 'savon'
2
- require 'erb'
3
- require 'benchmark'
4
-
5
- module AvaTax
6
- #Avalara tax class
7
- class TaxService
8
- def initialize(credentials)
9
-
10
- #Retrieve gemspec details
11
- spec = Gem::Specification.find_by_name("Avatax_TaxService")
12
- #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
13
- #find other objects that it needs.
14
- gem_root = spec.gem_dir
15
- @def_locn = gem_root + "/lib"
16
-
17
- #Extract data from hash
18
- username = credentials[:username]
19
- password = credentials[:password]
20
- if username.nil? and password.nil?
21
- raise ArgumentError, "username and password are required"
22
- end
23
- name = credentials[:name]
24
- clientname = credentials[:clientname]
25
- adapter = credentials[:adapter]
26
- machine = credentials[:machine]
27
- use_production_account = credentials[:use_production_account]
28
-
29
- #Set credentials and Profile information
30
- @username = username == nil ? "" : username
31
- @password = password == nil ? "" : password
32
- @name = name == nil ? "" : name
33
- @clientname = (clientname == nil or clientname == "") ? "Avatax SDK for Ruby Default Client Name" : clientname
34
- @adapter = (adapter == nil or adapter == "") ? spec.summary + spec.version.to_s : adapter
35
- @machine = machine == nil ? "" : machine
36
- @use_production_account = (use_production_account != true) ? false : use_production_account
37
-
38
-
39
- #Header for response data
40
- @responsetime_hdr = " (User) (System) (Total) (Real)"
41
-
42
- #Open Avatax Error Log
43
- @log = File.new(@def_locn + '/tax_log.txt', "w")
44
-
45
- #Get service details from WSDL - control_array[2] contains the WSDL read from the address_control file
46
- #log :false turns off HTTP logging. Select either Dev or Prod depending on the value of the boolean value 'use_production_account'
47
- if @use_production_account
48
- @log.puts "#{Time.now}: Avalara Production Tax service started"
49
- @client = Savon.client(wsdl: @def_locn + '/taxservice_prd.wsdl', log: false)
50
- else
51
- @log.puts "#{Time.now}: Avalara Development Tax service started"
52
- @client = Savon.client(wsdl: @def_locn + '/taxservice_dev.wsdl', log: false)
53
- end
54
-
55
-
56
- #Read in the SOAP template for Get tax
57
- begin
58
- @template_gettax = ERB.new(File.read(@def_locn + '/template_gettax.erb'))
59
- rescue
60
- @log.puts "#{Time.now}: Error loading the GetTax template"
61
- end
62
-
63
- #Read in the SOAP template for Adjust tax
64
- begin
65
- @template_adjust = ERB.new(File.read(@def_locn + '/template_adjusttax.erb'))
66
- rescue
67
- @log.puts "#{Time.now}: Error loading the AdjustTax template"
68
- end
69
-
70
- #Read in the SOAP template for Ping
71
- begin
72
- @template_ping = ERB.new(File.read(@def_locn + '/template_ping.erb'))
73
- rescue
74
- @log.puts "#{Time.now}: Error loading the Ping template"
75
- end
76
-
77
- #Read in the SOAP template for IsAuthorized
78
- begin
79
- @template_isauthorized = ERB.new(File.read(@def_locn + '/template_isauthorized.erb'))
80
- rescue
81
- @log.puts "#{Time.now}: Error loading the IsAuthorized template"
82
- end
83
-
84
- #Read in the SOAP template for Tax
85
- begin
86
- @template_post = ERB.new(File.read(@def_locn + '/template_posttax.erb'))
87
- rescue
88
- @log.puts "#{Time.now}: Error loading the Post template"
89
- end
90
-
91
- #Read in the SOAP template for Commit tax
92
- begin
93
- @template_commit = ERB.new(File.read(@def_locn + '/template_committax.erb'))
94
- rescue
95
- @log.puts "#{Time.now}: Error loading the CommitTax template"
96
- end
97
-
98
- #Read in the SOAP template for Cancel tax
99
- begin
100
- @template_cancel = ERB.new(File.read(@def_locn + '/template_canceltax.erb'))
101
- rescue
102
- @log.puts "#{Time.now}: Error loading the CancelTax template"
103
- end
104
-
105
- #Read in the SOAP template for GetTaxHistory tax
106
- begin
107
- @template_gettaxhistory = ERB.new(File.read(@def_locn + '/template_gettaxhistory.erb'))
108
- rescue
109
- @log.puts "#{Time.now}: Error loading the GetTaxHistory template"
110
- end
111
-
112
- #Read in the SOAP template for GetTaxHistory tax
113
- begin
114
- @template_reconciletaxhistory = ERB.new(File.read(@def_locn + '/template_reconciletaxhistory.erb'))
115
- rescue
116
- @log.puts "#{Time.now}: Error loading the ReconcileTaxHistory template"
117
- end
118
-
119
- # Create hash for validate result
120
- @response = Hash.new
121
- end
122
-
123
- ####################################################################################################
124
- # ping - Verifies connectivity to the web service and returns version information about the service.
125
- ####################################################################################################
126
- def ping(message = nil)
127
-
128
- @service = 'Ping'
129
-
130
- #Read in the SOAP template
131
- @message = message == nil ? "?" : message
132
-
133
- # Subsitute real vales for template place holders
134
- @soap = @template_ping.result(binding)
135
- if @debug
136
- @log.puts "#{Time.now}: SOAP request created:"
137
- @log.puts @soap
138
- end
139
-
140
- # Make the call to the Avalara service
141
- begin
142
- # Call using debug
143
- if @debug
144
- # Use Ruby built in Benchmark function to record response times
145
- @log.puts "#{Time.now}: Calling #{@service} Service"
146
- time = Benchmark.measure do
147
- # Call Ping Service
148
- @response = @client.call(:ping, xml: @soap).to_hash
149
- end
150
- @log.puts "Response times for Ping:"
151
- @log.puts @responsetime_hdr
152
- @log.puts time
153
- else
154
- # Call Ping Service
155
- @response = @client.call(:ping, xml: @soap).to_hash
156
- end
157
-
158
- #Strip off outer layer of the hash - not needed
159
- @response = messages_to_array(@response[:ping_response][:ping_result])
160
-
161
- return @response
162
-
163
- #Capture unexpected errors
164
- rescue Savon::Error => error
165
- abend(error)
166
- end
167
-
168
- end
169
-
170
- ####################################################################################################
171
- # gettax - Calls the Avatax GetTax Service
172
- ####################################################################################################
173
- def gettax(document)
174
-
175
- @service = 'GetTax'
176
-
177
- #create instance variables for each entry in input
178
- document.each do |k,v|
179
- instance_variable_set("@#{k}",v)
180
- end
181
- #set required default values for missing required inputs
182
- @doctype ||= "SalesOrder"
183
- @discount ||= "0"
184
- @detaillevel ||= "Tax"
185
- @commit ||= false
186
- @servicemode ||= "Remote"
187
- @paymentdate ||= "1900-01-01"
188
- @exchangerate ||= "0"
189
- @exchangerateeffdate ||= "1900-01-01"
190
- @taxoverridetype ||= "None"
191
- @taxamount ||= "0"
192
- @taxdate ||= "1900-01-01"
193
-
194
- #set required values for some fields
195
- @hashcode = "0"
196
- @batchcode = ""
197
-
198
-
199
- #@addresses
200
- @addresses.each do |addr|
201
- addr[:taxregionid] ||= "0"
202
- end
203
- #@lines
204
- @lines.each do |line|
205
- line[:taxoverridetypeline] ||= "None"
206
- line[:taxamountline] ||= "0"
207
- line[:taxdateline] ||= "1900-01-01"
208
- line[:discounted] ||= false
209
- line[:taxincluded] ||= false
210
- end
211
-
212
- # Subsitute template place holders with real values
213
- @soap = @template_gettax.result(binding)
214
- # If in debug mode write SOAP request to log
215
- if @debug
216
- @log.puts "#{Time.now}: SOAP request created:"
217
- @log.puts @soap
218
- end
219
-
220
- # Make the call to the Avalara service
221
- begin
222
- # Call using debug
223
- if @debug
224
- # Use Ruby built in Benchmark function to record response times
225
- @log.puts "#{Time.now}: Calling GetTax Service for DocCode: #{@doccode}"
226
- time = Benchmark.measure do
227
- # Call GetTax Service
228
- @response = @client.call(:get_tax, xml: @soap).to_hash
229
- end
230
- @log.puts "Response times for GetTax:"
231
- @log.puts @responsetime_hdr
232
- @log.puts time
233
- else
234
- # Call GetTax Service
235
- @response = @client.call(:get_tax, xml: @soap).to_hash
236
- end
237
-
238
- #Strip off outer layer of the hash - not needed
239
- @response = messages_to_array(@response[:get_tax_response][:get_tax_result])
240
-
241
- #Return data to calling program
242
- return @response
243
-
244
- #Capture unexpected errors
245
- rescue Savon::Error => error
246
- abend(error)
247
- end
248
- end
249
-
250
- ####################################################################################################
251
- # adjusttax - Calls the Avatax AdjustTax Service
252
- ####################################################################################################
253
- def adjusttax(document)
254
-
255
- @service = 'AdjustTax'
256
-
257
- #create instance variables for each entry in input
258
- document.each do |k,v|
259
- instance_variable_set("@#{k}",v)
260
- end
261
- #set required default values for missing required inputs
262
- @doctype ||= "SalesOrder"
263
- @discount ||= "0"
264
- @detaillevel ||= "Tax"
265
- @commit ||= false
266
- @servicemode ||= "Remote"
267
- @paymentdate ||= "1900-01-01"
268
- @exchangerate ||= "0"
269
- @exchangerateeffdate ||= "1900-01-01"
270
- @taxoverridetype ||= "None"
271
- @taxamount ||= "0"
272
- @taxdate ||= "1900-01-01"
273
-
274
- #set required values for some fields
275
- @hashcode = "0"
276
- @batchcode = ""
277
-
278
-
279
- #@addresses
280
- @addresses.each do |addr|
281
- addr[:taxregionid] ||= "0"
282
- end
283
- #@lines
284
- @lines.each do |line|
285
- line[:taxoverridetypeline] ||= "None"
286
- line[:taxamountline] ||= "0"
287
- line[:taxdateline] ||= "1900-01-01"
288
- line[:discounted] ||= false
289
- line[:taxincluded] ||= false
290
- end
291
-
292
-
293
-
294
- # Subsitute template place holders with real values
295
- @soap = @template_adjust.result(binding)
296
- if @debug
297
- @log.puts "#{Time.now}: SOAP request created:"
298
- @log.puts @soap
299
- end
300
-
301
- # Make the call to the Avalara service
302
- begin
303
- # Call using debug
304
- if @debug
305
- # Use Ruby built in Benchmark function to record response times
306
- @log.puts "#{Time.now}: Calling AdjustTax Service for DocCode: #{@doccode}"
307
- time = Benchmark.measure do
308
- # Call AdjustTax Service
309
- @response = @client.call(:adjust_tax, xml: @soap).to_hash
310
- end
311
- @log.puts "Response times for AdjustTax:"
312
- @log.puts @responsetime_hdr
313
- @log.puts time
314
- else
315
- # Call AdjustTax Service
316
- @response = @client.call(:adjust_tax, xml: @soap).to_hash
317
- end
318
-
319
- #Strip off outer layer of the hash - not needed
320
- return messages_to_array(@response[:adjust_tax_response][:adjust_tax_result])
321
-
322
- #Capture unexpected errors
323
- rescue Savon::Error => error
324
- abend(error)
325
- end
326
-
327
- end
328
-
329
- ####################################################################################################
330
- # posttax - Calls the Avatax PostTax Service
331
- ####################################################################################################
332
- def posttax(document)
333
-
334
- @service = 'PostTax'
335
-
336
- #create instance variables for each entry in input
337
- document.each do |k,v|
338
- instance_variable_set("@#{k}",v)
339
- end
340
- #set required default values for missing required inputs
341
- @hashcode = "0"
342
-
343
-
344
-
345
- # Subsitute template place holders with real values
346
- @soap = @template_post.result(binding)
347
- if @debug
348
- @log.puts "#{Time.now}: SOAP request created:"
349
- @log.puts @soap
350
- end
351
-
352
- # Make the call to the Avalara service
353
- begin
354
- # Call using debug
355
- if @debug
356
- # Use Ruby built in Benchmark function to record response times
357
- @log.puts "#{Time.now}: Calling PostTax Service for DocCode: #{@doccode}"
358
- time = Benchmark.measure do
359
- # Call PostTax Service
360
- @response = @client.call(:post_tax, xml: @soap).to_hash
361
- end
362
- @log.puts "Response times for PostTax:"
363
- @log.puts @responsetime_hdr
364
- @log.puts time
365
- else
366
- # Call PostTax Service
367
- @response = @client.call(:post_tax, xml: @soap).to_hash
368
- end
369
-
370
- #Strip off outer layer of the hash - not needed
371
- #Return data to calling program
372
- return messages_to_array(@response[:post_tax_response][:post_tax_result])
373
-
374
- #Capture unexpected errors
375
- rescue Savon::Error => error
376
- abend(error)
377
- end
378
-
379
- end
380
-
381
- ####################################################################################################
382
- # committax - Calls the Avatax CommitTax Service
383
- ####################################################################################################
384
- def committax(document)
385
-
386
- @service = 'CommitTax'
387
-
388
- #create instance variables for each entry in input
389
- document.each do |k,v|
390
- instance_variable_set("@#{k}",v)
391
- end
392
- #set required default values for missing required inputs
393
- #no required default values exist for committax
394
-
395
- # Subsitute template place holders with real values
396
- @soap = @template_commit.result(binding)
397
- if @debug
398
- @log.puts "#{Time.now}: SOAP request created:"
399
- @log.puts @soap
400
- end
401
-
402
- # Make the call to the Avalara service
403
- begin
404
- # Call using debug
405
- if @debug
406
- # Use Ruby built in Benchmark function to record response times
407
- @log.puts "#{Time.now}: Calling CommitTax Service for DocCode: #{@doccode}"
408
- time = Benchmark.measure do
409
- # Call CommitTax Service
410
- @response = @client.call(:commit_tax, xml: @soap).to_hash
411
- end
412
- @log.puts "Response times for CommitTax:"
413
- @log.puts @responsetime_hdr
414
- @log.puts time
415
- else
416
- # Call CommitTax Service
417
- @response = @client.call(:commit_tax, xml: @soap).to_hash
418
- end
419
-
420
- #Strip off outer layer of the hash - not needed
421
- #Return data to calling program
422
- return messages_to_array(@response[:commit_tax_response][:commit_tax_result])
423
-
424
- #Capture unexpected errors
425
- rescue Savon::Error => error
426
- abend(error)
427
- end
428
- end
429
-
430
- ####################################################################################################
431
- # canceltax - Calls the Avatax CancelTax Service
432
- ####################################################################################################
433
- def canceltax(document)
434
-
435
- @service = 'CancelTax'
436
-
437
- #create instance variables for each entry in input
438
- document.each do |k,v|
439
- instance_variable_set("@#{k}",v)
440
- end
441
- #set required default values for missing required inputs
442
- #no required default values exist for canceltax
443
-
444
- # Subsitute template place holders with real values
445
- @soap = @template_cancel.result(binding)
446
- if @debug
447
- @log.puts "#{Time.now}: SOAP request created:"
448
- @log.puts @soap
449
- end
450
-
451
- # Make the call to the Avalara service
452
- begin
453
- # Call using debug
454
- if @debug
455
- # Use Ruby built in Benchmark function to record response times
456
- @log.puts "#{Time.now}: Calling CancelTax Service for DocCode: #{@doccode}"
457
- time = Benchmark.measure do
458
- # Call CancelTax Service
459
- @response = @client.call(:cancel_tax, xml: @soap).to_hash
460
- end
461
- @log.puts "Response times for CancelTax:"
462
- @log.puts @responsetime_hdr
463
- @log.puts time
464
- else
465
- # Call CancelTax Service
466
- @response = @client.call(:cancel_tax, xml: @soap).to_hash
467
- end
468
-
469
- #Strip off outer layer of the hash - not needed
470
- return messages_to_array(@response[:cancel_tax_response][:cancel_tax_result])
471
-
472
- #Capture unexpected errors
473
- rescue Savon::Error => error
474
- abend(error)
475
- end
476
- end
477
-
478
- ####################################################################################################
479
- # gettaxhistory - Calls the Avatax GetTaxHistory Service
480
- ####################################################################################################
481
- def gettaxhistory(document)
482
-
483
- @service = 'GetTaxHistory'
484
-
485
- #create instance variables for each entry in input
486
- document.each do |k,v|
487
- instance_variable_set("@#{k}",v)
488
- end
489
- #set required default values for missing required inputs
490
- @detaillevel ||= "Tax"
491
-
492
- # Subsitute template place holders with real values
493
- @soap = @template_gettaxhistory.result(binding)
494
- if @debug
495
- @log.puts "#{Time.now}: SOAP request created:"
496
- @log.puts @soap
497
- end
498
-
499
- # Make the call to the Avalara service
500
- begin
501
- # Call using debug
502
- if @debug
503
- # Use Ruby built in Benchmark function to record response times
504
- @log.puts "#{Time.now}: Calling GetTaxHistory Service"
505
- time = Benchmark.measure do
506
- # Call GetTaxHistory Service
507
- @response = @client.call(:get_tax_history, xml: @soap).to_hash
508
- end
509
- @log.puts "Response times for GetTaxHistory:"
510
- @log.puts @responsetime_hdr
511
- @log.puts time
512
- else
513
- # Call GetTaxHistory Service
514
- @response = @client.call(:get_tax_history, xml: @soap).to_hash
515
- end
516
-
517
- #Strip off outer layer of the hash - not needed
518
- return messages_to_array(@response[:get_tax_history_response][:get_tax_history_result])
519
-
520
-
521
- #Capture unexpected errors
522
- rescue Savon::Error => error
523
- abend(error)
524
- end
525
- end
526
-
527
- ####################################################################################################
528
- # reconciletaxhistory - Calls the Avatax ReconcileTaxHistory Service
529
- ####################################################################################################
530
- def reconciletaxhistory(document)
531
-
532
-
533
- @service = 'ReconcileTaxHistory'
534
-
535
- #create instance variables for each entry in input
536
- document.each do |k,v|
537
- instance_variable_set("@#{k}",v)
538
- end
539
- #set required default values for missing required inputs
540
- @pagesize ||= "100"
541
- @reconciled ||= false
542
- @doctype ||= "SalesInvoice"
543
- @docstatus ||= "Any"
544
- @lastdoccode ||= ''
545
-
546
-
547
-
548
- # Subsitute template place holders with real values
549
- @soap = @template_reconciletaxhistory.result(binding)
550
- if @debug
551
- @log.puts "#{Time.now}: SOAP request created:"
552
- @log.puts @soap
553
- end
554
-
555
- # Make the call to the Avalara service
556
- begin
557
- # Call using debug
558
- if @debug
559
- # Use Ruby built in Benchmark function to record response times
560
- @log.puts "#{Time.now}: Calling ReconcileTaxHistory Service"
561
- time = Benchmark.measure do
562
- # Call ReconcileTaxHistory Service
563
- @response = @client.call(:reconcile_tax_history, xml: @soap).to_hash
564
- end
565
- @log.puts "Response times for ReconcileTaxHistory:"
566
- @log.puts @responsetime_hdr
567
- @log.puts time
568
- else
569
- # Call ReconcileTaxHistory Service
570
- @response = @client.call(:reconcile_tax_history, xml: @soap).to_hash
571
- end
572
-
573
- #Strip off outer layer of the hash - not needed
574
- return messages_to_array(@response[:reconcile_tax_history_response][:reconcile_tax_history_result])
575
-
576
- #Capture unexpected errors
577
- rescue Savon::Error => error
578
- abend(error)
579
- end
580
- end
581
-
582
- ############################################################################################################
583
- # isauthorized - Verifies connectivity to the web service and returns expiry information about the service.
584
- ############################################################################################################
585
- def isauthorized(operation = nil)
586
-
587
- @service = 'IsAuthorized'
588
-
589
- #Read in the SOAP template
590
- @operation = operation == nil ? "?" : operation
591
-
592
- # Subsitute real vales for template place holders
593
- @soap = @template_isauthorized.result(binding)
594
- if @debug
595
- @log.puts "#{Time.now}: SOAP request created:"
596
- @log.puts @soap
597
- end
598
-
599
- # Make the call to the Avalara service
600
- begin
601
- @response = @client.call(:is_authorized, xml: @soap).to_hash
602
-
603
- #Strip off outer layer of the hash - not needed
604
- return messages_to_array(@response[:is_authorized_response][:is_authorized_result])
605
-
606
- #Capture unexpected errors
607
- rescue Savon::Error => error
608
- abend(error)
609
- end
610
- end
611
-
612
- private
613
-
614
- ############################################################################################################
615
- # abend - Unexpected error handling
616
- ############################################################################################################
617
- def abend(error)
618
- @log.puts "An unexpected error occurred: Response from server = #{error}"
619
- @log.puts "#{Time.now}: Error calling #{@service} service ... check that your account name and password are correct."
620
- @response = error.to_hash
621
- @response[:result_code] = 'Error'
622
- @response[:summary] = @response[:fault][:faultcode]
623
- @response[:details] = @response[:fault][:faultstring]
624
- return messages_to_array(@response)
625
- end
626
- ############################################################################################################
627
- #standardizes error message format to an array of messages - nokogiri will collapse a single element array into the response hash.
628
- ############################################################################################################
629
- def messages_to_array(response)
630
- if not response[:messages].nil?
631
- return response
632
- end
633
- # add the messages array to the response - if we got here, there was only one error.
634
- response[:messages] = [{
635
- :summary => response[:summary],
636
- :details => response[:details],
637
- :helplink => response[:helplink],
638
- :refersto => response[:refersto],
639
- :severity => response[:severity],
640
- :source => response[:source]
641
- }]
642
- #remove all the error information from the hash
643
- response[:messages][0].each do |k,v|
644
- response.delete(k)
645
- end
646
- return response
647
- end
648
- end
1
+ require 'savon'
2
+ require 'erb'
3
+ require 'benchmark'
4
+
5
+ module AvaTax
6
+ #Avalara tax class
7
+ class TaxService
8
+ def initialize(credentials)
9
+
10
+ #Retrieve gemspec details
11
+ spec = Gem::Specification.find_by_name("Avatax_TaxService")
12
+ #Set @def_locn to the Avatax-x.x.x gem install library. This enables the ruby programs to
13
+ #find other objects that it needs.
14
+ gem_root = spec.gem_dir
15
+ @def_locn = "../lib"#gem_root + "/lib"
16
+
17
+ #Extract data from hash
18
+ username = credentials[:username] || ""
19
+ password = credentials[:password] || ""
20
+ service_url = credentials[:service_url]
21
+ if service_url.nil? or service_url.empty?
22
+ raise ArgumentError, "service_url is required"
23
+ end
24
+ name = credentials[:name] || ""
25
+ clientname = (credentials[:clientname].nil? or credentials[:clientname].empty? ) ? "AvataxRubySDK" : credentials[:clientname]
26
+ adapter = (credentials[:adapter].nil? or credentials[:adapter].empty?) ? spec.summary + spec.version.to_s : credentials[:adapter]
27
+ machine = credentials[:machine] || ""
28
+
29
+
30
+ #Set credentials and Profile information
31
+ @username = username
32
+ @password = password
33
+ @name = name
34
+ @clientname = clientname
35
+ @adapter = adapter
36
+ @machine = machine
37
+
38
+ #Header for response data
39
+ @responsetime_hdr = " (User) (System) (Total) (Real)"
40
+
41
+ #Open Avatax Error Log
42
+ @log = File.new(@def_locn + '/tax_log.txt', "w")
43
+
44
+ #Get service details from WSDL - control_array[2] contains the WSDL read from the address_control file
45
+ #log :false turns off HTTP logging. Select either Dev or Prod depending on the value of the boolean value 'use_production_account'
46
+ @log.puts "#{Time.now}: Avalara Tax service started"
47
+ @client = Savon.client(wsdl: @def_locn + '/taxservice.wsdl', endpoint: URI.parse(service_url+"/Address/AddressSvc.asmx"), log: false)
48
+
49
+
50
+
51
+ #Read in the SOAP template for Get tax
52
+ begin
53
+ @template_gettax = ERB.new(File.read(@def_locn + '/template_gettax.erb'))
54
+ rescue
55
+ @log.puts "#{Time.now}: Error loading the GetTax template"
56
+ end
57
+
58
+ #Read in the SOAP template for Adjust tax
59
+ begin
60
+ @template_adjust = ERB.new(File.read(@def_locn + '/template_adjusttax.erb'))
61
+ rescue
62
+ @log.puts "#{Time.now}: Error loading the AdjustTax template"
63
+ end
64
+
65
+ #Read in the SOAP template for Ping
66
+ begin
67
+ @template_ping = ERB.new(File.read(@def_locn + '/template_ping.erb'))
68
+ rescue
69
+ @log.puts "#{Time.now}: Error loading the Ping template"
70
+ end
71
+
72
+ #Read in the SOAP template for IsAuthorized
73
+ begin
74
+ @template_isauthorized = ERB.new(File.read(@def_locn + '/template_isauthorized.erb'))
75
+ rescue
76
+ @log.puts "#{Time.now}: Error loading the IsAuthorized template"
77
+ end
78
+
79
+ #Read in the SOAP template for Tax
80
+ begin
81
+ @template_post = ERB.new(File.read(@def_locn + '/template_posttax.erb'))
82
+ rescue
83
+ @log.puts "#{Time.now}: Error loading the Post template"
84
+ end
85
+
86
+ #Read in the SOAP template for Commit tax
87
+ begin
88
+ @template_commit = ERB.new(File.read(@def_locn + '/template_committax.erb'))
89
+ rescue
90
+ @log.puts "#{Time.now}: Error loading the CommitTax template"
91
+ end
92
+
93
+ #Read in the SOAP template for Cancel tax
94
+ begin
95
+ @template_cancel = ERB.new(File.read(@def_locn + '/template_canceltax.erb'))
96
+ rescue
97
+ @log.puts "#{Time.now}: Error loading the CancelTax template"
98
+ end
99
+
100
+ #Read in the SOAP template for GetTaxHistory tax
101
+ begin
102
+ @template_gettaxhistory = ERB.new(File.read(@def_locn + '/template_gettaxhistory.erb'))
103
+ rescue
104
+ @log.puts "#{Time.now}: Error loading the GetTaxHistory template"
105
+ end
106
+
107
+ #Read in the SOAP template for GetTaxHistory tax
108
+ begin
109
+ @template_reconciletaxhistory = ERB.new(File.read(@def_locn + '/template_reconciletaxhistory.erb'))
110
+ rescue
111
+ @log.puts "#{Time.now}: Error loading the ReconcileTaxHistory template"
112
+ end
113
+
114
+ # Create hash for validate result
115
+ @response = Hash.new
116
+ end
117
+
118
+ ####################################################################################################
119
+ # ping - Verifies connectivity to the web service and returns version information about the service.
120
+ ####################################################################################################
121
+ def ping(message = nil)
122
+
123
+ @service = 'Ping'
124
+
125
+ #Read in the SOAP template
126
+ @message = message == nil ? "?" : message
127
+
128
+ # Subsitute real vales for template place holders
129
+ @soap = @template_ping.result(binding)
130
+ if @debug
131
+ @log.puts "#{Time.now}: SOAP request created:"
132
+ @log.puts @soap
133
+ end
134
+
135
+ # Make the call to the Avalara service
136
+ begin
137
+ # Call using debug
138
+ if @debug
139
+ # Use Ruby built in Benchmark function to record response times
140
+ @log.puts "#{Time.now}: Calling #{@service} Service"
141
+ time = Benchmark.measure do
142
+ # Call Ping Service
143
+ @response = @client.call(:ping, xml: @soap).to_hash
144
+ end
145
+ @log.puts "Response times for Ping:"
146
+ @log.puts @responsetime_hdr
147
+ @log.puts time
148
+ else
149
+ # Call Ping Service
150
+ @response = @client.call(:ping, xml: @soap).to_hash
151
+ end
152
+
153
+ #Strip off outer layer of the hash - not needed
154
+ @response = messages_to_array(@response[:ping_response][:ping_result])
155
+
156
+ return @response
157
+
158
+ #Capture unexpected errors
159
+ rescue Savon::Error => error
160
+ abend(error)
161
+ end
162
+
163
+ end
164
+
165
+ ####################################################################################################
166
+ # gettax - Calls the Avatax GetTax Service
167
+ ####################################################################################################
168
+ def gettax(document)
169
+
170
+ @service = 'GetTax'
171
+
172
+ #create instance variables for each entry in input
173
+ document.each do |k,v|
174
+ instance_variable_set("@#{k}",v)
175
+ end
176
+ #set required default values for missing required inputs
177
+ @doctype ||= "SalesOrder"
178
+ @discount ||= "0"
179
+ @detaillevel ||= "Tax"
180
+ @commit ||= false
181
+ @servicemode ||= "Remote"
182
+ @paymentdate ||= "1900-01-01"
183
+ @exchangerate ||= "0"
184
+ @exchangerateeffdate ||= "1900-01-01"
185
+ @taxoverridetype ||= "None"
186
+ @taxamount ||= "0"
187
+ @taxdate ||= "1900-01-01"
188
+
189
+ #set required values for some fields
190
+ @hashcode = "0"
191
+ @batchcode = ""
192
+
193
+
194
+ #@addresses
195
+ @addresses.each do |addr|
196
+ addr[:taxregionid] ||= "0"
197
+ end
198
+ #@lines
199
+ @lines.each do |line|
200
+ line[:taxoverridetypeline] ||= "None"
201
+ line[:taxamountline] ||= "0"
202
+ line[:taxdateline] ||= "1900-01-01"
203
+ line[:discounted] ||= false
204
+ line[:taxincluded] ||= false
205
+ end
206
+
207
+ # Subsitute template place holders with real values
208
+ @soap = @template_gettax.result(binding)
209
+ # If in debug mode write SOAP request to log
210
+ if @debug
211
+ @log.puts "#{Time.now}: SOAP request created:"
212
+ @log.puts @soap
213
+ end
214
+
215
+ # Make the call to the Avalara service
216
+ begin
217
+ # Call using debug
218
+ if @debug
219
+ # Use Ruby built in Benchmark function to record response times
220
+ @log.puts "#{Time.now}: Calling GetTax Service for DocCode: #{@doccode}"
221
+ time = Benchmark.measure do
222
+ # Call GetTax Service
223
+ @response = @client.call(:get_tax, xml: @soap).to_hash
224
+ end
225
+ @log.puts "Response times for GetTax:"
226
+ @log.puts @responsetime_hdr
227
+ @log.puts time
228
+ else
229
+ # Call GetTax Service
230
+ @response = @client.call(:get_tax, xml: @soap).to_hash
231
+ end
232
+
233
+ #Strip off outer layer of the hash - not needed
234
+ @response = messages_to_array(@response[:get_tax_response][:get_tax_result])
235
+
236
+ #Return data to calling program
237
+ return @response
238
+
239
+ #Capture unexpected errors
240
+ rescue Savon::Error => error
241
+ abend(error)
242
+ end
243
+ end
244
+
245
+ ####################################################################################################
246
+ # adjusttax - Calls the Avatax AdjustTax Service
247
+ ####################################################################################################
248
+ def adjusttax(document)
249
+
250
+ @service = 'AdjustTax'
251
+
252
+ #create instance variables for each entry in input
253
+ document.each do |k,v|
254
+ instance_variable_set("@#{k}",v)
255
+ end
256
+ #set required default values for missing required inputs
257
+ @doctype ||= "SalesOrder"
258
+ @discount ||= "0"
259
+ @detaillevel ||= "Tax"
260
+ @commit ||= false
261
+ @servicemode ||= "Remote"
262
+ @paymentdate ||= "1900-01-01"
263
+ @exchangerate ||= "0"
264
+ @exchangerateeffdate ||= "1900-01-01"
265
+ @taxoverridetype ||= "None"
266
+ @taxamount ||= "0"
267
+ @taxdate ||= "1900-01-01"
268
+
269
+ #set required values for some fields
270
+ @hashcode = "0"
271
+ @batchcode = ""
272
+
273
+
274
+ #@addresses
275
+ @addresses.each do |addr|
276
+ addr[:taxregionid] ||= "0"
277
+ end
278
+ #@lines
279
+ @lines.each do |line|
280
+ line[:taxoverridetypeline] ||= "None"
281
+ line[:taxamountline] ||= "0"
282
+ line[:taxdateline] ||= "1900-01-01"
283
+ line[:discounted] ||= false
284
+ line[:taxincluded] ||= false
285
+ end
286
+
287
+
288
+
289
+ # Subsitute template place holders with real values
290
+ @soap = @template_adjust.result(binding)
291
+ if @debug
292
+ @log.puts "#{Time.now}: SOAP request created:"
293
+ @log.puts @soap
294
+ end
295
+
296
+ # Make the call to the Avalara service
297
+ begin
298
+ # Call using debug
299
+ if @debug
300
+ # Use Ruby built in Benchmark function to record response times
301
+ @log.puts "#{Time.now}: Calling AdjustTax Service for DocCode: #{@doccode}"
302
+ time = Benchmark.measure do
303
+ # Call AdjustTax Service
304
+ @response = @client.call(:adjust_tax, xml: @soap).to_hash
305
+ end
306
+ @log.puts "Response times for AdjustTax:"
307
+ @log.puts @responsetime_hdr
308
+ @log.puts time
309
+ else
310
+ # Call AdjustTax Service
311
+ @response = @client.call(:adjust_tax, xml: @soap).to_hash
312
+ end
313
+
314
+ #Strip off outer layer of the hash - not needed
315
+ return messages_to_array(@response[:adjust_tax_response][:adjust_tax_result])
316
+
317
+ #Capture unexpected errors
318
+ rescue Savon::Error => error
319
+ abend(error)
320
+ end
321
+
322
+ end
323
+
324
+ ####################################################################################################
325
+ # posttax - Calls the Avatax PostTax Service
326
+ ####################################################################################################
327
+ def posttax(document)
328
+
329
+ @service = 'PostTax'
330
+
331
+ #create instance variables for each entry in input
332
+ document.each do |k,v|
333
+ instance_variable_set("@#{k}",v)
334
+ end
335
+ #set required default values for missing required inputs
336
+ @hashcode = "0"
337
+
338
+
339
+
340
+ # Subsitute template place holders with real values
341
+ @soap = @template_post.result(binding)
342
+ if @debug
343
+ @log.puts "#{Time.now}: SOAP request created:"
344
+ @log.puts @soap
345
+ end
346
+
347
+ # Make the call to the Avalara service
348
+ begin
349
+ # Call using debug
350
+ if @debug
351
+ # Use Ruby built in Benchmark function to record response times
352
+ @log.puts "#{Time.now}: Calling PostTax Service for DocCode: #{@doccode}"
353
+ time = Benchmark.measure do
354
+ # Call PostTax Service
355
+ @response = @client.call(:post_tax, xml: @soap).to_hash
356
+ end
357
+ @log.puts "Response times for PostTax:"
358
+ @log.puts @responsetime_hdr
359
+ @log.puts time
360
+ else
361
+ # Call PostTax Service
362
+ @response = @client.call(:post_tax, xml: @soap).to_hash
363
+ end
364
+
365
+ #Strip off outer layer of the hash - not needed
366
+ #Return data to calling program
367
+ return messages_to_array(@response[:post_tax_response][:post_tax_result])
368
+
369
+ #Capture unexpected errors
370
+ rescue Savon::Error => error
371
+ abend(error)
372
+ end
373
+
374
+ end
375
+
376
+ ####################################################################################################
377
+ # committax - Calls the Avatax CommitTax Service
378
+ ####################################################################################################
379
+ def committax(document)
380
+
381
+ @service = 'CommitTax'
382
+
383
+ #create instance variables for each entry in input
384
+ document.each do |k,v|
385
+ instance_variable_set("@#{k}",v)
386
+ end
387
+ #set required default values for missing required inputs
388
+ #no required default values exist for committax
389
+
390
+ # Subsitute template place holders with real values
391
+ @soap = @template_commit.result(binding)
392
+ if @debug
393
+ @log.puts "#{Time.now}: SOAP request created:"
394
+ @log.puts @soap
395
+ end
396
+
397
+ # Make the call to the Avalara service
398
+ begin
399
+ # Call using debug
400
+ if @debug
401
+ # Use Ruby built in Benchmark function to record response times
402
+ @log.puts "#{Time.now}: Calling CommitTax Service for DocCode: #{@doccode}"
403
+ time = Benchmark.measure do
404
+ # Call CommitTax Service
405
+ @response = @client.call(:commit_tax, xml: @soap).to_hash
406
+ end
407
+ @log.puts "Response times for CommitTax:"
408
+ @log.puts @responsetime_hdr
409
+ @log.puts time
410
+ else
411
+ # Call CommitTax Service
412
+ @response = @client.call(:commit_tax, xml: @soap).to_hash
413
+ end
414
+
415
+ #Strip off outer layer of the hash - not needed
416
+ #Return data to calling program
417
+ return messages_to_array(@response[:commit_tax_response][:commit_tax_result])
418
+
419
+ #Capture unexpected errors
420
+ rescue Savon::Error => error
421
+ abend(error)
422
+ end
423
+ end
424
+
425
+ ####################################################################################################
426
+ # canceltax - Calls the Avatax CancelTax Service
427
+ ####################################################################################################
428
+ def canceltax(document)
429
+
430
+ @service = 'CancelTax'
431
+
432
+ #create instance variables for each entry in input
433
+ document.each do |k,v|
434
+ instance_variable_set("@#{k}",v)
435
+ end
436
+ #set required default values for missing required inputs
437
+ #no required default values exist for canceltax
438
+
439
+ # Subsitute template place holders with real values
440
+ @soap = @template_cancel.result(binding)
441
+ if @debug
442
+ @log.puts "#{Time.now}: SOAP request created:"
443
+ @log.puts @soap
444
+ end
445
+
446
+ # Make the call to the Avalara service
447
+ begin
448
+ # Call using debug
449
+ if @debug
450
+ # Use Ruby built in Benchmark function to record response times
451
+ @log.puts "#{Time.now}: Calling CancelTax Service for DocCode: #{@doccode}"
452
+ time = Benchmark.measure do
453
+ # Call CancelTax Service
454
+ @response = @client.call(:cancel_tax, xml: @soap).to_hash
455
+ end
456
+ @log.puts "Response times for CancelTax:"
457
+ @log.puts @responsetime_hdr
458
+ @log.puts time
459
+ else
460
+ # Call CancelTax Service
461
+ @response = @client.call(:cancel_tax, xml: @soap).to_hash
462
+ end
463
+
464
+ #Strip off outer layer of the hash - not needed
465
+ return messages_to_array(@response[:cancel_tax_response][:cancel_tax_result])
466
+
467
+ #Capture unexpected errors
468
+ rescue Savon::Error => error
469
+ abend(error)
470
+ end
471
+ end
472
+
473
+ ####################################################################################################
474
+ # gettaxhistory - Calls the Avatax GetTaxHistory Service
475
+ ####################################################################################################
476
+ def gettaxhistory(document)
477
+
478
+ @service = 'GetTaxHistory'
479
+
480
+ #create instance variables for each entry in input
481
+ document.each do |k,v|
482
+ instance_variable_set("@#{k}",v)
483
+ end
484
+ #set required default values for missing required inputs
485
+ @detaillevel ||= "Tax"
486
+
487
+ # Subsitute template place holders with real values
488
+ @soap = @template_gettaxhistory.result(binding)
489
+ if @debug
490
+ @log.puts "#{Time.now}: SOAP request created:"
491
+ @log.puts @soap
492
+ end
493
+
494
+ # Make the call to the Avalara service
495
+ begin
496
+ # Call using debug
497
+ if @debug
498
+ # Use Ruby built in Benchmark function to record response times
499
+ @log.puts "#{Time.now}: Calling GetTaxHistory Service"
500
+ time = Benchmark.measure do
501
+ # Call GetTaxHistory Service
502
+ @response = @client.call(:get_tax_history, xml: @soap).to_hash
503
+ end
504
+ @log.puts "Response times for GetTaxHistory:"
505
+ @log.puts @responsetime_hdr
506
+ @log.puts time
507
+ else
508
+ # Call GetTaxHistory Service
509
+ @response = @client.call(:get_tax_history, xml: @soap).to_hash
510
+ end
511
+
512
+ #Strip off outer layer of the hash - not needed
513
+ return messages_to_array(@response[:get_tax_history_response][:get_tax_history_result])
514
+
515
+
516
+ #Capture unexpected errors
517
+ rescue Savon::Error => error
518
+ abend(error)
519
+ end
520
+ end
521
+
522
+ ####################################################################################################
523
+ # reconciletaxhistory - Calls the Avatax ReconcileTaxHistory Service
524
+ ####################################################################################################
525
+ def reconciletaxhistory(document)
526
+
527
+
528
+ @service = 'ReconcileTaxHistory'
529
+
530
+ #create instance variables for each entry in input
531
+ document.each do |k,v|
532
+ instance_variable_set("@#{k}",v)
533
+ end
534
+ #set required default values for missing required inputs
535
+ @pagesize ||= "100"
536
+ @reconciled ||= false
537
+ @doctype ||= "SalesInvoice"
538
+ @docstatus ||= "Any"
539
+ @lastdoccode ||= ''
540
+
541
+
542
+
543
+ # Subsitute template place holders with real values
544
+ @soap = @template_reconciletaxhistory.result(binding)
545
+ if @debug
546
+ @log.puts "#{Time.now}: SOAP request created:"
547
+ @log.puts @soap
548
+ end
549
+
550
+ # Make the call to the Avalara service
551
+ begin
552
+ # Call using debug
553
+ if @debug
554
+ # Use Ruby built in Benchmark function to record response times
555
+ @log.puts "#{Time.now}: Calling ReconcileTaxHistory Service"
556
+ time = Benchmark.measure do
557
+ # Call ReconcileTaxHistory Service
558
+ @response = @client.call(:reconcile_tax_history, xml: @soap).to_hash
559
+ end
560
+ @log.puts "Response times for ReconcileTaxHistory:"
561
+ @log.puts @responsetime_hdr
562
+ @log.puts time
563
+ else
564
+ # Call ReconcileTaxHistory Service
565
+ @response = @client.call(:reconcile_tax_history, xml: @soap).to_hash
566
+ end
567
+
568
+ #Strip off outer layer of the hash - not needed
569
+ return messages_to_array(@response[:reconcile_tax_history_response][:reconcile_tax_history_result])
570
+
571
+ #Capture unexpected errors
572
+ rescue Savon::Error => error
573
+ abend(error)
574
+ end
575
+ end
576
+
577
+ ############################################################################################################
578
+ # isauthorized - Verifies connectivity to the web service and returns expiry information about the service.
579
+ ############################################################################################################
580
+ def isauthorized(operation = nil)
581
+
582
+ @service = 'IsAuthorized'
583
+
584
+ #Read in the SOAP template
585
+ @operation = operation == nil ? "?" : operation
586
+
587
+ # Subsitute real vales for template place holders
588
+ @soap = @template_isauthorized.result(binding)
589
+ if @debug
590
+ @log.puts "#{Time.now}: SOAP request created:"
591
+ @log.puts @soap
592
+ end
593
+
594
+ # Make the call to the Avalara service
595
+ begin
596
+ @response = @client.call(:is_authorized, xml: @soap).to_hash
597
+
598
+ #Strip off outer layer of the hash - not needed
599
+ return messages_to_array(@response[:is_authorized_response][:is_authorized_result])
600
+
601
+ #Capture unexpected errors
602
+ rescue Savon::Error => error
603
+ abend(error)
604
+ end
605
+ end
606
+
607
+ private
608
+
609
+ ############################################################################################################
610
+ # abend - Unexpected error handling
611
+ ############################################################################################################
612
+ def abend(error)
613
+ @log.puts "An unexpected error occurred: Response from server = #{error}"
614
+ @log.puts "#{Time.now}: Error calling #{@service} service ... check that your account name and password are correct."
615
+ @response = error.to_hash
616
+ @response[:result_code] = 'Error'
617
+ @response[:summary] = @response[:fault][:faultcode]
618
+ @response[:details] = @response[:fault][:faultstring]
619
+ return messages_to_array(@response)
620
+ end
621
+ ############################################################################################################
622
+ #standardizes error message format to an array of messages - nokogiri will collapse a single element array into the response hash.
623
+ ############################################################################################################
624
+ def messages_to_array(response)
625
+ if not response[:messages].nil?
626
+ return response
627
+ end
628
+ # add the messages array to the response - if we got here, there was only one error.
629
+ response[:messages] = [{
630
+ :summary => response[:summary],
631
+ :details => response[:details],
632
+ :helplink => response[:helplink],
633
+ :refersto => response[:refersto],
634
+ :severity => response[:severity],
635
+ :source => response[:source]
636
+ }]
637
+ #remove all the error information from the hash
638
+ response[:messages][0].each do |k,v|
639
+ response.delete(k)
640
+ end
641
+ return response
642
+ end
643
+ end
649
644
  end