2

In a REST API, in payment method (user gives his card infos), should I return 200 if the payment fails (for card expiration or balance too low problem) with an attached message + custom additional error code in JSON ? Or is there any other HTTP code for this ?

I did not found any information about this particular case, I just found 402 code for payment but it does not seem to be designed for this case. I'm not talking about a server (500) error or unreachable bank, just payment problem due to user card.

rap-2-h
  • 23,287
  • 23
  • 130
  • 217

2 Answers2

0

I think you should use 406 code.

Code - 406

Message - Not Acceptable

Description - Insufficient funds or other payment problem.

asega
  • 1
  • 3
  • Not acceptable specifically pertains to failures related to the `Accept`, `Accept-Language`, and `Accept-Encoding` request headers, so this is not correct. – Evert Sep 24 '20 at 05:41
-2

What does "the payment fails" mean exactly? I can think of a few different scenarios, many of which might require a different status code to reflect the actual status.

It all depends how the rest of your API works though. If you already use JSON objects in the response body, you may as well report 200 OK for any action:

HTTP/1.1 200 OK
...

{ 
    success: false,
    message: "Balance too low"
}

If you don't want that, any 4xx or 5xx error that applies will do. Try searching.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
  • Ok, thanks :). "What does "the payment fails" mean exactly?" Any scenario (except server error 500). My question is about payment failure in general. The cause will be in a JSON message with a custom error code, but the question was just about the HTTP Code – rap-2-h May 26 '15 at 13:42
  • Well for that, see the linked questions and search some more. There's a _lot_ of discussion about which status code to use in which situation. _You_ will have to decide which HTTP status code is appropriate. When the payment provider is down, do you want to return the same code as when the user doesn't have enough balance? Go read and pick any. – CodeCaster May 26 '15 at 13:44
  • Ok. I know there's many discussion about status code. Sorry if my question was unclear. I know an API can send 400 on a bad request or 404 on not found resource, but for a refused payment (balance too low, invalid card, etc.) I don't know which one I should use. Which one would *you* choose on "invalid card" ? I will edit my question too. – rap-2-h May 26 '15 at 13:51