-
Notifications
You must be signed in to change notification settings - Fork 12
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
Conversation
While doing manual tests, we realized we've missed one condition: The Currently it's impossible to capture a payment if reporting is enabled. |
1ea8154
to
64b59e3
Compare
64b59e3
to
6568158
Compare
I will perform the manual tests tomorrow. But in the meantime, this code is reviewable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some tricky stuff, thanks for diving into it! Overall it looks great, just a couple of comments!
lib/super_good/solidus_taxjar/testing_support/factories/order_transaction_factory.rb
Show resolved
Hide resolved
@Noah-Silvera Thank you for such a thoughtful review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Ben and Co. great work here. Left a few comments around what Noah brought up and I think we should address those concerns before merging this.
@forkata Thank you for the thoughtful review ! |
899465c
to
8574280
Compare
describe "#refund_and_create_transaction" do | ||
subject { reporting.refund_and_create_new_transaction(order) } | ||
|
||
it "refunds the transaction and creates a new one in TaxJar" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why aren't we using VCR for this test but we are for the others?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the only test case where we explicitly cared about an error class from an external library. (A Taxjar::Error
subclass.)
I would like to use VCR more but this is the only case where it seemed absolutely necessary to help our future selves make sense of the extension code being the way it is: @api.create_transaction(order)
will not get executed if @api.refund_transaction(order)
fails beforehand.
Thanks for your patience 🐢 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙇
app/subscribers/super_good/solidus_taxjar/spree/reporting_subscriber.rb
Outdated
Show resolved
Hide resolved
During manual testing, I noticed there's an issue with where the re-reported totals do not match the updated order totals. I will need to debug this. |
We noticed that the `OrderTransaction` factory could create test failures due to a misuse of `#sequence`. The second order transaction created would always be suffixed with `-1`, the third would be suffixed with `-2`, and so on. This is not how order transactions work in the real world. Co-Authored-By: Alex Blackie <[email protected]>
If a TaxJar transaction already exists for a Solidus order and the order totals have changed, we need to cancel (or refund) the existing TaxJar transaction and create a new one with the latest Solidus order totals on it. If the refund fails for some reason, we *do not* want to create the new transaction. So we're testing that a new transaction is not created in that case. We can use this service method to do that in a child commit. Co-Authored-By: Alex Blackie <[email protected]> Co-Authored-By: Mike Conlin <[email protected]>
If a TaxJar order transaction already exists for a Solidus order, we need to increment the transaction ID used. For example: If an order transaction for Solidus order number R123 already exists in TaxJar with the transaction ID "R123", the next transaction ID will be "R123-1". As of this commit, we're checking for order transactions known to exist and incrementing based on the existing transactions IDs. Co-Authored-By: Mike Conlin <[email protected]>
Previously called `#report_transaction`, this method is no longer going to be the only method that technically reports transactions. So it seems like a good moment to rename it to be more specific. Co-Authored-By: Alex Blackie <[email protected]>
If an order total changes, we need to re-report the transaction with the updated order totals. This commit adds a flow for refunding the inaccurate transaction and creating a new one to replace it. This is TaxJar's recommended way of dealing with out-of-date order transactions. Co-Authored-By: Adam Mueller <[email protected]> Co-Authored-By: Alex Blackie <[email protected]> Co-Authored-By: Chris Todorov <[email protected]> Co-Authored-By: Nicholas Van Doorn <[email protected]>
Since we implemented this feature, a new `preferred_reporting_enabled` configuration option has appeared. This commit updates specs to conform to it. It was a very nasty rebase, so we aren't rebasing this. Co-authored-by: Alex Blackie <[email protected]>
Our `ReportingSubscriber#replace_transaction` subscriber should only try to replace an existing transaction if one exists.
The first time that a TaxJar order transaction is created for an order, we know that TaxJar won't already have a transaction on record. We still want to defensively check for a foreign reference to the TaxJar order transaction in the context of an ActiveJob, in case the job is run more than once. In a subsequent change, we'll be re-adding the `NotImplementedError` we removed here in our refund flow. Co-Authored-By: Adam Mueller <[email protected]> Co-Authored-By: Noah Silvera <[email protected]> Co-Authored-By: Chris Todorov <[email protected]>
We haven't finished implementing the refund-and-recreate TaxJar order transactions flow for the TaxJar reporting feature. For now, we'll be sure to raise a `NotImplementedError` if we attempt to create a refund transaction for an order we have no record of being previously reported. Co-Authored-By: Adam Mueller <[email protected]> Co-Authored-By: Chris Todorov <[email protected]> Co-Authored-By: Noah Silvera <[email protected]>
It turns out that `Order#paid?` returns `true` even if credit is owed. So we want to explicitly check that the order's payment state is "paid" and not some other value, like "credit_owed". Co-Authored-By: Noah Silvera <[email protected]>
410c1b5
to
9a029a0
Compare
Latest push rebases onto master. |
Note: @nvandoorn and I tested this and were able to get it to work when rebased on a fix to the discount calculator #181 We tested it by removing X quantity from a line item and then issuing a refund for the payment One test we didn't do which we probably should is testing having two separate line items, and removing one entirely. |
Thanks @joeleonjr. I deleted the comment and rotated the key. Fortunately it's only a test TaxJar trial account. |
What is the goal of this PR?
The goal of this PR is to:
Spree::Event
s.spree_order.total
does not equal the amounts as they are in thematching TaxJar order transactions (closing Allow checking whether TaxJar has up-to-date order totals #106), we need to refund that
transaction and create a new one with the new amounts (closing Custom TaxJar transaction update logic (refund then create) #112).
subscriber.
How do you manually test these changes? (if applicable)
In a sandbox application:
solidus_taxjar
reporting.spree_order.order_transactions
has a record.order_recalculated
event has fired.spree_order.order_transactions
has a second record.Merge Checklist
Screenshots
N/A