Class: SuperGood::SolidusTaxjar::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/super_good/solidus_taxjar/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taxjar_client: self.class.default_taxjar_client) ⇒ Api

Returns a new instance of Api.



16
17
18
# File 'lib/super_good/solidus_taxjar/api.rb', line 16

def initialize(taxjar_client: self.class.default_taxjar_client)
  @taxjar_client = taxjar_client
end

Class Method Details

.default_taxjar_clientObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/super_good/solidus_taxjar/api.rb', line 4

def self.default_taxjar_client
  client = ::Taxjar::Client.new(
    api_key: ENV["TAXJAR_API_KEY"],
    api_url: ENV.fetch("TAXJAR_API_URL") { "https://api.taxjar.com" } # Sandbox URL: https://api.sandbox.taxjar.com
  )
  client.set_api_config('headers', {
    'x-api-version' => '2020-08-07',
    'plugin' => 'supergoodsolidustaxjar'
  })
  client
end

Instance Method Details

#create_refund_for(reimbursement) ⇒ Object



82
83
84
# File 'lib/super_good/solidus_taxjar/api.rb', line 82

def create_refund_for(reimbursement)
  taxjar_client.create_refund ApiParams.refund_params(reimbursement)
end

#create_refund_transaction_for(order) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/super_good/solidus_taxjar/api.rb', line 69

def create_refund_transaction_for(order)
  unless OrderTransaction.latest_for(order)
    raise NotImplementedError,
      "No latest TaxJar order transaction for #{order.number}. "       \
      "Backfilling TaxJar transaction orders from Solidus is not yet " \
      "implemented."
  end

  taxjar_order = show_latest_transaction_for(order)

  taxjar_client.create_refund ApiParams.refund_transaction_params(order, taxjar_order)
end

#create_transaction_for(order) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/super_good/solidus_taxjar/api.rb', line 36

def create_transaction_for(order)
  latest_transaction_id =
    OrderTransaction.latest_for(order)&.transaction_id

  transaction_id = TransactionIdGenerator.next_transaction_id(
    order: order,
    current_transaction_id: latest_transaction_id
  )

  taxjar_client.create_order(
    ApiParams.transaction_params(order, transaction_id)
  )
end

#delete_transaction_for(order) ⇒ Object



54
55
56
# File 'lib/super_good/solidus_taxjar/api.rb', line 54

def delete_transaction_for(order)
  taxjar_client.delete_order order.number
end

#nexus_regionsObject



90
91
92
# File 'lib/super_good/solidus_taxjar/api.rb', line 90

def nexus_regions
  taxjar_client.nexus_regions
end

#show_latest_transaction_for(order) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/super_good/solidus_taxjar/api.rb', line 58

def show_latest_transaction_for(order)
  latest_transaction_id =
    OrderTransaction.latest_for(order)&.transaction_id

  return unless latest_transaction_id

  taxjar_client.show_order(latest_transaction_id)
rescue Taxjar::Error::NotFound
  nil
end

#tax_categoriesObject



20
21
22
# File 'lib/super_good/solidus_taxjar/api.rb', line 20

def tax_categories
  taxjar_client.categories
end

#tax_for(order) ⇒ Object



24
25
26
# File 'lib/super_good/solidus_taxjar/api.rb', line 24

def tax_for(order)
  taxjar_client.tax_for_order(ApiParams.order_params(order))
end

#tax_rate_for(address) ⇒ Object



28
29
30
# File 'lib/super_good/solidus_taxjar/api.rb', line 28

def tax_rate_for(address)
  taxjar_client.tax_for_order(ApiParams.tax_rate_address_params(address)).rate
end

#tax_rates_for(address) ⇒ Object



32
33
34
# File 'lib/super_good/solidus_taxjar/api.rb', line 32

def tax_rates_for(address)
  taxjar_client.rates_for_location(*ApiParams.address_params(address))
end

#update_transaction_for(order) ⇒ Object



50
51
52
# File 'lib/super_good/solidus_taxjar/api.rb', line 50

def update_transaction_for(order)
  taxjar_client.update_order ApiParams.transaction_params(order)
end

#validate_spree_address(spree_address) ⇒ Object



86
87
88
# File 'lib/super_good/solidus_taxjar/api.rb', line 86

def validate_spree_address(spree_address)
  taxjar_client.validate_address ApiParams.validate_address_params(spree_address)
end