stripe_error.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "stripe/stripe_error.h"
  8. #include "stripe/stripe_decode.h"
  9. namespace Stripe {
  10. Error::Code Error::code() const {
  11. return _code;
  12. }
  13. QString Error::description() const {
  14. return _description;
  15. }
  16. QString Error::message() const {
  17. return _message;
  18. }
  19. QString Error::parameter() const {
  20. return _parameter;
  21. }
  22. Error Error::None() {
  23. return Error(Code::None, {}, {}, {});
  24. }
  25. Error Error::DecodedObjectFromResponse(QJsonObject object) {
  26. const auto entry = object.value("error");
  27. if (!entry.isObject()) {
  28. return Error::None();
  29. }
  30. const auto error = entry.toObject();
  31. const auto string = [&](QStringView key) {
  32. return error.value(key).toString();
  33. };
  34. const auto type = string(u"type");
  35. const auto message = string(u"message");
  36. const auto parameterSnakeCase = string(u"param");
  37. // There should always be a message and type for the error
  38. if (message.isEmpty() || type.isEmpty()) {
  39. return {
  40. Code::API,
  41. "GenericError",
  42. "Could not interpret the error response "
  43. "that was returned from Stripe."
  44. };
  45. }
  46. auto parameterWords = parameterSnakeCase.isEmpty()
  47. ? QStringList()
  48. : parameterSnakeCase.split('_', Qt::SkipEmptyParts);
  49. auto first = true;
  50. for (auto &word : parameterWords) {
  51. if (first) {
  52. first = false;
  53. } else {
  54. word = word[0].toUpper() + word.mid(1);
  55. }
  56. }
  57. const auto parameter = parameterWords.join(QString());
  58. if (type == "api_error") {
  59. return { Code::API, "GenericError", message, parameter };
  60. } else if (type == "invalid_request_error") {
  61. return { Code::InvalidRequest, "GenericError", message, parameter };
  62. } else if (type != "card_error") {
  63. return { Code::Unknown, type, message, parameter };
  64. }
  65. const auto code = string(u"code");
  66. const auto cardError = [&](const QString &description) {
  67. return Error{ Code::Card, description, message, parameter };
  68. };
  69. if (code == "incorrect_number") {
  70. return cardError("IncorrectNumber");
  71. } else if (code == "invalid_number") {
  72. return cardError("InvalidNumber");
  73. } else if (code == "invalid_expiry_month") {
  74. return cardError("InvalidExpiryMonth");
  75. } else if (code == "invalid_expiry_year") {
  76. return cardError("InvalidExpiryYear");
  77. } else if (code == "invalid_cvc") {
  78. return cardError("InvalidCVC");
  79. } else if (code == "expired_card") {
  80. return cardError("ExpiredCard");
  81. } else if (code == "incorrect_cvc") {
  82. return cardError("IncorrectCVC");
  83. } else if (code == "card_declined") {
  84. return cardError("CardDeclined");
  85. } else if (code == "processing_error") {
  86. return cardError("ProcessingError");
  87. } else {
  88. return cardError(code);
  89. }
  90. }
  91. bool Error::empty() const {
  92. return (_code == Code::None);
  93. }
  94. } // namespace Stripe