super_good-solidus_taxjar 0.15.2 → 0.18.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +16 -0
- data/.gitignore +19 -13
- data/.travis.yml +12 -5
- data/CHANGELOG.md +71 -0
- data/Gemfile +23 -5
- data/LICENSE +26 -0
- data/PULL_REQUEST_TEMPLATE.md +20 -0
- data/README.md +34 -9
- data/Rakefile +4 -17
- data/app/controllers/spree/admin/taxjar_settings_controller.rb +8 -0
- data/app/decorators/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb +18 -0
- data/app/overrides/spree/admin/shared/_configuration_menu.rb +11 -0
- data/app/views/spree/admin/taxjar_settings/show.html.erb +13 -0
- data/bin/rails +7 -0
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +84 -0
- data/config/routes.rb +7 -0
- data/lib/super_good-solidus_taxjar.rb +4 -0
- data/lib/super_good/engine.rb +10 -0
- data/lib/super_good/solidus_taxjar.rb +19 -7
- data/lib/super_good/solidus_taxjar/addresses.rb +63 -0
- data/lib/super_good/solidus_taxjar/api.rb +27 -10
- data/lib/super_good/solidus_taxjar/api_params.rb +26 -8
- data/lib/super_good/solidus_taxjar/calculator_helper.rb +38 -0
- data/lib/super_good/solidus_taxjar/discount_calculator.rb +1 -1
- data/lib/super_good/solidus_taxjar/tax_calculator.rb +17 -50
- data/lib/super_good/solidus_taxjar/tax_rate_calculator.rb +35 -0
- data/lib/super_good/solidus_taxjar/version.rb +2 -2
- data/spec/features/spree/admin/taxjar_settings_spec.rb +48 -0
- data/spec/models/spree/order_updater_spec.rb +12 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/super_good/solidus_taxjar/addresses_spec.rb +288 -0
- data/spec/super_good/solidus_taxjar/api_params_spec.rb +434 -0
- data/spec/super_good/solidus_taxjar/api_spec.rb +222 -0
- data/spec/super_good/solidus_taxjar/discount_calculator_spec.rb +13 -0
- data/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +332 -0
- data/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb +116 -0
- data/spec/super_good/solidus_taxjar_spec.rb +92 -0
- data/super_good-solidus_taxjar.gemspec +17 -14
- metadata +46 -9
- data/LICENSE.txt +0 -21
@@ -0,0 +1,35 @@
|
|
1
|
+
module SuperGood
|
2
|
+
module SolidusTaxjar
|
3
|
+
class TaxRateCalculator
|
4
|
+
include CalculatorHelper
|
5
|
+
def initialize(address, api: SuperGood::SolidusTaxjar.api)
|
6
|
+
@address = address
|
7
|
+
@api = api
|
8
|
+
end
|
9
|
+
|
10
|
+
def calculate
|
11
|
+
return no_rate if SuperGood::SolidusTaxjar.test_mode
|
12
|
+
return no_rate if incomplete_address?(address)
|
13
|
+
return no_rate unless taxable_address?(address)
|
14
|
+
cache do
|
15
|
+
api.tax_rate_for(address).to_d
|
16
|
+
end
|
17
|
+
rescue => e
|
18
|
+
exception_handler.call(e)
|
19
|
+
no_rate
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :address, :api
|
25
|
+
|
26
|
+
def no_rate
|
27
|
+
BigDecimal(0)
|
28
|
+
end
|
29
|
+
|
30
|
+
def cache_key
|
31
|
+
SuperGood::SolidusTaxjar.cache_key.call(address)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.feature 'Admin TaxJar Settings', js: true do
|
4
|
+
stub_authorization!
|
5
|
+
|
6
|
+
background do
|
7
|
+
create :store, default: true
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Taxjar settings tab" do
|
11
|
+
before do
|
12
|
+
allow(ENV).to receive(:[]).and_call_original
|
13
|
+
allow(ENV).to receive(:[]).with("TAXJAR_API_KEY").and_return(api_token)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "Taxjar API token is set" do
|
17
|
+
let(:api_token) { "token" }
|
18
|
+
|
19
|
+
it "shows a blank settings page" do
|
20
|
+
|
21
|
+
visit "/admin"
|
22
|
+
click_on "Settings"
|
23
|
+
expect(page).to have_content("Taxes")
|
24
|
+
click_on "Taxes"
|
25
|
+
expect(page).to have_content("TaxJar Settings")
|
26
|
+
click_on "TaxJar Settings"
|
27
|
+
expect(page).not_to have_content "You must provide a TaxJar API token"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "Taxjar API token isn't set" do
|
32
|
+
let(:api_token) { nil }
|
33
|
+
|
34
|
+
it "shows a descriptive error message" do
|
35
|
+
visit "/admin"
|
36
|
+
click_on "Settings"
|
37
|
+
expect(page).to have_content("Taxes")
|
38
|
+
click_on "Taxes"
|
39
|
+
expect(page).to have_content("TaxJar Settings")
|
40
|
+
click_on "TaxJar Settings"
|
41
|
+
expect(page).to have_content "You must provide a TaxJar API token"
|
42
|
+
|
43
|
+
expect(page).to have_link(href: "https://app.taxjar.com/api_sign_up")
|
44
|
+
expect(page).to have_link(href: "https://support.taxjar.com/article/160-how-do-i-get-a-sales-tax-api-token")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
RSpec.describe Spree::OrderUpdater do
|
2
|
+
describe '#update' do
|
3
|
+
it 'fires the order_recalculated event exactly once' do
|
4
|
+
stub_const('Spree::Event', class_spy(Spree::Event))
|
5
|
+
order = create(:order)
|
6
|
+
|
7
|
+
described_class.new(order).update
|
8
|
+
|
9
|
+
expect(Spree::Event).to have_received(:fire).with('order_recalculated', order: order).once
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Configure Rails Environment
|
4
|
+
ENV["RAILS_ENV"] = "test"
|
5
|
+
|
6
|
+
require File.expand_path("dummy/config/environment.rb", __dir__).tap { |file|
|
7
|
+
# Create the dummy app if it's still missing.
|
8
|
+
system "bin/rake extension:test_app" unless File.exist? file
|
9
|
+
}
|
10
|
+
|
11
|
+
require "solidus_dev_support/rspec/feature_helper"
|
12
|
+
|
13
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
14
|
+
# in spec/support/ and its subdirectories.
|
15
|
+
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
config.infer_spec_type_from_file_location!
|
19
|
+
config.use_transactional_fixtures = false
|
20
|
+
end
|
@@ -0,0 +1,288 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe SuperGood::SolidusTaxjar::Addresses do
|
4
|
+
describe "#normalize" do
|
5
|
+
subject { described_class.new(api: dummy_api).normalize(spree_address) }
|
6
|
+
|
7
|
+
let(:spree_address) {
|
8
|
+
create(
|
9
|
+
:address,
|
10
|
+
address1: "475 North Beverly Drive",
|
11
|
+
city: "Los Angeles",
|
12
|
+
country: country_us,
|
13
|
+
first_name: "Chuck",
|
14
|
+
last_name: "Schuldiner",
|
15
|
+
phone: "1-250-555-4444",
|
16
|
+
state: state_california,
|
17
|
+
zipcode: "90210"
|
18
|
+
)
|
19
|
+
}
|
20
|
+
|
21
|
+
let(:country_us) {
|
22
|
+
Spree::Country.create!(
|
23
|
+
iso3: "USA",
|
24
|
+
iso: "US",
|
25
|
+
iso_name: "UNITED STATES",
|
26
|
+
name: "United States",
|
27
|
+
numcode: 840,
|
28
|
+
states_required: true
|
29
|
+
)
|
30
|
+
}
|
31
|
+
|
32
|
+
let(:state_california) {
|
33
|
+
Spree::State.create!(
|
34
|
+
abbr: "CA",
|
35
|
+
country: country_us,
|
36
|
+
name: "California"
|
37
|
+
)
|
38
|
+
}
|
39
|
+
|
40
|
+
let(:dummy_api) {
|
41
|
+
instance_double ::SuperGood::SolidusTaxjar::Api
|
42
|
+
}
|
43
|
+
|
44
|
+
context "when there are no possibilities for the address" do
|
45
|
+
before do
|
46
|
+
allow(dummy_api)
|
47
|
+
.to receive(:validate_spree_address)
|
48
|
+
.with(spree_address)
|
49
|
+
.and_raise(Taxjar::Error::NotFound)
|
50
|
+
end
|
51
|
+
|
52
|
+
it { is_expected.to be_nil }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when there is one possibility for the address" do
|
56
|
+
before do
|
57
|
+
allow(dummy_api)
|
58
|
+
.to receive(:validate_spree_address)
|
59
|
+
.with(spree_address)
|
60
|
+
.and_return(results)
|
61
|
+
end
|
62
|
+
|
63
|
+
let(:results) {
|
64
|
+
[
|
65
|
+
Taxjar::Address.new(
|
66
|
+
country: "US",
|
67
|
+
state: "CA",
|
68
|
+
zip: "90210-4606",
|
69
|
+
city: "Beverly Hills",
|
70
|
+
street: "475 N Beverly Dr"
|
71
|
+
)
|
72
|
+
]
|
73
|
+
}
|
74
|
+
|
75
|
+
it "returns a sanitized address" do
|
76
|
+
expect(subject).to eq(
|
77
|
+
build(
|
78
|
+
:address,
|
79
|
+
address1: "475 N Beverly Dr",
|
80
|
+
city: "Beverly Hills",
|
81
|
+
country: country_us,
|
82
|
+
first_name: "Chuck",
|
83
|
+
last_name: "Schuldiner",
|
84
|
+
phone: "1-250-555-4444",
|
85
|
+
state: state_california,
|
86
|
+
zipcode: "90210-4606"
|
87
|
+
)
|
88
|
+
)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "when there are multiple possibilities for the address" do
|
93
|
+
before do
|
94
|
+
allow(dummy_api)
|
95
|
+
.to receive(:validate_spree_address)
|
96
|
+
.with(spree_address)
|
97
|
+
.and_return(results)
|
98
|
+
end
|
99
|
+
|
100
|
+
let(:results) {
|
101
|
+
[
|
102
|
+
Taxjar::Address.new(
|
103
|
+
country: "US",
|
104
|
+
state: "CA",
|
105
|
+
zip: "90210-4606",
|
106
|
+
city: "Beverly Hills",
|
107
|
+
street: "475 N Beverly Dr"
|
108
|
+
),
|
109
|
+
Taxjar::Address.new(
|
110
|
+
country: "US",
|
111
|
+
state: "AZ",
|
112
|
+
zip: "90213-4606",
|
113
|
+
city: "Beverly Hills",
|
114
|
+
street: "475 N Beverly Dr"
|
115
|
+
)
|
116
|
+
]
|
117
|
+
}
|
118
|
+
|
119
|
+
it "uses the first result to sanitize the addresses" do
|
120
|
+
expect(subject).to eq(
|
121
|
+
build(
|
122
|
+
:address,
|
123
|
+
address1: "475 N Beverly Dr",
|
124
|
+
city: "Beverly Hills",
|
125
|
+
country: country_us,
|
126
|
+
first_name: "Chuck",
|
127
|
+
last_name: "Schuldiner",
|
128
|
+
phone: "1-250-555-4444",
|
129
|
+
state: state_california,
|
130
|
+
zipcode: "90210-4606"
|
131
|
+
)
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#normalize" do
|
138
|
+
subject { described_class.new(api: dummy_api).possibilities(spree_address) }
|
139
|
+
|
140
|
+
let(:spree_address) {
|
141
|
+
create(
|
142
|
+
:address,
|
143
|
+
address1: "475 North Beverly Drive",
|
144
|
+
city: "Los Angeles",
|
145
|
+
country: country_us,
|
146
|
+
first_name: "Chuck",
|
147
|
+
last_name: "Schuldiner",
|
148
|
+
phone: "1-250-555-4444",
|
149
|
+
state: state_california,
|
150
|
+
zipcode: "90210"
|
151
|
+
)
|
152
|
+
}
|
153
|
+
|
154
|
+
let(:country_us) {
|
155
|
+
Spree::Country.create!(
|
156
|
+
iso3: "USA",
|
157
|
+
iso: "US",
|
158
|
+
iso_name: "UNITED STATES",
|
159
|
+
name: "United States",
|
160
|
+
numcode: 840,
|
161
|
+
states_required: true
|
162
|
+
)
|
163
|
+
}
|
164
|
+
|
165
|
+
let(:state_california) {
|
166
|
+
Spree::State.create!(
|
167
|
+
abbr: "CA",
|
168
|
+
country: country_us,
|
169
|
+
name: "California"
|
170
|
+
)
|
171
|
+
}
|
172
|
+
|
173
|
+
let(:dummy_api) {
|
174
|
+
instance_double ::SuperGood::SolidusTaxjar::Api
|
175
|
+
}
|
176
|
+
|
177
|
+
context "when there are no possibilities for the address" do
|
178
|
+
before do
|
179
|
+
allow(dummy_api)
|
180
|
+
.to receive(:validate_spree_address)
|
181
|
+
.with(spree_address)
|
182
|
+
.and_raise(Taxjar::Error::NotFound)
|
183
|
+
end
|
184
|
+
|
185
|
+
it { is_expected.to be_empty }
|
186
|
+
end
|
187
|
+
|
188
|
+
context "when there is one possibility for the address" do
|
189
|
+
before do
|
190
|
+
allow(dummy_api)
|
191
|
+
.to receive(:validate_spree_address)
|
192
|
+
.with(spree_address)
|
193
|
+
.and_return(results)
|
194
|
+
end
|
195
|
+
|
196
|
+
let(:results) {
|
197
|
+
[
|
198
|
+
Taxjar::Address.new(
|
199
|
+
country: "US",
|
200
|
+
state: "CA",
|
201
|
+
zip: "90210-4606",
|
202
|
+
city: "Beverly Hills",
|
203
|
+
street: "475 N Beverly Dr"
|
204
|
+
)
|
205
|
+
]
|
206
|
+
}
|
207
|
+
|
208
|
+
it "returns the possibilities" do
|
209
|
+
expect(subject).to eq([
|
210
|
+
build(
|
211
|
+
:address,
|
212
|
+
address1: "475 N Beverly Dr",
|
213
|
+
city: "Beverly Hills",
|
214
|
+
country: country_us,
|
215
|
+
first_name: "Chuck",
|
216
|
+
last_name: "Schuldiner",
|
217
|
+
phone: "1-250-555-4444",
|
218
|
+
state: state_california,
|
219
|
+
zipcode: "90210-4606"
|
220
|
+
)
|
221
|
+
])
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
context "when there are multiple possibilities for the address" do
|
226
|
+
before do
|
227
|
+
allow(dummy_api)
|
228
|
+
.to receive(:validate_spree_address)
|
229
|
+
.with(spree_address)
|
230
|
+
.and_return(results)
|
231
|
+
end
|
232
|
+
|
233
|
+
let(:results) {
|
234
|
+
[
|
235
|
+
Taxjar::Address.new(
|
236
|
+
country: "US",
|
237
|
+
state: "CA",
|
238
|
+
zip: "90210-4606",
|
239
|
+
city: "Beverly Hills",
|
240
|
+
street: "475 N Beverly Dr"
|
241
|
+
),
|
242
|
+
Taxjar::Address.new(
|
243
|
+
country: "US",
|
244
|
+
state: "AZ",
|
245
|
+
zip: "90213-1234",
|
246
|
+
city: "Phoenix",
|
247
|
+
street: "473 N Beverly Dr"
|
248
|
+
)
|
249
|
+
]
|
250
|
+
}
|
251
|
+
|
252
|
+
let!(:state_arizona) {
|
253
|
+
Spree::State.create!(
|
254
|
+
abbr: "AZ",
|
255
|
+
country: country_us,
|
256
|
+
name: "Arizona"
|
257
|
+
)
|
258
|
+
}
|
259
|
+
|
260
|
+
it "returns all the possibilities" do
|
261
|
+
expect(subject).to eq([
|
262
|
+
build(
|
263
|
+
:address,
|
264
|
+
address1: "475 N Beverly Dr",
|
265
|
+
city: "Beverly Hills",
|
266
|
+
country: country_us,
|
267
|
+
first_name: "Chuck",
|
268
|
+
last_name: "Schuldiner",
|
269
|
+
phone: "1-250-555-4444",
|
270
|
+
state: state_california,
|
271
|
+
zipcode: "90210-4606"
|
272
|
+
),
|
273
|
+
build(
|
274
|
+
:address,
|
275
|
+
address1: "473 N Beverly Dr",
|
276
|
+
city: "Phoenix",
|
277
|
+
country: country_us,
|
278
|
+
first_name: "Chuck",
|
279
|
+
last_name: "Schuldiner",
|
280
|
+
phone: "1-250-555-4444",
|
281
|
+
state: state_arizona,
|
282
|
+
zipcode: "90213-1234"
|
283
|
+
)
|
284
|
+
])
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
@@ -0,0 +1,434 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe SuperGood::SolidusTaxjar::ApiParams do
|
4
|
+
let(:order) do
|
5
|
+
Spree::Order.create!(
|
6
|
+
additional_tax_total: BigDecimal("9.87"),
|
7
|
+
item_total: BigDecimal("28.00"),
|
8
|
+
line_items: [line_item],
|
9
|
+
number: "R111222333",
|
10
|
+
ship_address: ship_address,
|
11
|
+
store: store,
|
12
|
+
total: order_total,
|
13
|
+
shipments: [shipment],
|
14
|
+
user_id: 12345
|
15
|
+
).tap do |order|
|
16
|
+
order.update! completed_at: DateTime.new(2018, 3, 6, 12, 10, 33)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
let(:order_total) { BigDecimal("123.45") }
|
20
|
+
|
21
|
+
let(:store) do
|
22
|
+
Spree::Store.create!(
|
23
|
+
cart_tax_country_iso: "US",
|
24
|
+
code: "store",
|
25
|
+
mail_from_address: "contact@example.com",
|
26
|
+
name: "Default Store",
|
27
|
+
url: "https://store.example.com"
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:ship_address) do
|
32
|
+
Spree::Address.create!(
|
33
|
+
address1: "475 N Beverly Dr",
|
34
|
+
city: "Los Angeles",
|
35
|
+
country: country_us,
|
36
|
+
first_name: "Chuck",
|
37
|
+
last_name: "Schuldiner",
|
38
|
+
phone: "1-250-555-4444",
|
39
|
+
state: state_california,
|
40
|
+
zipcode: "90210"
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:country_us) do
|
45
|
+
Spree::Country.create!(
|
46
|
+
iso3: "USA",
|
47
|
+
iso: "US",
|
48
|
+
iso_name: "UNITED STATES",
|
49
|
+
name: "United States",
|
50
|
+
numcode: 840,
|
51
|
+
states_required: true
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
let(:state_california) do
|
56
|
+
Spree::State.create!(
|
57
|
+
abbr: "CA",
|
58
|
+
country: country_us,
|
59
|
+
name: "California"
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
let(:line_item) do
|
64
|
+
Spree::LineItem.new(
|
65
|
+
additional_tax_total: 4,
|
66
|
+
price: 10,
|
67
|
+
promo_total: -2,
|
68
|
+
quantity: 3,
|
69
|
+
variant: variant
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:variant) do
|
74
|
+
Spree::Variant.create!(
|
75
|
+
price: 10,
|
76
|
+
product: product,
|
77
|
+
sku: "G00D-PR0DUCT"
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
let(:product) do
|
82
|
+
Spree::Product.create!(
|
83
|
+
master: master_variant,
|
84
|
+
name: "Product Name",
|
85
|
+
shipping_category: shipping_category,
|
86
|
+
tax_category: tax_category,
|
87
|
+
variants: [master_variant]
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
let(:shipping_category) do
|
92
|
+
Spree::ShippingCategory.create!(name: "Default Category")
|
93
|
+
end
|
94
|
+
|
95
|
+
let(:tax_category) do
|
96
|
+
Spree::TaxCategory.create!(
|
97
|
+
is_default: true,
|
98
|
+
name: "Default",
|
99
|
+
tax_code: "A_GEN_TAX"
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
let(:master_variant) do
|
104
|
+
Spree::Variant.new(
|
105
|
+
is_master: true,
|
106
|
+
price: 10
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
let(:reimbursement) do
|
111
|
+
Spree::Reimbursement.new(
|
112
|
+
number: "RI123123123",
|
113
|
+
order: order,
|
114
|
+
return_items: [
|
115
|
+
Spree::ReturnItem.new(additional_tax_total: 0.33),
|
116
|
+
Spree::ReturnItem.new(additional_tax_total: 33.0)
|
117
|
+
],
|
118
|
+
total: 333.33
|
119
|
+
)
|
120
|
+
end
|
121
|
+
|
122
|
+
let(:shipment) { Spree::Shipment.create!(cost: BigDecimal("3.01")) }
|
123
|
+
|
124
|
+
describe "#order_params" do
|
125
|
+
subject { described_class.order_params(order) }
|
126
|
+
|
127
|
+
it "returns params for fetching the tax for the order" do
|
128
|
+
expect(subject).to eq(
|
129
|
+
customer_id: "12345",
|
130
|
+
line_items: [{
|
131
|
+
discount: 2.00,
|
132
|
+
id: order.line_items.first.id,
|
133
|
+
product_tax_code: "A_GEN_TAX",
|
134
|
+
quantity: 3,
|
135
|
+
unit_price: 10.00
|
136
|
+
}],
|
137
|
+
shipping: 3.01,
|
138
|
+
to_city: "Los Angeles",
|
139
|
+
to_country: "US",
|
140
|
+
to_state: "CA",
|
141
|
+
to_street: "475 N Beverly Dr",
|
142
|
+
to_zip: "90210"
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
146
|
+
context "when custom params are used" do
|
147
|
+
around do |example|
|
148
|
+
default = SuperGood::SolidusTaxjar.custom_order_params
|
149
|
+
SuperGood::SolidusTaxjar.custom_order_params = ->(order) {
|
150
|
+
{
|
151
|
+
nexus_addresses: [
|
152
|
+
{
|
153
|
+
id: "Main Location",
|
154
|
+
country: "AU",
|
155
|
+
zip: "NSW 2000",
|
156
|
+
city: "Sydney",
|
157
|
+
street: "483 George St"
|
158
|
+
}
|
159
|
+
]
|
160
|
+
}
|
161
|
+
}
|
162
|
+
example.run
|
163
|
+
SuperGood::SolidusTaxjar.custom_order_params = default
|
164
|
+
end
|
165
|
+
|
166
|
+
it "returns params for fetching the tax for the order" do
|
167
|
+
expect(subject).to eq(
|
168
|
+
customer_id: "12345",
|
169
|
+
line_items: [{
|
170
|
+
discount: 2.00,
|
171
|
+
id: order.line_items.first.id,
|
172
|
+
product_tax_code: "A_GEN_TAX",
|
173
|
+
quantity: 3,
|
174
|
+
unit_price: 10.00
|
175
|
+
}],
|
176
|
+
nexus_addresses: [{
|
177
|
+
id: "Main Location",
|
178
|
+
country: "AU",
|
179
|
+
zip: "NSW 2000",
|
180
|
+
city: "Sydney",
|
181
|
+
street: "483 George St"
|
182
|
+
}],
|
183
|
+
shipping: 3.01,
|
184
|
+
to_city: "Los Angeles",
|
185
|
+
to_country: "US",
|
186
|
+
to_state: "CA",
|
187
|
+
to_street: "475 N Beverly Dr",
|
188
|
+
to_zip: "90210"
|
189
|
+
)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
context "when the line item has zero quantity" do
|
194
|
+
let(:line_item) do
|
195
|
+
Spree::LineItem.new(
|
196
|
+
additional_tax_total: 4,
|
197
|
+
price: 10,
|
198
|
+
promo_total: -2,
|
199
|
+
quantity: 0,
|
200
|
+
variant: variant
|
201
|
+
)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "excludes the line item" do
|
205
|
+
expect(subject).to eq(
|
206
|
+
customer_id: "12345",
|
207
|
+
line_items: [],
|
208
|
+
shipping: 3.01,
|
209
|
+
to_city: "Los Angeles",
|
210
|
+
to_country: "US",
|
211
|
+
to_state: "CA",
|
212
|
+
to_street: "475 N Beverly Dr",
|
213
|
+
to_zip: "90210"
|
214
|
+
)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "#address_params" do
|
220
|
+
subject { described_class.address_params(ship_address) }
|
221
|
+
|
222
|
+
it "returns params for fetching the tax info for that address" do
|
223
|
+
expect(subject).to eq([
|
224
|
+
"90210",
|
225
|
+
{
|
226
|
+
city: "Los Angeles",
|
227
|
+
country: "US",
|
228
|
+
state: "CA",
|
229
|
+
street: "475 N Beverly Dr"
|
230
|
+
}
|
231
|
+
])
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "#tax_rate_address_params" do
|
236
|
+
subject { described_class.tax_rate_address_params(ship_address) }
|
237
|
+
|
238
|
+
it "returns params for fetching the tax rate for that address" do
|
239
|
+
expect(subject).to eq(
|
240
|
+
{
|
241
|
+
amount: 100,
|
242
|
+
shipping: 0,
|
243
|
+
to_city: "Los Angeles",
|
244
|
+
to_country: "US",
|
245
|
+
to_state: "CA",
|
246
|
+
to_street: "475 N Beverly Dr",
|
247
|
+
to_zip: "90210"
|
248
|
+
}
|
249
|
+
)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe "#transaction_params" do
|
254
|
+
subject { described_class.transaction_params(order) }
|
255
|
+
|
256
|
+
it "returns params for creating/updating an order transaction" do
|
257
|
+
expect(subject).to eq({
|
258
|
+
amount: BigDecimal("113.58"),
|
259
|
+
customer_id: "12345",
|
260
|
+
line_items: [{
|
261
|
+
discount: 2,
|
262
|
+
id: line_item.id,
|
263
|
+
product_identifier: "G00D-PR0DUCT",
|
264
|
+
product_tax_code: "A_GEN_TAX",
|
265
|
+
quantity: 3,
|
266
|
+
sales_tax: 4,
|
267
|
+
unit_price: 10
|
268
|
+
}],
|
269
|
+
sales_tax: BigDecimal("9.87"),
|
270
|
+
shipping: BigDecimal("3.01"),
|
271
|
+
to_city: "Los Angeles",
|
272
|
+
to_country: "US",
|
273
|
+
to_state: "CA",
|
274
|
+
to_street: "475 N Beverly Dr",
|
275
|
+
to_zip: "90210",
|
276
|
+
transaction_date: "2018-03-06T12:10:33Z",
|
277
|
+
transaction_id: "R111222333"
|
278
|
+
})
|
279
|
+
end
|
280
|
+
|
281
|
+
context "when the order is adjusted to 0" do
|
282
|
+
let(:order_total) { BigDecimal("0") }
|
283
|
+
|
284
|
+
it "sends the order total as zero" do
|
285
|
+
expect(subject[:amount]).to be_zero
|
286
|
+
end
|
287
|
+
|
288
|
+
it "sends the sales tax total as zero" do
|
289
|
+
expect(subject[:sales_tax]).to be_zero
|
290
|
+
end
|
291
|
+
|
292
|
+
it "sends the sales tax on the line items as zero" do
|
293
|
+
expect(subject[:line_items]).to contain_exactly({
|
294
|
+
discount: 2,
|
295
|
+
id: line_item.id,
|
296
|
+
product_identifier: "G00D-PR0DUCT",
|
297
|
+
product_tax_code: "A_GEN_TAX",
|
298
|
+
quantity: 3,
|
299
|
+
sales_tax: 0,
|
300
|
+
unit_price: 10
|
301
|
+
})
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
context "when the line item has 0 quantity" do
|
306
|
+
let(:line_item) do
|
307
|
+
Spree::LineItem.new(
|
308
|
+
additional_tax_total: 4,
|
309
|
+
price: 10,
|
310
|
+
promo_total: -2,
|
311
|
+
quantity: 0,
|
312
|
+
variant: variant
|
313
|
+
)
|
314
|
+
end
|
315
|
+
|
316
|
+
it "excludes the line item" do
|
317
|
+
expect(subject).to eq({
|
318
|
+
amount: BigDecimal("113.58"),
|
319
|
+
customer_id: "12345",
|
320
|
+
line_items: [],
|
321
|
+
sales_tax: BigDecimal("9.87"),
|
322
|
+
shipping: BigDecimal("3.01"),
|
323
|
+
to_city: "Los Angeles",
|
324
|
+
to_country: "US",
|
325
|
+
to_state: "CA",
|
326
|
+
to_street: "475 N Beverly Dr",
|
327
|
+
to_zip: "90210",
|
328
|
+
transaction_date: "2018-03-06T12:10:33Z",
|
329
|
+
transaction_id: "R111222333"
|
330
|
+
})
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
describe "#refund_params" do
|
336
|
+
subject { described_class.refund_params(reimbursement) }
|
337
|
+
|
338
|
+
it "returns params for creating/updating a refund" do
|
339
|
+
expect(subject).to eq({
|
340
|
+
amount: BigDecimal("300.00"),
|
341
|
+
sales_tax: BigDecimal("33.33"),
|
342
|
+
shipping: 0,
|
343
|
+
to_city: "Los Angeles",
|
344
|
+
to_country: "US",
|
345
|
+
to_state: "CA",
|
346
|
+
to_street: "475 N Beverly Dr",
|
347
|
+
to_zip: "90210",
|
348
|
+
transaction_date: "2018-03-06T12:10:33Z",
|
349
|
+
transaction_id: "RI123123123",
|
350
|
+
transaction_reference_id: "R111222333"
|
351
|
+
})
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
describe "#validate_address_params" do
|
356
|
+
subject { described_class.validate_address_params(ship_address) }
|
357
|
+
|
358
|
+
it "returns params for validating an address" do
|
359
|
+
expect(subject).to eq({
|
360
|
+
country: "US",
|
361
|
+
state: "CA",
|
362
|
+
zip: "90210",
|
363
|
+
city: "Los Angeles",
|
364
|
+
street: "475 N Beverly Dr"
|
365
|
+
})
|
366
|
+
end
|
367
|
+
|
368
|
+
context "with an address without a state" do
|
369
|
+
let(:ship_address) do
|
370
|
+
Spree::Address.create!(
|
371
|
+
address1: "72 High St",
|
372
|
+
city: "Birmingham",
|
373
|
+
country: country_uk,
|
374
|
+
first_name: "Chuck",
|
375
|
+
last_name: "Schuldiner",
|
376
|
+
phone: "1-250-555-4444",
|
377
|
+
state_name: "West Midlands",
|
378
|
+
zipcode: "B4 7TA"
|
379
|
+
)
|
380
|
+
end
|
381
|
+
|
382
|
+
let(:country_uk) do
|
383
|
+
Spree::Country.create!(
|
384
|
+
iso3: "GBR",
|
385
|
+
iso: "GB",
|
386
|
+
iso_name: "UNITED KINGDOM",
|
387
|
+
name: "United Kingdom",
|
388
|
+
numcode: 826,
|
389
|
+
states_required: false
|
390
|
+
)
|
391
|
+
end
|
392
|
+
|
393
|
+
it "uses the state_name to build address params" do
|
394
|
+
expect(subject).to eq({
|
395
|
+
country: "GB",
|
396
|
+
state: "West Midlands",
|
397
|
+
zip: "B4 7TA",
|
398
|
+
city: "Birmingham",
|
399
|
+
street: "72 High St"
|
400
|
+
})
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
context "an address with address2" do
|
405
|
+
let(:ship_address) do
|
406
|
+
Spree::Address.create!(
|
407
|
+
address1: "1 World Trade CTR",
|
408
|
+
address2: "STE 45A",
|
409
|
+
city: "New York",
|
410
|
+
country: country_us,
|
411
|
+
first_name: "Chuck",
|
412
|
+
last_name: "Schuldiner",
|
413
|
+
phone: "1-250-555-4444",
|
414
|
+
state: Spree::State.create!(
|
415
|
+
abbr: "NY",
|
416
|
+
country: country_us,
|
417
|
+
name: "New York"
|
418
|
+
),
|
419
|
+
zipcode: "10007"
|
420
|
+
)
|
421
|
+
end
|
422
|
+
|
423
|
+
it "concatenates address1 and address2 into the street parameter" do
|
424
|
+
expect(subject).to eq({
|
425
|
+
country: "US",
|
426
|
+
state: "NY",
|
427
|
+
zip: "10007",
|
428
|
+
city: "New York",
|
429
|
+
street: "1 World Trade CTR STE 45A"
|
430
|
+
})
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|