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

Conversation

benjaminwil
Copy link
Member

@benjaminwil benjaminwil commented Dec 16, 2021

What is the goal of this PR?

The goal of this PR is to:

How do you manually test these changes? (if applicable)

In a sandbox application:

  • Enable solidus_taxjar reporting.
  • Ensure a tax rate named "Sales Tax" is setup
  • Complete an order and pay for it (and then capture the payment)
  • Mark the order as shipped
  • Check that spree_order.order_transactions has a record.
  • Update the order total.
    • One easy way to do this would be to check out with two line items and then cancel one of them.
    • Order-level adjustments will not work.
  • See that the order_recalculated event has fired.
  • Check that spree_order.order_transactions has a second record.
  • Check the TaxJar reporting dashboard to ensure the reporting totals are correct.

Merge Checklist

  • Run the manual tests
  • Update the changelog

Screenshots

N/A

@benjaminwil benjaminwil changed the title Update TaxJar order transactions when "order_recalculate" events are fired Refund and create a new order transaction when an order is recalculated Dec 16, 2021
@benjaminwil
Copy link
Member Author

benjaminwil commented Dec 16, 2021

While doing manual tests, we realized we've missed one condition:

The #update_transaction reporting subscriber should only happen if the order has already shipped. At least until we've gotten rid of this StandardError, which will be replaced by functionality we just haven't built out yet.

Currently it's impossible to capture a payment if reporting is enabled.

@benjaminwil benjaminwil force-pushed the bw/order-recalculated-subscriber branch from 1ea8154 to 64b59e3 Compare January 21, 2022 23:18
@benjaminwil benjaminwil force-pushed the bw/order-recalculated-subscriber branch from 64b59e3 to 6568158 Compare February 3, 2022 23:06
@benjaminwil
Copy link
Member Author

I will perform the manual tests tomorrow. But in the meantime, this code is reviewable.

Copy link
Member

@Noah-Silvera Noah-Silvera left a 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!

@benjaminwil
Copy link
Member Author

@Noah-Silvera Thank you for such a thoughtful review!

Copy link
Collaborator

@forkata forkata left a 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.

@benjaminwil
Copy link
Member Author

@forkata Thank you for the thoughtful review !

@benjaminwil benjaminwil force-pushed the bw/order-recalculated-subscriber branch 2 times, most recently from 899465c to 8574280 Compare February 17, 2022 23:43
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
Copy link
Member

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?

Copy link
Member Author

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.

@Noah-Silvera
Copy link
Member

Thanks for your patience 🐢

Copy link
Contributor

@stewart stewart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇

@benjaminwil
Copy link
Member Author

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.

benjaminwil and others added 2 commits March 22, 2022 14:21
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]>
benjaminwil and others added 9 commits March 22, 2022 14:21
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]>
@nvandoorn nvandoorn force-pushed the bw/order-recalculated-subscriber branch from 410c1b5 to 9a029a0 Compare March 22, 2022 23:46
@nvandoorn
Copy link
Member

Latest push rebases onto master.

@Noah-Silvera
Copy link
Member

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.

@nvandoorn nvandoorn merged commit 416c09b into master Apr 20, 2022
@nvandoorn nvandoorn deleted the bw/order-recalculated-subscriber branch April 20, 2022 00:08
@nvandoorn
Copy link
Member

Thanks @joeleonjr. I deleted the comment and rotated the key. Fortunately it's only a test TaxJar trial account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants