I am developing an api gateway service for an inherited service, the customer sends payment orders to the gateway and the gateway sends the order to the legacy service. Every payment order comes with a tracking key, with this key the legacy service checks if the order is duplicated. If an order is duplicated, the legacy service sends me a status 200 and I need to look for an error property in the response, but the client could change the tracking key and try again, so I was thinking of changing the response of that error in the gateway and not responding successfully, but I am I am not really sure if it is the correct way and what error to throw at the client, at the beginning I was thinking about BadGatewayException, since the error is in the legacy service.
I think Http status code of 409 would suit in this scenario because you are trying to create a order which already exist on the server. Since the changing the tracking id by the user fixes the conflict, I would say return 409.
Refer the doco for HTTP Status code 409
The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.
Reference : HTTP Status Code Definitions