summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-31 09:24:44 -0800
committerJared Norman <jared@super.gd>2019-01-31 09:24:44 -0800
commit8581484be15ed19bbd37d861b082ba1e9b34b978 (patch)
treec80167bd82aa785d9eac6006c7e76a6920c9e3d1
parent61837b129c1e580085b0f1abc1ec2f51b4b8142b (diff)
Add ability to create refunds
-rw-r--r--lib/super_good/solidus_taxjar/api.rb4
-rw-r--r--lib/super_good/solidus_taxjar/api_params.rb15
-rw-r--r--spec/super_good/solidus_taxjar/api_params_spec.rb32
-rw-r--r--spec/super_good/solidus_taxjar/api_spec.rb22
4 files changed, 73 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb
index 61fecae..7309a64 100644
--- a/lib/super_good/solidus_taxjar/api.rb
+++ b/lib/super_good/solidus_taxjar/api.rb
@@ -32,6 +32,10 @@ module SuperGood
taxjar_client.delete_order order.number
end
+ def create_refund_for(reimbursement)
+ taxjar_client.create_refund APIParams.refund_params(reimbursement)
+ end
+
private
attr_reader :taxjar_client
diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb
index f75713c..b848eee 100644
--- a/lib/super_good/solidus_taxjar/api_params.rb
+++ b/lib/super_good/solidus_taxjar/api_params.rb
@@ -33,6 +33,21 @@ module SuperGood
)
end
+ def refund_params(reimbursement)
+ additional_taxes = reimbursement.return_items.sum(&:additional_tax_total)
+
+ {}
+ .merge(order_address_params(reimbursement.order.tax_address))
+ .merge(
+ transaction_id: reimbursement.number,
+ transaction_reference_id: reimbursement.order.number,
+ transaction_date: reimbursement.order.completed_at.to_formatted_s(:iso8601),
+ amount: reimbursement.total - additional_taxes,
+ shipping: 0,
+ sales_tax: additional_taxes
+ )
+ end
+
private
def order_address_params(address)
diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb
index cb70804..8b0e9d0 100644
--- a/spec/super_good/solidus_taxjar/api_params_spec.rb
+++ b/spec/super_good/solidus_taxjar/api_params_spec.rb
@@ -104,6 +104,18 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do
)
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) }
@@ -162,4 +174,24 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do
})
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
diff --git a/spec/super_good/solidus_taxjar/api_spec.rb b/spec/super_good/solidus_taxjar/api_spec.rb
index 64f017d..0f890ca 100644
--- a/spec/super_good/solidus_taxjar/api_spec.rb
+++ b/spec/super_good/solidus_taxjar/api_spec.rb
@@ -105,4 +105,26 @@ RSpec.describe SuperGood::SolidusTaxJar::API do
it { is_expected.to eq({ some_kind_of: "response" }) }
end
+
+ describe "#create_refund_for" do
+ subject { api.create_refund_for reimbursement }
+
+ let(:api) { described_class.new(taxjar_client: dummy_client) }
+ let(:dummy_client) { instance_double ::Taxjar::Client }
+ let(:reimbursement) { Spree::Reimbursement.new }
+
+ before do
+ allow(SuperGood::SolidusTaxJar::APIParams)
+ .to receive(:refund_params)
+ .with(reimbursement)
+ .and_return({ refund: "params" })
+
+ allow(dummy_client)
+ .to receive(:create_refund)
+ .with({ refund: "params" })
+ .and_return({ some_kind_of: "response" })
+ end
+
+ it { is_expected.to eq({ some_kind_of: "response" }) }
+ end
end