summaryrefslogtreecommitdiff
path: root/spec/super_good/solidus_taxjar/api_params_spec.rb
blob: abc8a23144ac224c9a3dbaf48e4785f787199f21 (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
require 'spec_helper'

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

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

  let(:ship_address) do
    Spree::Address.create!(
      country: country_us,
      state: state_california,
      zipcode: "90210",
      city: "Los Angeles",
      address1: "475 N Beverly Dr",

      first_name: "Chuck",
      last_name: "Schuldiner",
      phone: "1-250-555-4444"
    )
  end

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

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

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

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

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

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

  let(:tax_category) do
    Spree::TaxCategory.create!(
      name: "Default",
      is_default: true,
      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(
      order: order,
      total: 333.33,
      number: "RI123123123",
      return_items: [
        Spree::ReturnItem.new(additional_tax_total: 0.33),
        Spree::ReturnItem.new(additional_tax_total: 33.0)
      ]
    )
  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(
        to_country: "US",
        to_zip: "90210",
        to_city: "Los Angeles",
        to_state: "CA",
        to_street: "475 N Beverly Dr",

        shipping: 3.01,

        line_items: [{
          id: order.line_items.first.id,
          quantity: 3,
          unit_price: 10.00,
          discount: 2.00,
          product_tax_code: "A_GEN_TAX"
        }]
      )
    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"),
       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",
       line_items: [{
         id: line_item.id,
         quantity: 3,
         product_identifier: "G00D-PR0DUCT",
         product_tax_code: "A_GEN_TAX",
         unit_price: 10,
         discount: 2,
         sales_tax: 4
       }]
      })
    end
  end

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

    it "returns params for creating/updatingin 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