-
Notifications
You must be signed in to change notification settings - Fork 45
/
tax_svc.rb
108 lines (80 loc) · 2.31 KB
/
tax_svc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true
require 'logging'
# Avatax tax calculation API calls
class TaxSvc
def get_tax(request_hash)
log(__method__, request_hash)
req = client.transactions.create_or_adjust(request_hash)
response = SolidusAvataxCertified::Response::GetTax.new(req)
handle_response(response)
end
def cancel_tax(transaction_code)
log(__method__, transaction_code)
req = client.transactions.void(company_code, transaction_code)
response = SolidusAvataxCertified::Response::CancelTax.new(req)
handle_response(response)
end
def ping
logger.info 'Ping Call'
# Testing if configuration is set up properly, ping will fail if it is not
client.tax_rates.get(:by_postal_code, country: 'US', postalCode: '07801')
end
def validate_address(address)
begin
request = client.addresses.validate(address)
rescue StandardError => e
logger.error(e)
request = { 'error' => { 'message' => e } }
end
response = SolidusAvataxCertified::Response::AddressValidation.new(request)
handle_response(response)
end
protected
def handle_response(response)
result = response.result
begin
if response.error?
raise SolidusAvataxCertified::RequestError, result
end
logger.debug(result, response.description + ' Response')
rescue SolidusAvataxCertified::RequestError => e
logger.error(e.message, response.description + ' Error')
raise if raise_exceptions?
end
response
end
def logger
@logger ||= SolidusAvataxCertified::AvataxLog.new('TaxSvc class', 'Call to tax service')
end
private
def tax_calculation_enabled?
Spree::Avatax::Config.tax_calculation
end
def account_number
Spree::Avatax::Config.account
end
def license_key
Spree::Avatax::Config.license_key
end
def raise_exceptions?
Spree::Avatax::Config.raise_exceptions
end
def company_code
Spree::Avatax::Config.company_code
end
def environment
Spree::Avatax::Config.environment
end
def client
@client ||= Avatax::Client.new(
username: account_number,
password: license_key,
env: environment,
headers: AVATAX_HEADERS
)
end
def log(method, request_hash = nil)
return if request_hash.nil?
logger.debug(request_hash, "#{method} request hash")
end
end