Class: Customer::SyncWithTaxjar
Defined Under Namespace
Classes: Result
Instance Method Summary
collapse
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #process, #tagged_logger
Constructor Details
#initialize(customer, options = {}) ⇒ SyncWithTaxjar
Returns a new instance of SyncWithTaxjar.
8
9
10
11
12
13
|
# File 'app/services/customer/sync_with_taxjar.rb', line 8
def initialize(customer, options={})
@customer = customer
@options = options
@logger = options[:logger] || Rails.logger
@client = Api::Taxjar.client
end
|
Instance Method Details
#create ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/services/customer/sync_with_taxjar.rb', line 33
def create
if existing_record?
puts "Record already exists on Taxjar for customer #{taxjar_id}"
Result.new(
success: false,
message: "Record already exists on Taxjar for customer #{taxjar_id}"
)
else
new_record = @client.create_customer(details)
Result.new(
success: true,
exempt_regions: new_record.exempt_regions
)
end
end
|
#delete ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'app/services/customer/sync_with_taxjar.rb', line 75
def delete
require 'taxjar'
begin
existing_record = @client.show_customer(taxjar_id)
@client.delete_customer(taxjar_id)
Result.new(
success: true,
message: "Record on Taxjar for customer #{taxjar_id} successfully deleted"
)
rescue Taxjar::Error::NotFound
puts "Record not found on Taxjar for customer #{taxjar_id}"
Result.new(
success: false,
message: "Record not found on Taxjar for customer #{taxjar_id}"
)
end
end
|
#details ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'app/services/customer/sync_with_taxjar.rb', line 93
def details
res = {
:customer_id => taxjar_id,
:exemption_type => 'wholesale',
:name => @customer.name,
:exempt_regions => @customer.tax_exemptions.effective_on(effective_date).collect {|te| {state: te.state_code, country: te.state.country.iso}}
}
if ['USA','CAN'].include?(@customer.billing_address&.country_iso3)
res[:country] = @customer.billing_address.country.iso
res[:state] = @customer.billing_address.state_code
res[:zip] = @customer.billing_address.zip
res[:city] = @customer.billing_address.city
res[:street] = @customer.billing_address.street1
end
return res
end
|
#effective_date ⇒ Object
15
16
17
|
# File 'app/services/customer/sync_with_taxjar.rb', line 15
def effective_date
@options[:effective_date] || Date.current
end
|
#existing_record? ⇒ Boolean
49
50
51
52
53
|
# File 'app/services/customer/sync_with_taxjar.rb', line 49
def existing_record?
existing_records = @client.list_customers
existing_records.include?(taxjar_id)
end
|
#sync ⇒ Object
23
24
25
26
27
28
29
30
31
|
# File 'app/services/customer/sync_with_taxjar.rb', line 23
def sync
Retryable.retryable(tries: 3, sleep: 0.5, on: [Taxjar::Error]) do |attempt_number, exception|
if existing_record?
update
else
create
end
end
end
|
#taxjar_id ⇒ Object
19
20
21
|
# File 'app/services/customer/sync_with_taxjar.rb', line 19
def taxjar_id
"#{@customer.id}"
end
|
#update ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'app/services/customer/sync_with_taxjar.rb', line 55
def update
require 'taxjar'
begin
existing_record = @client.show_customer(taxjar_id)
updated_record = @client.update_customer(details)
Result.new(
success: true,
exempt_regions: updated_record.exempt_regions
)
rescue Taxjar::Error::NotFound
puts "Record not found on Taxjar for customer #{taxjar_id}"
Result.new(
success: false,
message: "Record not found on Taxjar for customer #{taxjar_id}"
)
end
end
|