summaryrefslogtreecommitdiff
path: root/spec/super_good/solidus_taxjar/api_params_spec.rb
blob: 3a55655fc5c3430b516a27d2c4e80de981c982d1 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'spec_helper'

RSpec.describe SuperGood::SolidusTaxJar::APIParams do
  let(:order) do
    Spree::Order.create!(
      additional_tax_total: BigDecimal("9.87"),
      item_total: BigDecimal("28.00"),
      line_items: [line_item],
      number: "R111222333",
      ship_address: ship_address,
      shipment_total: BigDecimal("3.01"),
      store: store,
      total: order_total,
      user_id: 12345
    ).tap do |order|
      order.update! completed_at: DateTime.new(2018, 3, 6, 12, 10, 33)
    end
  end
  let(:order_total) { BigDecimal("123.45") }

  let(:store) do
    Spree::Store.create!(
      cart_tax_country_iso: "US",
      code: "store",
      mail_from_address: "contact@example.com",
      name: "Default Store",
      url: "https://store.example.com"
    )
  end

  let(:ship_address) do
    Spree::Address.create!(
      address1: "475 N Beverly Dr",
      city: "Los Angeles",
      country: country_us,
      first_name: "Chuck",
      last_name: "Schuldiner",
      phone: "1-250-555-4444",
      state: state_california,
      zipcode: "90210"
    )
  end

  let(:country_us) do
    Spree::Country.create!(
      iso3: "USA",
      iso: "US",
      iso_name: "UNITED STATES",
      name: "United States",
      numcode: 840,
      states_required: true
    )
  end

  let(:state_california) do
    Spree::State.create!(
      abbr: "CA",
      country: country_us,
      name: "California"
    )
  end

  let(:line_item) do
    Spree::LineItem.new(
      additional_tax_total: 4,
      price: 10,
      promo_total: -2,
      quantity: 3,
      variant: variant
    )
  end

  let(:variant) do
    Spree::Variant.create!(
      price: 10,
      product: product,
      sku: "G00D-PR0DUCT"
    )
  end

  let(:product) do
    Spree::Product.create!(
      master: master_variant,
      name: "Product Name",
      shipping_category: shipping_category,
      tax_category: tax_category,
      variants: [master_variant]
    )
  end

  let(:shipping_category) do
    Spree::ShippingCategory.create!(name: "Default Category")
  end

  let(:tax_category) do
    Spree::TaxCategory.create!(
      is_default: true,
      name: "Default",
      tax_code: "A_GEN_TAX"
    )
  end

  let(:master_variant) do
    Spree::Variant.new(
      is_master: true,
      price: 10
    )
  end

  let(:reimbursement) do
    Spree::Reimbursement.new(
      number: "RI123123123",
      order: order,
      return_items: [
        Spree::ReturnItem.new(additional_tax_total: 0.33),
        Spree::ReturnItem.new(additional_tax_total: 33.0)
      ],
      total: 333.33
    )
  end

  describe "#order_params" do
    subject { described_class.order_params(order) }

    it "returns params for fetching the tax for the order" do
      expect(subject).to eq(
        customer_id: "12345",
        line_items: [{
          discount: 2.00,
          id: order.line_items.first.id,
          product_tax_code: "A_GEN_TAX",
          quantity: 3,
          unit_price: 10.00
        }],
        shipping: 3.01,
        to_city: "Los Angeles",
        to_country: "US",
        to_state: "CA",
        to_street: "475 N Beverly Dr",
        to_zip: "90210"
      )
    end

    context "when the line item has zero quantity" do
      let(:line_item) do
        Spree::LineItem.new(
          additional_tax_total: 4,
          price: 10,
          promo_total: -2,
          quantity: 0,
          variant: variant
        )
      end

      it "excludes the line item" do
        expect(subject).to eq(
          customer_id: "12345",
          line_items: [],
          shipping: 3.01,
          to_city: "Los Angeles",
          to_country: "US",
          to_state: "CA",
          to_street: "475 N Beverly Dr",
          to_zip: "90210"
        )
      end
    end
  end

  describe "#address_params" do
    subject { described_class.address_params(ship_address) }

    it "returns params for fetching the tax info for that address" do
      expect(subject).to eq([
        "90210",
        {
          city: "Los Angeles",
          country: "US",
          state: "CA",
          street: "475 N Beverly Dr"
        }
      ])
    end
  end

  describe "#transaction_params" do
    subject { described_class.transaction_params(order) }

    it "returns params for creating/updating an order transaction" do
      expect(subject).to eq({
        amount: BigDecimal("113.58"),
        customer_id: "12345",
        line_items: [{
          discount: 2,
          id: line_item.id,
          product_identifier: "G00D-PR0DUCT",
          product_tax_code: "A_GEN_TAX",
          quantity: 3,
          sales_tax: 4,
          unit_price: 10
        }],
        sales_tax: BigDecimal("9.87"),
        shipping: BigDecimal("3.01"),
        to_city: "Los Angeles",
        to_country: "US",
        to_state: "CA",
        to_street: "475 N Beverly Dr",
        to_zip: "90210",
        transaction_date: "2018-03-06T12:10:33Z",
        transaction_id: "R111222333"
      })
    end

    context "when the order is adjusted to 0" do
      let(:order_total) { BigDecimal("0") }

      it "sends the order total as zero" do
        expect(subject[:amount]).to be_zero
      end

      it "sends the sales tax total as zero" do
        expect(subject[:sales_tax]).to be_zero
      end

      it "sends the sales tax on the line items as zero" do
        expect(subject[:line_items]).to contain_exactly({
          discount: 2,
          id: line_item.id,
          product_identifier: "G00D-PR0DUCT",
          product_tax_code: "A_GEN_TAX",
          quantity: 3,
          sales_tax: 0,
          unit_price: 10
        })
      end
    end

    context "when the line item has 0 quantity" do
      let(:line_item) do
        Spree::LineItem.new(
          additional_tax_total: 4,
          price: 10,
          promo_total: -2,
          quantity: 0,
          variant: variant
        )
      end

      it "excludes the line item" do
        expect(subject).to eq({
          amount: BigDecimal("113.58"),
          customer_id: "12345",
          line_items: [],
          sales_tax: BigDecimal("9.87"),
          shipping: BigDecimal("3.01"),
          to_city: "Los Angeles",
          to_country: "US",
          to_state: "CA",
          to_street: "475 N Beverly Dr",
          to_zip: "90210",
          transaction_date: "2018-03-06T12:10:33Z",
          transaction_id: "R111222333",
        })
      end
    end
  end

  describe "#refund_params" do
    subject { described_class.refund_params(reimbursement) }

    it "returns params for creating/updating a refund" do
      expect(subject).to eq({
        amount: BigDecimal("300.00"),
        sales_tax: BigDecimal("33.33"),
        shipping: 0,
        to_city: "Los Angeles",
        to_country: "US",
        to_state: "CA",
        to_street: "475 N Beverly Dr",
        to_zip: "90210",
        transaction_date: "2018-03-06T12:10:33Z",
        transaction_id: "RI123123123",
        transaction_reference_id: "R111222333"
      })
    end
  end
end