I am trying to match BalanceTransactions to OrderTransactions.
Looking at balanceTransactions I see the following field.
With the following description.
The id of the Order Transaction that resulted in this balance transaction.
Seems to be exactly what I am looking for. A direct foreign key from BalanceTransaction to OrderTransaction. I can now use this Id to find the OrderTransaction
WRONG!
SOMETIMES, it is the direct foreign key, but OTHER TIMES, it is the ID of the PARENT transaction.
OK, not too bad, we can check for both values. Sometimes it actually matches the grandparent (the parent of the parent) of the transaction. That’s OK as well, recursion exists for a reason.
Hmm, wait a minute, won’t that introduce false positives? Yes it will, however usually only refunds have grandparents, so we can check if the type of the balanceTransaction matches the type of the OrderTransaction (with some mapping involved).
OK, this is manageable.
Now I have found an edge case. the ID matches a SIBLING Transaction (both share the same parent)
Now what? I can check sibling transactions, but this will most probably result in false positives.
And I go back to the wording of the documentation.
The id of the Order Transaction that resulted in this balance transaction.
I can see how the order transaction itself resulted in the balance transaction. I can see how the parent transaction (indirectly) resulted in the balance transaction. I can see how the auth of the order that was refunded resulted in the refund balance transaction, but I fail to see how a sibling transaction “resulted” in this transaction.
What should have been a simple task, (matching one id to the other) has morphed into a complicated mess.
My biggest issue is that it isn’t consistent, if it would always match the sibling ID then OK, I can write nonsensical code. But as it stands, this would find false positives.
NOTE: I am using the Rest version to get balanceTransactions, because the GraphQL version requires different scopes, (which should be fixed, either with a top level query for balanceTransactions or smarter checking on the shopifyPaymentsAccount query for scopes. but this is not the topic now)
PS: I have tried using both amounts and processing dates to match. They don’t work. They have edge cases as well, and those are much harder to navigate without causing false positives.