Rest – Http Status Code When Downstream Validation Fails

apimicroservicesrestwebweb-applications

I have an API that charges for an order. It accepts the orderId and the amount as inputs. Then it makes a '/charge' call to the downstream, which returns a 202. Immediately after this call, it calls a '/verify' endpoint to make sure that the previous charge was successful.

Now it may happen that the charge was declined. One of the reasons for this can be that the user used an expired card. What should be the error code in this scenario?

As I see it, I can't send a 4xx as the request was correct for my API perspective. A bad request is something that the user can correct – In this case, he can't correct anything since the API just accepts the 'orderId' and the total amount to charge.

If I am sending a 5XX, then 500 does not make sense as this was not an 'unexpected condition' on my server. I can neither send a 503 as my server was not overloaded or down for maintenance.

Currently, I am sending back a 503 with an app code that maps to: Payment verification failed.

Best Answer

HTTP Status Codes are only designed to tell you about the status of your HTTP transmissions. They have no notion of "business rules."

Stick to 200 and 400. When you get a 400, you can retry the request or fail it. When you get a 200, your message is valid; you can then check the returned metadata for status information from your application (i.e. whether the payment succeeded or not).

Related Topic