Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refund and create a new order transaction when an order is recalculated #139

Merged
merged 11 commits into from
Apr 20, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## master

- [#139](https://github.com/SuperGoodSoft/solidus_taxjar/pull/139) Refund and create a new order transaction when an order is recalculated
- [#175](https://github.com/SuperGoodSoft/solidus_taxjar/pull/175) Add request logging to TaxJar API requests
- [#138](https://github.com/SuperGoodSoft/solidus_taxjar/pull/138) Add admin UI for configuring reporting
- [#158](https://github.com/SuperGoodSoft/solidus_taxjar/pull/158) Update sandbox bin stub for `solidus@3`
Expand Down
13 changes: 13 additions & 0 deletions app/jobs/super_good/solidus_taxjar/replace_transaction_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module SuperGood
module SolidusTaxjar
class ReplaceTransactionJob < ApplicationJob
forkata marked this conversation as resolved.
Show resolved Hide resolved
queue_as { SuperGood::SolidusTaxjar.job_queue }

def perform(order)
SuperGood::SolidusTaxjar.reporting.refund_and_create_new_transaction(order)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ReportTransactionJob < ApplicationJob
queue_as { SuperGood::SolidusTaxjar.job_queue }

def perform(order)
SuperGood::SolidusTaxjar.reporting.report_transaction(order)
SuperGood::SolidusTaxjar.reporting.show_or_create_transaction(order)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,38 @@ module ReportingSubscriber
if ::Spree::Event.method_defined?(:register)
::Spree::Event.register("shipment_shipped")
end

event_action :report_transaction, event_name: :shipment_shipped
event_action :replace_transaction, event_name: :order_recalculated

def report_transaction(event)
return unless SuperGood::SolidusTaxjar.configuration.preferred_reporting_enabled

SuperGood::SolidusTaxjar::ReportTransactionJob.perform_later(event.payload[:shipment].order)
end

def replace_transaction(event)
order = event.payload[:order]

return unless SuperGood::SolidusTaxjar.configuration.preferred_reporting_enabled

if transaction_replaceable?(order) && amount_changed?(order)
SuperGood::SolidusTaxjar::ReplaceTransactionJob.perform_later(event.payload[:order])
end
end

private

def amount_changed?(order)
SuperGood::SolidusTaxjar.api.show_latest_transaction_for(order).amount !=
Noah-Silvera marked this conversation as resolved.
Show resolved Hide resolved
Noah-Silvera marked this conversation as resolved.
Show resolved Hide resolved
(order.total - order.additional_tax_total)
end

def transaction_replaceable?(order)
order.taxjar_order_transactions.present? &&
order.complete? &&
order.payment_state == "paid"
end
end
end
end
Expand Down
25 changes: 19 additions & 6 deletions lib/super_good/solidus_taxjar/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ def tax_rates_for(address)
end

def create_transaction_for(order)
transaction_id = TransactionIdGenerator.next_transaction_id(order: order)
latest_transaction_id =
OrderTransaction.latest_for(order)&.transaction_id
forkata marked this conversation as resolved.
Show resolved Hide resolved

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

response = taxjar_client.create_order(
ApiParams.transaction_params(order, transaction_id)
)

order.taxjar_order_transactions.create!(
transaction_id: response.transaction_id,
transaction_date: response.transaction_date
Expand All @@ -57,18 +65,23 @@ def show_latest_transaction_for(order)
latest_transaction_id =
OrderTransaction.latest_for(order)&.transaction_id

if latest_transaction_id.nil?
return unless latest_transaction_id

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

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_client.show_order(latest_transaction_id)
end

def create_refund_transaction_for(order)
taxjar_order = show_latest_transaction_for(order)

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

Expand Down
19 changes: 7 additions & 12 deletions lib/super_good/solidus_taxjar/reporting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ def initialize(api: SuperGood::SolidusTaxjar.api)
@api = api
end

def report_transaction(order)
begin
@api.show_latest_transaction_for(order)
rescue NotImplementedError
# FIXME:
# We can stop rescuing from `NotImplementedError` once we have
# fleshed out and implemented functionality to correctly backfill
# TaxJar order transactions and
# `SuperGood::SolidusTaxjar::OrderTransaction` records.
rescue Taxjar::Error::NotFound
@api.create_transaction_for(order)
end
def refund_and_create_new_transaction(order)
benjaminwil marked this conversation as resolved.
Show resolved Hide resolved
@api.create_refund_transaction_for(order)
@api.create_transaction_for(order)
end

def show_or_create_transaction(order)
@api.show_latest_transaction_for(order) || @api.create_transaction_for(order)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
order
transaction_date { Date.current }

sequence(:transaction_id) { |n|
if n == 1
order.number
else
"#{order.number}-#{n - 1}"
end
transient do
last_transaction_id {
benjaminwil marked this conversation as resolved.
Show resolved Hide resolved
SuperGood::SolidusTaxjar::OrderTransaction
.latest_for(order)
&.transaction_id
}
end

transaction_id {
SuperGood::SolidusTaxjar::TransactionIdGenerator
.next_transaction_id(
order: order,
current_transaction_id: last_transaction_id
)
}
end
end
Loading