How should finance handle the accounts of two companies belonging to the same group?

Company A and Company B, which belong to the same group, mistakenly returned the money that should be returned to Company B to Company A, because both companies have independent accounting, and now they need to reconcile. The best way is for company A to return the money, and then the customer will pay the money to company B.. If you feel that the risk is high, you can write a tripartite agreement, stating that the wrong money will be transferred directly from Company A to Company B, and the remittance can be made after the three parties sign and seal it.